123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import cryptojs from 'crypto-js'
- var codecutil = {}
- export default codecutil //全局导出对象
- //MD5(Message-Digest Algorithm),信息摘要算法
- //一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致
- codecutil.md5 = function (data) {
- return cryptojs.MD5(data).toString()
- }
- //SHA1(Secure Hash Algorithm 1),安全散列算法,
- //SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数
- codecutil.sha1 = function (data) {
- return cryptojs.SHA1(data).toString()
- }
- //SHA256(Secure Hash Algorithm 1),安全散列算法,
- //对于任意长度的消息,SHA256都会产生一个256位的哈希值,称作消息摘要。这个摘要相当于是个长度为32个字节的数组,共256位,通常由一个长度为64的十六进制字符串来表示
- codecutil.sha256 = function (data) {
- return cryptojs.SHA256(data).toString()
- }
- //解析密钥
- codecutil.aesDeKey = function (key) {
- return cryptojs.enc.Utf8.parse(key)
- }
- //aes加密
- codecutil.aesEncode = function (data, key) {
- if (data && key) {
- let aesKey = cryptojs.enc.Utf8.parse(key) //将密钥按照utf8格式化
- let content = cryptojs.enc.Utf8.parse(data)//将明文按照utf8格式化
- //加密后已经是base64编码格式的数据
- const ciphertext = cryptojs.AES.encrypt(content, aesKey, { iv: aesKey, mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 }).toString()
- return ciphertext
- } else {
- return ''
- }
- }
- //aes解密
- codecutil.aesDecode = function (data, key) {
- if (data && key) {
- var ciphertext = codecutil.base64Decode(data)
- // var bytes = cryptojs.AES.decrypt(ciphertext, key, { mode: cryptojs.mode.ECB, padding: cryptojs.pad.Pkcs7 })
- var bytes = cryptojs.AES.decrypt(ciphertext, key, { mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 })
- var plaintext = bytes.toString(cryptojs.enc.Utf8)
- return plaintext
- } else {
- return ''
- }
- }
- //base64加密
- codecutil.base64Encode = function (data) {
- if (data) {
- var bytes = cryptojs.enc.Utf8.parse(data)
- var plaintext = bytes.toString(cryptojs.enc.Base64)
- plaintext = plaintext.toString()
- // plaintext = plaintext.replace(/\//g, '_')
- // plaintext = plaintext.replace(/\+/g, '-')
- // plaintext = plaintext.replace(/=/g, '')
- return plaintext
- } else {
- return ''
- }
- }
- //base64解密
- codecutil.base64Decode = function (data) {
- if (data) {
- var ciphertext = data.toString()
- ciphertext = ciphertext.replace(/_/g, '/')
- ciphertext = ciphertext.replace(/-/g, '+')
- for (var i = 0; i < 4 - ciphertext.length % 4; i++) {
- ciphertext = ciphertext + '='
- }
- var bytes = cryptojs.enc.Base64.parse(ciphertext)
- var plaintext = bytes.toString(cryptojs.enc.Utf8)
- return plaintext
- } else {
- return ''
- }
- }
- codecutil.hexEncode = function (data) {
- if (data) {
- var bytes = cryptojs.enc.Utf8.parse(data)
- var plaintext = bytes.toString(cryptojs.enc.Hex)
- return plaintext
- } else {
- return ''
- }
- }
- codecutil.hexDecode = function (data) {
- if (data) {
- var bytes = cryptojs.enc.Hex.parse(data)
- var plaintext = bytes.toString(cryptojs.enc.Utf8)
- return plaintext
- } else {
- return ''
- }
- }
|