-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
37 lines (31 loc) · 1.22 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import crypto from "node:crypto";
function xor(a: Buffer, b: Buffer) {
const result = Buffer.allocUnsafe(a.length);
for (let i = 0; i < a.length; i++) {
result[i] = a[i] ^ b[i];
}
return result;
}
function sha256(password: Buffer) {
const hash = crypto.createHash("sha256");
return hash.update(password).digest();
}
function sha1(password: Buffer) {
const hash = crypto.createHash("sha1");
return hash.update(password).digest();
}
function calculateTokenCaching256(password: string, scramble: string) {
// SHA256( password ) XOR SHA256( "20-bytes random data from server" <concat> SHA256( SHA1( password ) ) )
const stage1 = sha256(Buffer.from(password));
const stage2 = sha256(stage1);
const stage3 = sha256(Buffer.concat([stage2, Buffer.from(scramble)]))
return xor(stage1, stage3);
}
function calculateTokenNativePassword(password: string, scramble: string) {
// SHA1( password ) XOR SHA1( "20-bytes random data from server" <concat> SHA1( SHA1( password ) ) )
const stage1 = sha1(Buffer.from(password));
const stage2 = sha1(stage1);
const stage3 = sha1(Buffer.concat([Buffer.from(scramble), stage2]))
return xor(stage1, stage3);
}
export {calculateTokenCaching256, calculateTokenNativePassword};