import { hashSync, compareSync } from 'bcryptjs';
import { createDecipheriv, createCipheriv } from 'node:crypto';

/**
 * 加密密码
 * @param password
 * @returns
 */
export function encryptPass(password: string) {
  return hashSync(password, 10);
}

/**
 * 比较密码
 */
export function comparePass(password: string, encryptPassword: string) {
  return compareSync(password, encryptPassword);
}

/**
 * aes加密
 * @param data
 * @param key
 */
export function aesEncrypt(data, key, iv) {
  const cipher = createCipheriv('aes-128-cbc', key, iv);
  let crypted = cipher.update(data, 'utf8', 'base64');
  crypted += cipher.final('base64');
  return crypted;
}

/**
 * aes解密
 * @param data
 * @param key
 */
export function aesDecrypt(data, key, iv) {
  const decipher = createDecipheriv('aes-128-cbc', key, iv);
  let decrypted = decipher.update(data, 'base64', 'utf8');
  decrypted += decipher.final('utf8');
  return decrypted;
}
