UNPKG

authenzify

Version:

server to manage authentication authorization of users and more

46 lines (37 loc) 1.17 kB
import { promisify } from 'util' import { randomBytes, scrypt } from 'crypto' const scryptPromisify = promisify(scrypt) const keyLength = 64 const baseFormat = 'hex' export const getSalt = (length) => { return randomBytes(length) } export const getSaltHex = (length) => { return getSalt(length).toString(baseFormat) } const getEncryptedBuffer = async ({ expression, salt }) => { const encryptedExpression = await scryptPromisify(expression, salt, keyLength) return encryptedExpression } export const encrypt = async ({ expression, salt, passwordPrivateKey }) => { const encryptedExpression = await getEncryptedBuffer({ expression, salt: `${salt}${passwordPrivateKey ? passwordPrivateKey : ''}`, }) return encryptedExpression.toString(baseFormat) } export const doesPasswordMatch = async ({ password, encryptedPassword, salt, passwordPrivateKey, }) => { const encryptedCurrentPassword = await getEncryptedBuffer({ expression: password, salt: `${salt}${passwordPrivateKey ? passwordPrivateKey : ''}`, }) const isMatch = encryptedCurrentPassword.equals( Buffer.from(encryptedPassword, baseFormat), ) return isMatch }