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(
a: ArrayBuffer,
a: ArrayBuffer | Uint8Array,
b: ArrayBuffer | Uint8Array,
): boolean {
const aBuffer = new Uint8Array(a);

View File

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