-
Notifications
You must be signed in to change notification settings - Fork 92
/
auth.ts
25 lines (22 loc) · 898 Bytes
/
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
import { crypto, hex } from "../deps.ts";
const encoder = new TextEncoder();
async function md5(bytes: Uint8Array): Promise<string> {
return hex.encodeHex(await crypto.subtle.digest("MD5", bytes));
}
// AuthenticationMD5Password
// The actual PasswordMessage can be computed in SQL as:
// concat('md5', md5(concat(md5(concat(password, username)), random-salt))).
// (Keep in mind the md5() function returns its result as a hex string.)
export async function hashMd5Password(
password: string,
username: string,
salt: Uint8Array,
): Promise<string> {
const innerHash = await md5(encoder.encode(password + username));
const innerBytes = encoder.encode(innerHash);
const outerBuffer = new Uint8Array(innerBytes.length + salt.length);
outerBuffer.set(innerBytes);
outerBuffer.set(salt, innerBytes.length);
const outerHash = await md5(outerBuffer);
return "md5" + outerHash;
}