Created
September 15, 2021 09:28
-
-
Save Tnifey/f9a1f4b7d7109fc721617db5edace445 to your computer and use it in GitHub Desktop.
argon2.lib.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { argon2id, argon2Verify } from "https://cdn.skypack.dev/hash-wasm@v4.9.0"; | |
/** | |
* deno 1.13.1 (release, x86_64-unknown-linux-gnu) | |
* v8 9.3.345.11 | |
* typescript 4.3.5 | |
*/ | |
export type HashOptions = { | |
salt?: String | Uint8Array; | |
parallelism?: number; | |
iterations?: number; | |
memorySize?: number; | |
hashLength?: number; | |
outputType?: "encoded"; | |
}; | |
export async function hash(plain: string, options?: HashOptions) { | |
if (typeof options?.salt === "string") { | |
const enc = new TextEncoder(); | |
options.salt = enc.encode(options.salt); | |
} | |
return await argon2id({ | |
password: plain, | |
salt: options?.salt || window.crypto.getRandomValues(new Uint8Array(16)), | |
parallelism: options?.parallelism || 1, | |
iterations: options?.iterations || 256, | |
memorySize: options?.memorySize || 512, | |
hashLength: options?.hashLength || 32, | |
outputType: options?.outputType || "encoded", | |
}); | |
} | |
export async function verify(hash: string, plain: string) { | |
try { | |
return await argon2Verify({ hash, password: plain }); | |
} catch (error) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment