24 lines
802 B
JavaScript
24 lines
802 B
JavaScript
|
|
import crypto from 'node:crypto';
|
|||
|
|
import { Algorithm, hashRawSync } from '@node-rs/argon2';
|
|||
|
|
|
|||
|
|
/** Node < 24 无 crypto.argon2Sync,Memind user-auth 登录会崩溃。 */
|
|||
|
|
export function ensureArgon2Sync() {
|
|||
|
|
if (typeof crypto.argon2Sync === 'function') return;
|
|||
|
|
|
|||
|
|
crypto.argon2Sync = (algorithm, options = {}) => {
|
|||
|
|
if (algorithm !== 'argon2id') {
|
|||
|
|
throw new Error(`unsupported argon2 algorithm: ${algorithm}`);
|
|||
|
|
}
|
|||
|
|
const message = options.message ?? '';
|
|||
|
|
const nonce = options.nonce ?? Buffer.alloc(0);
|
|||
|
|
return hashRawSync(String(message), {
|
|||
|
|
algorithm: Algorithm.Argon2id,
|
|||
|
|
salt: nonce,
|
|||
|
|
parallelism: options.parallelism ?? 1,
|
|||
|
|
outputLen: options.tagLength ?? 32,
|
|||
|
|
memoryCost: options.memory ?? 65536,
|
|||
|
|
timeCost: options.passes ?? 3,
|
|||
|
|
});
|
|||
|
|
};
|
|||
|
|
}
|