|
1 |
| -import { |
2 |
| - hash as hashArgon2, |
3 |
| - Argon2Params, |
4 |
| - Argon2Version, |
5 |
| - Argon2Algorithm, |
6 |
| -} from "https://deno.land/x/[email protected]/mod.ts"; |
7 |
| -import { generateSalt } from "./common.ts"; |
8 |
| -import { timingSafeEqual } from "https://deno.land/[email protected]/crypto/timing_safe_equal.ts"; |
9 |
| -import base64 from "https://deno.land/x/[email protected]/src/base64.js"; |
10 |
| - |
11 |
| - |
12 |
| -// OWASP recommended defaults: |
13 |
| -// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id |
14 |
| -const argon2Defaults: Argon2Params = { |
15 |
| - algorithm: "Argon2id", |
16 |
| - version: 19, |
17 |
| - tCost: 2, // 3 iterations |
18 |
| - mCost: 19 * 1024, // 19MiB of memory |
19 |
| - pCost: 1, // 1 thread |
20 |
| - |
21 |
| - secret: undefined, |
22 |
| - outputLength: 32, |
23 |
| -}; |
24 |
| - |
25 |
| -export function createHash(password: string) { |
26 |
| - const salt = generateSalt(); |
27 |
| - const passwordBuffer = new TextEncoder().encode(password); |
28 |
| - const hash = hashArgon2(passwordBuffer, salt, argon2Defaults); |
29 |
| - return packDigest(hash, salt, argon2Defaults); |
30 |
| -} |
31 |
| - |
32 |
| -export function hashMatches(guess: string, digest: string) { |
33 |
| - const { hash, salt, params } = unpackDigest(digest); |
34 |
| - const guessBuffer = new TextEncoder().encode(guess); |
35 |
| - |
36 |
| - const hashGuess = hashArgon2(guessBuffer, salt, params); |
37 |
| - |
38 |
| - return timingSafeEqual(hashGuess, hash); |
39 |
| -} |
40 |
| - |
41 |
| -function packDigest(hash: ArrayBuffer, salt: ArrayBuffer, params: Argon2Params) { |
42 |
| - const headerPart = `$${params.algorithm.toLowerCase()}$v=${params.version}$`; |
43 |
| - const paramPart = `m=${params.mCost},t=${params.tCost},p=${params.pCost}`; |
44 |
| - |
45 |
| - const saltBase64 = base64.fromArrayBuffer(salt); |
46 |
| - const hashBase64 = base64.fromArrayBuffer(hash); |
47 |
| - |
48 |
| - return `${headerPart}${paramPart}$${saltBase64}$${hashBase64}`; |
49 |
| -} |
50 |
| - |
51 |
| -const algorithmMap: Record<string, Argon2Algorithm> = { |
52 |
| - "argon2i": "Argon2i", |
53 |
| - "argon2d": "Argon2d", |
54 |
| - "argon2id": "Argon2id", |
55 |
| -}; |
56 |
| - |
57 |
| -function unpackDigest(digest: string): { hash: ArrayBuffer, salt: ArrayBuffer, params: Argon2Params } { |
58 |
| - const [, algorithmName, versionStr, params, saltStr, hashStr] = digest.split("$"); |
59 |
| - |
60 |
| - const algorithm = algorithmMap[algorithmName]; |
61 |
| - const version = parseInt(versionStr.match(/v=(\d+)/)![1]); |
62 |
| - |
63 |
| - const mCost = parseInt(params.match(/m=(\d+)/)![1]); |
64 |
| - const tCost = parseInt(params.match(/t=(\d+)/)![1]); |
65 |
| - const pCost = parseInt(params.match(/p=(\d+)/)![1]); |
66 |
| - |
67 |
| - const salt = base64.toArrayBuffer(saltStr); |
68 |
| - const hash = base64.toArrayBuffer(hashStr); |
69 |
| - |
70 |
| - |
71 |
| - if (!algorithm || !version || !mCost || !tCost || !pCost || !salt || !hash) { |
72 |
| - throw new Error("Invalid internal hash format"); |
73 |
| - } |
74 |
| - |
75 |
| - return { |
76 |
| - hash, |
77 |
| - salt, |
78 |
| - params: { |
79 |
| - algorithm, |
80 |
| - version: version as Argon2Version, |
81 |
| - mCost, |
82 |
| - tCost, |
83 |
| - pCost, |
84 |
| - }, |
85 |
| - }; |
86 |
| -} |
| 1 | +// import { |
| 2 | +// hash as hashArgon2, |
| 3 | +// Argon2Params, |
| 4 | +// Argon2Version, |
| 5 | +// Argon2Algorithm, |
| 6 | +// } from "https://deno.land/x/[email protected]/mod.ts"; |
| 7 | +// import { generateSalt } from "./common.ts"; |
| 8 | +// import { timingSafeEqual } from "https://deno.land/[email protected]/crypto/timing_safe_equal.ts"; |
| 9 | +// import base64 from "https://deno.land/x/[email protected]/src/base64.js"; |
| 10 | +// |
| 11 | +// |
| 12 | +// // OWASP recommended defaults: |
| 13 | +// // https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id |
| 14 | +// const argon2Defaults: Argon2Params = { |
| 15 | +// algorithm: "Argon2id", |
| 16 | +// version: 19, |
| 17 | +// tCost: 2, // 3 iterations |
| 18 | +// mCost: 19 * 1024, // 19MiB of memory |
| 19 | +// pCost: 1, // 1 thread |
| 20 | +// |
| 21 | +// secret: undefined, |
| 22 | +// outputLength: 32, |
| 23 | +// }; |
| 24 | +// |
| 25 | +// export function createHash(password: string) { |
| 26 | +// const salt = generateSalt(); |
| 27 | +// const passwordBuffer = new TextEncoder().encode(password); |
| 28 | +// const hash = hashArgon2(passwordBuffer, salt, argon2Defaults); |
| 29 | +// return packDigest(hash, salt, argon2Defaults); |
| 30 | +// } |
| 31 | +// |
| 32 | +// export function hashMatches(guess: string, digest: string) { |
| 33 | +// const { hash, salt, params } = unpackDigest(digest); |
| 34 | +// const guessBuffer = new TextEncoder().encode(guess); |
| 35 | +// |
| 36 | +// const hashGuess = hashArgon2(guessBuffer, salt, params); |
| 37 | +// |
| 38 | +// return timingSafeEqual(hashGuess, hash); |
| 39 | +// } |
| 40 | +// |
| 41 | +// function packDigest(hash: ArrayBuffer, salt: ArrayBuffer, params: Argon2Params) { |
| 42 | +// const headerPart = `$${params.algorithm.toLowerCase()}$v=${params.version}$`; |
| 43 | +// const paramPart = `m=${params.mCost},t=${params.tCost},p=${params.pCost}`; |
| 44 | +// |
| 45 | +// const saltBase64 = base64.fromArrayBuffer(salt); |
| 46 | +// const hashBase64 = base64.fromArrayBuffer(hash); |
| 47 | +// |
| 48 | +// return `${headerPart}${paramPart}$${saltBase64}$${hashBase64}`; |
| 49 | +// } |
| 50 | +// |
| 51 | +// const algorithmMap: Record<string, Argon2Algorithm> = { |
| 52 | +// "argon2i": "Argon2i", |
| 53 | +// "argon2d": "Argon2d", |
| 54 | +// "argon2id": "Argon2id", |
| 55 | +// }; |
| 56 | +// |
| 57 | +// function unpackDigest(digest: string): { hash: ArrayBuffer, salt: ArrayBuffer, params: Argon2Params } { |
| 58 | +// const [, algorithmName, versionStr, params, saltStr, hashStr] = digest.split("$"); |
| 59 | +// |
| 60 | +// const algorithm = algorithmMap[algorithmName]; |
| 61 | +// const version = parseInt(versionStr.match(/v=(\d+)/)![1]); |
| 62 | +// |
| 63 | +// const mCost = parseInt(params.match(/m=(\d+)/)![1]); |
| 64 | +// const tCost = parseInt(params.match(/t=(\d+)/)![1]); |
| 65 | +// const pCost = parseInt(params.match(/p=(\d+)/)![1]); |
| 66 | +// |
| 67 | +// const salt = base64.toArrayBuffer(saltStr); |
| 68 | +// const hash = base64.toArrayBuffer(hashStr); |
| 69 | +// |
| 70 | +// |
| 71 | +// if (!algorithm || !version || !mCost || !tCost || !pCost || !salt || !hash) { |
| 72 | +// throw new Error("Invalid internal hash format"); |
| 73 | +// } |
| 74 | +// |
| 75 | +// return { |
| 76 | +// hash, |
| 77 | +// salt, |
| 78 | +// params: { |
| 79 | +// algorithm, |
| 80 | +// version: version as Argon2Version, |
| 81 | +// mCost, |
| 82 | +// tCost, |
| 83 | +// pCost, |
| 84 | +// }, |
| 85 | +// }; |
| 86 | +// } |
0 commit comments