UNPKG

@xtsai/xai-utils

Version:

The xai-utils is an openai nodejs sdk compatible extension library.

44 lines 1.34 kB
import bcrypt from 'bcrypt'; import { objectKeySorted } from '../sortable'; const DEFAULT_ROUNDS = 10; const DEFAULT_PW = 'Tsailab'; export class BcryptHelper { static async encryptPassword(password = DEFAULT_PW, rounds = DEFAULT_ROUNDS) { const salt = await bcrypt.genSalt(rounds); const enpw = await bcrypt.hash(password, salt); return enpw; } /** * sortable json * @example * ```text * uid: x * json {b:2,a:1} * `${uid}${JSON.stringify(sortable:json)}` * stringify : x{\"a\":1,\"b\":2,\"uid\":\"x\"} * ``` * @param uid * @param json * @returns hash string */ static async keccak256(uid, json) { const salt = await bcrypt.genSalt(5); const merged = { ...(json ?? {}), uid }; const sortedJson = objectKeySorted(merged); const text = `${uid}${JSON.stringify(sortedJson)}`; const hash = await bcrypt.hash(text, salt); return hash; } /** * * @param password * @param enpassword * @returns boolean */ static async validPassword(password, enpassword = '') { if (!enpassword?.length && password === enpassword) return true; return await bcrypt.compare(password, enpassword); } } //# sourceMappingURL=bcrypt.helper.js.map