mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-10 12:27:44 +00:00
feat: move to noble hash scrypt impl
This commit is contained in:
@@ -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);
|
||||||
|
|||||||
@@ -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);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user