123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- const cryptojs = require('crypto-js');
- var codecutil = {}
- export default codecutil
- codecutil.md5 = function (data) {
- return cryptojs.MD5(data).toString()
- }
- codecutil.sha1 = function (data) {
- return cryptojs.SHA1(data).toString()
- }
- codecutil.sha256 = function (data) {
- return cryptojs.SHA256(data).toString()
- }
- codecutil.aesDeKey = function (key) {
- return cryptojs.enc.Utf8.parse(key)
- }
- codecutil.aesEncode = function (data, key) {
- if (data && key) {
- let aesKey = cryptojs.enc.Utf8.parse(key)
- let content = cryptojs.enc.Utf8.parse(data)
-
- const ciphertext = cryptojs.AES.encrypt(content, aesKey, { iv: aesKey, mode: cryptojs.mode.CBC, padding: cryptojs.pad.Pkcs7 }).toString()
- return ciphertext
- } else {
- return ''
- }
- }
- codecutil.aesDecode = function (data, key) {
- if (data && key) {
- var ciphertext = codecutil.base64Decode(data)
-
- 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 ''
- }
- }
- codecutil.base64Encode = function (data) {
- if (data) {
- var bytes = cryptojs.enc.Utf8.parse(data)
- var plaintext = bytes.toString(cryptojs.enc.Base64)
- plaintext = plaintext.toString()
-
-
-
- return plaintext
- } else {
- return ''
- }
- }
- 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 ''
- }
- }
|