@xtsai/xai-utils
Version:
The xai-utils is an openai nodejs sdk compatible extension library.
48 lines • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BcryptHelper = void 0;
const bcrypt_1 = require("bcrypt");
const sortable_1 = require("../sortable");
const DEFAULT_ROUNDS = 10;
const DEFAULT_PW = 'Tsailab';
class BcryptHelper {
static async encryptPassword(password = DEFAULT_PW, rounds = DEFAULT_ROUNDS) {
const salt = await bcrypt_1.default.genSalt(rounds);
const enpw = await bcrypt_1.default.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_1.default.genSalt(5);
const merged = { ...(json ?? {}), uid };
const sortedJson = (0, sortable_1.objectKeySorted)(merged);
const text = `${uid}${JSON.stringify(sortedJson)}`;
const hash = await bcrypt_1.default.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_1.default.compare(password, enpassword);
}
}
exports.BcryptHelper = BcryptHelper;
//# sourceMappingURL=bcrypt.helper.js.map