UNPKG

lotus-sdk

Version:

Central repository for several classes of tools for integrating with, and building for, the Lotusia ecosystem

26 lines (25 loc) 937 B
import { pbkdf2 as noblePbkdf2 } from '@noble/hashes/pbkdf2'; import { sha512 } from '@noble/hashes/sha512'; export function pbkdf2(key, salt, iterations, dkLen) { const hLen = 64; if (dkLen > (Math.pow(2, 32) - 1) * hLen) { throw new Error('Requested key length too long'); } if (typeof key !== 'string' && !Buffer.isBuffer(key)) { throw new TypeError('key must a string or Buffer'); } if (typeof salt !== 'string' && !Buffer.isBuffer(salt)) { throw new TypeError('salt must a string or Buffer'); } const keyBytes = typeof key === 'string' ? new TextEncoder().encode(key) : new Uint8Array(key); const saltBytes = typeof salt === 'string' ? new TextEncoder().encode(salt) : new Uint8Array(salt); const result = noblePbkdf2(sha512, keyBytes, saltBytes, { c: iterations, dkLen: dkLen, }); return Buffer.from(result); }