ab-helpers
Version:
A collection of helper functions for various tasks
27 lines (21 loc) • 771 B
JavaScript
const crypto = require("crypto");
const bcrypt = require("bcryptjs");
function encrypt(string, algorithm, secretKey) {
const cipher = crypto.createCipher(algorithm, secretKey);
let encrypted = cipher.update(string, "utf8", "hex");
encrypted += cipher.final("hex");
return encrypted;
}
function decrypt(encrypted, algorithm, secretKey) {
const decipher = crypto.createDecipher(algorithm, secretKey);
let decrypted = decipher.update(encrypted, "hex", "utf8");
decrypted += decipher.final("utf8");
return decrypted;
}
async function hash(password, saltOrRounds) {
return bcrypt.compare(password, saltOrRounds);
}
async function compare(password, hash) {
return bcrypt.compare(password, hash);
}
module.exports = { encrypt, decrypt, hash, compare };