feat: move to noble hash scrypt impl

This commit is contained in:
Bereket Engida
2024-10-14 20:05:47 +03:00
parent 054ffc4a1a
commit 256c8ca5ed
2 changed files with 8 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
export function constantTimeEqual( export function constantTimeEqual(
a: ArrayBuffer, a: ArrayBuffer | Uint8Array,
b: ArrayBuffer | Uint8Array, b: ArrayBuffer | Uint8Array,
): boolean { ): boolean {
const aBuffer = new Uint8Array(a); const aBuffer = new Uint8Array(a);

View File

@@ -1,7 +1,6 @@
import { scrypt } from "node:crypto";
import { decodeHex, encodeHex } from "oslo/encoding"; import { decodeHex, encodeHex } from "oslo/encoding";
import { constantTimeEqual } from "./buffer"; import { constantTimeEqual } from "./buffer";
import { scryptAsync } from "@noble/hashes/scrypt";
const config = { const config = {
N: 16384, N: 16384,
r: 16, r: 16,
@@ -9,28 +8,12 @@ const config = {
dkLen: 64, dkLen: 64,
}; };
async function generateKey( async function generateKey(password: string, salt: string) {
password: string, return await scryptAsync(password.normalize("NFKC"), salt, {
salt: string, N: config.N,
): Promise<ArrayBuffer> { p: config.p,
return await new Promise<ArrayBuffer>((resolve, reject) => { r: config.r,
scrypt( maxmem: 128 * config.N * config.r * 2,
password.normalize("NFKC"),
salt!,
config.dkLen,
{
N: config.N,
p: config.p,
r: config.r,
// errors when 128 * N * r > `maxmem` (approximately)
maxmem: 128 * config.N * config.r * 2,
},
(err, buff) => {
if (err) return reject(err);
// @ts-ignore
return resolve(buff);
},
);
}); });
} }