Skip to content

Commit c5fad12

Browse files
author
XuXiangjun
committed
Fix dh secret
1 parent 87456f8 commit c5fad12

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

BlufiLibrary/Security/BlufiDH.m

+20-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,26 @@ - (NSData *)generateSecret:(NSData *)srcPublicKey {
3434
ret = DH_compute_key(shareKey, pubKey, _dh);
3535
}
3636
BN_free(pubKey);
37-
return [NSData dataWithBytes:shareKey length:128];
37+
38+
int offset = 0;
39+
for (int i = 0; i < 128; i++) {
40+
if (shareKey[i] == 0) {
41+
offset++;
42+
} else {
43+
break;
44+
}
45+
}
46+
47+
if (offset == 0) {
48+
return [NSData dataWithBytes:shareKey length:128];
49+
} else {
50+
int secretLength = 128 - offset;
51+
Byte secretKey[secretLength];
52+
for (int i = 0; i < secretLength; i++) {
53+
secretKey[i] = shareKey[i + offset];
54+
}
55+
return [NSData dataWithBytes:secretKey length:secretLength];
56+
}
3857
}
3958

4059
- (void)releaseDH {

0 commit comments

Comments
 (0)