Skip to content

Commit 3a82032

Browse files
committed
Simplify checksum logic
1 parent 7cd1fce commit 3a82032

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/rendezvous/channels/ecdhV1.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ export interface ECDHv1RendezvousCode extends RendezvousCode {
3838
};
3939
}
4040

41-
// n.b. this is a copy and paste of:
41+
// The underlying algorithm is the same as:
4242
// https://github.com/matrix-org/matrix-js-sdk/blob/75204d5cd04d67be100fca399f83b1a66ffb8118/src/crypto/verification/SAS.ts#L54-L68
43-
function generateDecimalSas(sasBytes: number[]): [number, number, number] {
43+
function generateDecimalSas(sasBytes: number[]): string {
4444
/**
4545
* +--------+--------+--------+--------+--------+
4646
* | Byte 0 | Byte 1 | Byte 2 | Byte 3 | Byte 4 |
@@ -49,11 +49,13 @@ function generateDecimalSas(sasBytes: number[]): [number, number, number] {
4949
* \____________/\_____________/\____________/
5050
* 1st number 2nd number 3rd number
5151
*/
52-
return [
52+
const digits = [
5353
(sasBytes[0] << 5 | sasBytes[1] >> 3) + 1000,
5454
((sasBytes[1] & 0x7) << 10 | sasBytes[2] << 2 | sasBytes[3] >> 6) + 1000,
5555
((sasBytes[3] & 0x3f) << 7 | sasBytes[4] >> 1) + 1000,
5656
];
57+
58+
return digits.join('-');
5759
}
5860

5961
export class ECDHv1RendezvousChannel implements RendezvousChannel {
@@ -142,7 +144,7 @@ export class ECDHv1RendezvousChannel implements RendezvousChannel {
142144
logger.debug(`AES key: ${encodeBase64(this.aesKey)}`);
143145

144146
const rawChecksum = this.olmSAS.generate_bytes(aesInfo, 5);
145-
return generateDecimalSas(Array.from(rawChecksum)).join('-');
147+
return generateDecimalSas(Array.from(rawChecksum));
146148
}
147149

148150
public async send(data: any) {

0 commit comments

Comments
 (0)