【Vue】前端Crypto-js加密库md5加密转成二进制/十六进制/Base64格式
CryptoJS 提供了md5加密的功能,并且可以方便地将加密结果转换为二进制、十六进制、Base64格式。
解决方案1:
import CryptoJS from "crypto-js";
let message = "Hello, World!";
let messageMD5Binary = CryptoJS.MD5(message).toString(CryptoJS.enc.Binary);
let messageMD5Hex = CryptoJS.MD5(message).toString(CryptoJS.enc.Hex);
let messageMD5Base64 = CryptoJS.MD5(message).toString(CryptoJS.enc.Base64);
console.log(messageMD5Binary);
console.log(messageMD5Hex);
console.log(messageMD5Base64);
解决方案2:
import md5 from 'js-md5';
let message = "Hello, World!";
let messageMD5Binary = md5(message, { output: "binary" });
let messageMD5Hex = md5(message, { output: "hex" });
let messageMD5Base64 = md5(message, { output: "base64" });
console.log(messageMD5Binary);
console.log(messageMD5Hex);
console.log(messageMD5Base64);
解决方案3:
import crypto from 'crypto';
let message = "Hello, World!";
let messageMD5Binary = crypto.createHash('md5').update(message).digest('binary');
let messageMD5Hex = crypto.createHash('md5').update(message).digest('hex');
let messageMD5Base64 = crypto.createHash('md5').update(message).digest('base64');
console.log(messageMD5Binary);
console.log(messageMD5Hex);
console.log(messageMD5Base64);
以上三种方案都可以实现将明文转换为MD5加密的二进制、十六进制、Base64格式。其中,解决方案1和解决方案2使用的是CryptoJS库,解决方案3使用的是Node.js的crypto模块。这三种方案可以根据你的环境和需求选择使用。
评论已关闭