UNPKG

cmx-tool

Version:

308 lines (298 loc) 10.1 kB
var sm3 = require('sm3'); require('./sm4'); const sm2 = require('sm-crypto').sm2; const redis = require('redis'); var uuid = require('uuid'); var SM4KEY = '36069cea7409915799ff84ee2467c659'; var SM2PUBKEY = '04af572e0dd16934514817d0584485df85ba7b59f0edf9595683f1e06ec74c3c6d90501a0a6d8eeefca8d1083d70635a73a35f3ab55ad65b910e7fcdddeaff0e2e'; var SM2PRIVATEKEY = '5a8ea8ada51e4fd9ae12eba8ea692e6445d991f244039dcf90b38d3379b6ac2e'; var Base64 = { // 转码表 table: [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' ], UTF16ToUTF8: function (str) { var res = [], len = str.length; for (var i = 0; i < len; i++) { var code = str.charCodeAt(i); var byte1, byte2; if (code > 0x0000 && code <= 0x007F) { // 单字节,这里并不考虑0x0000,因为它是空字节 // U+00000000 – U+0000007F 0xxxxxxx res.push(str.charAt(i)); } else if (code >= 0x0080 && code <= 0x07FF) { // 双字节 // U+00000080 – U+000007FF 110xxxxx 10xxxxxx // 110xxxxx byte1 = 0xC0 | ((code >> 6) & 0x1F); // 10xxxxxx byte2 = 0x80 | (code & 0x3F); res.push( String.fromCharCode(byte1), String.fromCharCode(byte2) ); } else if (code >= 0x0800 && code <= 0xFFFF) { // 三字节 // U+00000800 – U+0000FFFF 1110xxxx 10xxxxxx 10xxxxxx // 1110xxxx byte1 = 0xE0 | ((code >> 12) & 0x0F); // 10xxxxxx byte2 = 0x80 | ((code >> 6) & 0x3F); // 10xxxxxx var byte3 = 0x80 | (code & 0x3F); res.push( String.fromCharCode(byte1), String.fromCharCode(byte2), String.fromCharCode(byte3) ); } else if (code >= 0x00010000 && code <= 0x001FFFFF) { // 四字节 // U+00010000 – U+001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (code >= 0x00200000 && code <= 0x03FFFFFF) { // 五字节 // U+00200000 – U+03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (code >= 0x04000000 && code <= 0x7FFFFFFF)*/ { // 六字节 // U+04000000 – U+7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return res.join(''); }, UTF8ToUTF16: function (str) { var res = [], len = str.length; for (var i = 0; i < len; i++) { var code = str.charCodeAt(i); // 对第一个字节进行判断 var code2, byte1, byte2, utf16; if (((code >> 7) & 0xFF) == 0x0) { // 单字节 // 0xxxxxxx res.push(str.charAt(i)); } else if (((code >> 5) & 0xFF) == 0x6) { // 双字节 // 110xxxxx 10xxxxxx code2 = str.charCodeAt(++i); byte1 = (code & 0x1F) << 6; byte2 = code2 & 0x3F; utf16 = byte1 | byte2; res.push(String.fromCharCode(utf16)); } else if (((code >> 4) & 0xFF) == 0xE) { // 三字节 // 1110xxxx 10xxxxxx 10xxxxxx code2 = str.charCodeAt(++i); code3 = str.charCodeAt(++i); byte1 = (code << 4) | ((code2 >> 2) & 0x0F); byte2 = ((code2 & 0x03) << 6) | (code3 & 0x3F); utf16 = ((byte1 & 0x00FF) << 8) | byte2; res.push(String.fromCharCode(utf16)); } else if (((code >> 3) & 0xFF) == 0x1E) { // 四字节 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx } else if (((code >> 2) & 0xFF) == 0x3E) { // 五字节 // 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } else /** if (((code >> 1) & 0xFF) == 0x7E)*/ { // 六字节 // 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx } } return res.join(''); }, encode: function (str) { if (!str) { return ''; } var utf8 = this.UTF16ToUTF8(str); // 转成UTF8 var i = 0; // 遍历索引 var len = utf8.length; var res = []; while (i < len) { var c1 = utf8.charCodeAt(i++) & 0xFF; res.push(this.table[c1 >> 2]); // 需要补2个= if (i == len) { res.push(this.table[(c1 & 0x3) << 4]); res.push('=='); break; } var c2 = utf8.charCodeAt(i++); // 需要补1个= if (i == len) { res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]); res.push(this.table[(c2 & 0x0F) << 2]); res.push('='); break; } var c3 = utf8.charCodeAt(i++); res.push(this.table[((c1 & 0x3) << 4) | ((c2 >> 4) & 0x0F)]); res.push(this.table[((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6)]); res.push(this.table[c3 & 0x3F]); } return res.join(''); }, decode: function (str) { if (!str) { return ''; } var len = str.length; var i = 0; var res = []; while (i < len) { code1 = this.table.indexOf(str.charAt(i++)); code2 = this.table.indexOf(str.charAt(i++)); code3 = this.table.indexOf(str.charAt(i++)); code4 = this.table.indexOf(str.charAt(i++)); c1 = (code1 << 2) | (code2 >> 4); c2 = ((code2 & 0xF) << 4) | (code3 >> 2); c3 = ((code3 & 0x3) << 6) | code4; res.push(String.fromCharCode(c1)); if (code3 != 64) { res.push(String.fromCharCode(c2)); } if (code4 != 64) { res.push(String.fromCharCode(c3)); } } return this.UTF8ToUTF16(res.join('')); } }; module.exports = function (config) { console.log(config) exports.sm3Encrypt = function (_str) {//sm3加密 return sm3(str); } exports.md5Encrypt = function (_str) {//md5加密 return md5(str); } exports.sm4Encrypt = function (_str) {//sm4加密 var ciphertext = sm4_encode_cbc(_str, SM4KEY); var result = ""; for (var i = 0; i < ciphertext.length; i++) { hextext = ciphertext[i].toString(16); result += hextext.length == 16 ? hextext : "0".repeat(8 - hextext.length) + hextext; } //var _data = execSync(process.cwd() + '/engine/SM4/Sm4Encrypt.exe "' + _str + '"').toString(); return result; } exports.SM4Decrypt = function (_str) {//sm4解密 var text = new Array(); for (var i = 0; i < _str.length; i += 8) { text.push(parseInt(_str.slice(i, i + 8), 16)) } var plaintext = sm4_decode_cbc(text, SM4KEY); //var _data = execSync(process.cwd() + '/engine/SM4/Sm4Decrypt.exe "' + _str + '"'); //return iconv.decode(_data, 'GBK'); return plaintext; } exports.SM2Encrypt = function (_str, _SM2PUBKEY = SM2PUBKEY, _cipherMode = 1) {//sm2加密 return sm2.doEncrypt(_str, _SM2PUBKEY, _cipherMode); } exports.SM2Decrypt = function (_str, _SM2PRIVATEKEY = SM2PRIVATEKEY, _cipherMode = 1) {//sm2解密 return sm2.doDecrypt(_str, _SM2PRIVATEKEY, _cipherMode); } exports.trim = function (_str) { try { return _str.replace(/^\s+|\s+$/g, ""); } catch (k) { return _str; } }; //数组存在查找indexOf //数组位置查找 //判断是否为数字 exports.IsNum = function (s) { if (s != null && s != "" && typeof (s) != "undefined") { return !isNaN(s); } return false; } //是否为NULL exports.IsNull = exports.isNull = function (value) { // 需要判断的内容 if (value == null || typeof (value) == 'undefined' || value === undefined) { return true; } return false; } //是否为空(包括空字符串) exports.IsEmpty = exports.isEmpty = function (strings) { if (!IsNull(strings)) { // 先判断是否为null,返回true ,判断是否为空字符串,返回true if ((strings + '').replace(/(^\s*)|(\s*$)/g, '').length === 0) { return true; } } else { return true; } // 不为空返回false return false; } //向缓存加数据(仅WEB) exports.addCache = function(_key,_value){ const redisClient = redis.createClient(config.redisPort, config.redisHost, { password: "ZCM_Redis4" }); redisClient.on("error", function (err) { console.log('redis连接失败'); return false; }); redisClient.set(_key,_value,function(err,reply){ if(err) return false; else return true; }) } //从缓存取数据(仅WEB) exports.getCache = function(_key){ const redisClient = redis.createClient(config.redisPort, config.redisHost, { password: "ZCM_Redis4" }); redisClient.on("error", function (err) { console.log('redis连接失败'); return false; }); redisClient.get(_key,function(err,data){ if(err) return false; else return data; }) } //清除缓存(仅WEB) exports.deleteCache = function(_key){ const redisClient = redis.createClient(config.redisPort, config.redisHost, { password: "ZCM_Redis4" }); redisClient.on("error", function (err) { console.log('redis连接失败'); return false; }); redisClient.del(_key,function(err,data){ if(err) return false; else return true; }) } //base64编码 exports.safeEncodeBase64 = function (str) { str = Base64.encode(str); var string = str.replace(/\+/g, "^").replace(/\//g, "_").replace(/=/g, "*"); return string; } //base64解码 exports.safeDecodeBase64 = function (str) { var string = str.replace(/\^/g, "+").replace(/\_/g, "/").replace(/\*/g, "="); return Base64.decode(string); } //获取url参数 //生成UUID exports.generateUUID = function(_type = 'v1'){ if(_type === 'v4'){ return uuid.v4(); }else return uuid.v1(); } return exports; }