@@ -22,26 +22,40 @@ export class SrpClient {
2222 private m : Uint8Array | null = null ;
2323 private cProof : Uint8Array | null = null ;
2424 private isServerProved : boolean = false ;
25- private group : SrpGroup ;
25+ private group : SrpGroup | null = null ;
2626 private badState = false ;
2727
2828 constructor ( group : SrpGroup , v : BigInteger , k ?: BigInteger ) {
29- this . group = group ;
29+ return this . newSrp ( group , v , k ) ;
30+ }
3031
32+ private newSrp ( group : SrpGroup , xORv : BigInteger , k ?: BigInteger ) {
33+ this . group = group ;
34+ this . x = xORv ;
3135 if ( k ) {
3236 this . k = k ;
3337 } else {
38+ this . k = this . makeLittleK ( ) ;
3439 }
40+ this . generateMySecret ( ) ;
41+ this . makeA ( ) ;
42+ return this ;
3543 }
3644
3745 private makeLittleK ( ) : BigInteger {
3846 const hash = createHash ( "sha256" ) ;
47+ if ( ! this . group ) {
48+ throw new Error ( "group is not set" ) ;
49+ }
3950 hash . update ( new Uint8Array ( this . group . getN ( ) . toByteArray ( ) ) ) ;
4051 hash . update ( new Uint8Array ( this . group . getGenerator ( ) . toByteArray ( ) ) ) ;
4152 return hexToBigInt ( hash . digest ( "hex" ) ) ;
4253 }
4354
4455 private generateMySecret ( ) : BigInteger {
56+ if ( ! this . group ) {
57+ throw new Error ( "group is not set" ) ;
58+ }
4559 const eSize = maxInt ( this . group . exponentSize , minExponentSize ) ;
4660 // get eSize random bytes
4761 const bytes = randomBytes ( eSize ) ;
@@ -53,6 +67,9 @@ export class SrpClient {
5367 if ( this . ephemeralPrivate === zero ) {
5468 this . generateMySecret ( ) ;
5569 }
70+ if ( ! this . group ) {
71+ throw new Error ( "group is not set" ) ;
72+ }
5673 this . ephemeralPublicA = this . group
5774 . getGenerator ( )
5875 . modPow ( this . ephemeralPrivate , this . group . getN ( ) ) ;
@@ -77,11 +94,17 @@ export class SrpClient {
7794 if ( this . x . equals ( zero ) ) {
7895 throw new Error ( "x must be known to calculate v" ) ;
7996 }
97+ if ( ! this . group ) {
98+ throw new Error ( "group is not set" ) ;
99+ }
80100 this . v = this . group . getGenerator ( ) . modPow ( this . x , this . group . getN ( ) ) ;
81101 return this . v ;
82102 }
83103
84104 public isPublicValid ( AorB : BigInteger ) : boolean {
105+ if ( ! this . group ) {
106+ throw new Error ( "group is not set" ) ;
107+ }
85108 if ( this . group . getGenerator ( ) . compareTo ( zero ) === 0 ) {
86109 return false ;
87110 }
@@ -186,6 +209,10 @@ need that.
186209 e = this . u . multiply ( this . x ) ;
187210 e = e . add ( this . ephemeralPrivate ) ;
188211
212+ if ( ! this . group ) {
213+ throw new Error ( "group is not set" ) ;
214+ }
215+
189216 b = this . group . getGenerator ( ) . modPow ( this . x , this . group . getN ( ) ) ;
190217 b = b . multiply ( this . k ) ;
191218 b = this . ephemeralPublicB . subtract ( b ) ;
@@ -212,6 +239,9 @@ We will use math/big Bytes() to get the absolute value as a big-endian byte
212239slice (without padding to size of N)
213240*/
214241 public computeM ( salt : Uint8Array , uname : string ) : Uint8Array {
242+ if ( ! this . group ) {
243+ throw new Error ( "group is not set" ) ;
244+ }
215245 const nLen = bigIntToBytes ( this . group . getN ( ) ) . length ;
216246 console . log ( `Server padding length: ${ nLen } ` ) ;
217247
0 commit comments