@@ -22,26 +22,40 @@ export class SrpClient {
22
22
private m : Uint8Array | null = null ;
23
23
private cProof : Uint8Array | null = null ;
24
24
private isServerProved : boolean = false ;
25
- private group : SrpGroup ;
25
+ private group : SrpGroup | null = null ;
26
26
private badState = false ;
27
27
28
28
constructor ( group : SrpGroup , v : BigInteger , k ?: BigInteger ) {
29
- this . group = group ;
29
+ return this . newSrp ( group , v , k ) ;
30
+ }
30
31
32
+ private newSrp ( group : SrpGroup , xORv : BigInteger , k ?: BigInteger ) {
33
+ this . group = group ;
34
+ this . x = xORv ;
31
35
if ( k ) {
32
36
this . k = k ;
33
37
} else {
38
+ this . k = this . makeLittleK ( ) ;
34
39
}
40
+ this . generateMySecret ( ) ;
41
+ this . makeA ( ) ;
42
+ return this ;
35
43
}
36
44
37
45
private makeLittleK ( ) : BigInteger {
38
46
const hash = createHash ( "sha256" ) ;
47
+ if ( ! this . group ) {
48
+ throw new Error ( "group is not set" ) ;
49
+ }
39
50
hash . update ( new Uint8Array ( this . group . getN ( ) . toByteArray ( ) ) ) ;
40
51
hash . update ( new Uint8Array ( this . group . getGenerator ( ) . toByteArray ( ) ) ) ;
41
52
return hexToBigInt ( hash . digest ( "hex" ) ) ;
42
53
}
43
54
44
55
private generateMySecret ( ) : BigInteger {
56
+ if ( ! this . group ) {
57
+ throw new Error ( "group is not set" ) ;
58
+ }
45
59
const eSize = maxInt ( this . group . exponentSize , minExponentSize ) ;
46
60
// get eSize random bytes
47
61
const bytes = randomBytes ( eSize ) ;
@@ -53,6 +67,9 @@ export class SrpClient {
53
67
if ( this . ephemeralPrivate === zero ) {
54
68
this . generateMySecret ( ) ;
55
69
}
70
+ if ( ! this . group ) {
71
+ throw new Error ( "group is not set" ) ;
72
+ }
56
73
this . ephemeralPublicA = this . group
57
74
. getGenerator ( )
58
75
. modPow ( this . ephemeralPrivate , this . group . getN ( ) ) ;
@@ -77,11 +94,17 @@ export class SrpClient {
77
94
if ( this . x . equals ( zero ) ) {
78
95
throw new Error ( "x must be known to calculate v" ) ;
79
96
}
97
+ if ( ! this . group ) {
98
+ throw new Error ( "group is not set" ) ;
99
+ }
80
100
this . v = this . group . getGenerator ( ) . modPow ( this . x , this . group . getN ( ) ) ;
81
101
return this . v ;
82
102
}
83
103
84
104
public isPublicValid ( AorB : BigInteger ) : boolean {
105
+ if ( ! this . group ) {
106
+ throw new Error ( "group is not set" ) ;
107
+ }
85
108
if ( this . group . getGenerator ( ) . compareTo ( zero ) === 0 ) {
86
109
return false ;
87
110
}
@@ -186,6 +209,10 @@ need that.
186
209
e = this . u . multiply ( this . x ) ;
187
210
e = e . add ( this . ephemeralPrivate ) ;
188
211
212
+ if ( ! this . group ) {
213
+ throw new Error ( "group is not set" ) ;
214
+ }
215
+
189
216
b = this . group . getGenerator ( ) . modPow ( this . x , this . group . getN ( ) ) ;
190
217
b = b . multiply ( this . k ) ;
191
218
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
212
239
slice (without padding to size of N)
213
240
*/
214
241
public computeM ( salt : Uint8Array , uname : string ) : Uint8Array {
242
+ if ( ! this . group ) {
243
+ throw new Error ( "group is not set" ) ;
244
+ }
215
245
const nLen = bigIntToBytes ( this . group . getN ( ) ) . length ;
216
246
console . log ( `Server padding length: ${ nLen } ` ) ;
217
247
0 commit comments