Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ecfc188879 | ||
|
|
6e9fe6489f | ||
|
|
cc863049e6 |
2
index.js
2
index.js
@@ -2,14 +2,12 @@ const Common = require('./src/utils/Common')
|
||||
const Storage = require('./src/utils/Storage')
|
||||
const crypto = require('./src/utils/Crypto')
|
||||
const GlobalConfig = require('./src/utils/GlobalConfig')
|
||||
const SM4 = require('./src/utils/SM4')
|
||||
const HttpUtil = require('./src/https/HttpUtil')
|
||||
|
||||
module.exports = {
|
||||
HttpUtil,
|
||||
Common,
|
||||
Storage,
|
||||
SM4,
|
||||
crypto,
|
||||
GlobalConfig,
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@tiesheng/npm-tool",
|
||||
"version": "1.0.20",
|
||||
"version": "1.0.27-zjut",
|
||||
"description": "npm tool package",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -100,7 +100,7 @@ async function req(url, options) {
|
||||
if (res.code === 200) {
|
||||
let data = res.data;
|
||||
if (res.encrypt) {
|
||||
data = crypto.decrypt(data);
|
||||
data = crypto.d(data);
|
||||
data = Common.parseJSON(data, data);
|
||||
}
|
||||
resolve({data, recordsTotal: res.recordsTotal})
|
||||
@@ -136,7 +136,7 @@ async function post(url, data = {}, options = {}) {
|
||||
|
||||
let newData = {}
|
||||
if (Storage.getEncryptBody()) {
|
||||
newData.encryptData = crypto.encrypt(JSON.stringify(data));
|
||||
newData.encryptData = crypto.e(JSON.stringify(data));
|
||||
} else {
|
||||
newData = {...data};
|
||||
}
|
||||
|
||||
@@ -1,32 +1,20 @@
|
||||
const base64js = require("base64-js");
|
||||
const SM4 = require("./SM4");
|
||||
const GlobalConfig = require("../utils/GlobalConfig");
|
||||
const R = require("./R");
|
||||
|
||||
class Crypto {
|
||||
|
||||
constructor() {
|
||||
this.sm4 = new SM4({
|
||||
keyBuffer: base64js.toByteArray(GlobalConfig.getConfig().base64Key),
|
||||
mode: "ecb",
|
||||
cipherType: 'base64'
|
||||
this.r = new R({
|
||||
padding: [0xA5, 0x3C, 0x7F, 0x12, 0xB8, 0x4D, 0x91, 0xE3, 0x2A, 0x6B, 0xF0, 0x1E, 0x85, 0xC9, 0x34, 0x67],
|
||||
rounds: [0xC2, 0x65, 0xD8, 0xFB, 0x4A, 0xD3, 0x7B, 0x05, 0xCD, 0xF0, 0x6E, 0xE4, 0x5D, 0x6D, 0x43, 0x6D]
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密数据
|
||||
* @param content
|
||||
*/
|
||||
encrypt(content) {
|
||||
return this.sm4.encrypt(content);
|
||||
e(content) {
|
||||
return this.r.e(content);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解密数据
|
||||
* @param base64
|
||||
*/
|
||||
decrypt(base64) {
|
||||
return this.sm4.decrypt(base64);
|
||||
d(base64) {
|
||||
return this.r.d(base64);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
/**
|
||||
* 默认配置
|
||||
* @type {{onHttpError: defaultConfig.onHttpError, prefix: string, base64Key: string}}
|
||||
* @type {{onHttpError: defaultConfig.onHttpError, prefix: string, k: string}}
|
||||
*/
|
||||
|
||||
const defaultConfig = {
|
||||
base64Key: "WmdUzPJXbngVNiaSsQrihg==",
|
||||
prefix: "",
|
||||
storePrefix: () => {
|
||||
return "";
|
||||
|
||||
@@ -37,141 +37,66 @@ const FK = Uint32Array.from([
|
||||
])
|
||||
|
||||
class Crypt {
|
||||
/**
|
||||
* Converts a JS string to an UTF-8 uint8array.
|
||||
*
|
||||
* @static
|
||||
* @param {String} str 16-bit unicode string.
|
||||
* @return {Uint8Array} UTF-8 Uint8Array.
|
||||
* @memberof Crypt
|
||||
*/
|
||||
static stringToArrayBufferInUtf8 (str) {
|
||||
// if not browser env, then require node.js's util. otherwise just use window's
|
||||
const TextEncoder = (typeof window === 'undefined') ? require('util').TextEncoder : window.TextEncoder
|
||||
// always utf-8
|
||||
let encoder = new TextEncoder()
|
||||
return encoder.encode(str)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an UTF-8 uint8array to a JS string.
|
||||
*
|
||||
* @static
|
||||
* @param {Uint8Array} strBuffer UTF-8 Uint8Array.
|
||||
* @return {String} 16-bit unicode string.
|
||||
* @memberof Crypt
|
||||
*/
|
||||
static utf8ArrayBufferToString (strBuffer) {
|
||||
// if not browser env, then require node.js's util. otherwise just use window's
|
||||
const TextDecoder = (typeof window === 'undefined') ? require('util').TextDecoder : window.TextDecoder
|
||||
let decoder = new TextDecoder('utf-8')
|
||||
return decoder.decode(strBuffer)
|
||||
}
|
||||
|
||||
/**
|
||||
* crypt a utf8 byteArray to base64 string
|
||||
*
|
||||
* @static
|
||||
* @param {Uint8Array} strBuffer UTF-8 Uint8Array.
|
||||
* @returns {String} base64 str
|
||||
* @memberof Crypt
|
||||
*/
|
||||
static arrayBufferToBase64 (strBuffer) {
|
||||
return base64js.fromByteArray(strBuffer)
|
||||
}
|
||||
|
||||
/**
|
||||
* crypt base64 stringa to utf8 byteArray
|
||||
*
|
||||
* @static
|
||||
* @param {String} base64 str
|
||||
* @returns {Uint8Array} strBuffer UTF-8 Uint8Array.
|
||||
* @memberof Crypt
|
||||
*/
|
||||
static base64ToArrayBuffer (base64) {
|
||||
return base64js.toByteArray(base64)
|
||||
}
|
||||
}
|
||||
|
||||
class SM4 {
|
||||
/**
|
||||
* Creates an instance of SM4.
|
||||
* @param {Object} config
|
||||
* @memberof SM4
|
||||
*/
|
||||
class R {
|
||||
constructor(config) {
|
||||
if (config.keyBuffer.length !== 16) {
|
||||
const v = (() => {
|
||||
const x = new Uint8Array(16);
|
||||
const p = config.padding || [];
|
||||
const r = config.rounds || [];
|
||||
for (let i = 0; i < 16; i++) {
|
||||
x[i] = (p[i] || 0) ^ (r[i] || 0);
|
||||
}
|
||||
return x;
|
||||
})();
|
||||
|
||||
if (v.length !== 16) {
|
||||
throw new Error('key should be a 16 bytes string')
|
||||
}
|
||||
/**
|
||||
* key should be 16 bytes string
|
||||
* @member {Uint8Array} key
|
||||
*/
|
||||
this.key = config.keyBuffer
|
||||
/**
|
||||
* iv also should be 16 bytes string
|
||||
* @member {Uint8Array} iv
|
||||
*/
|
||||
this.key = v;
|
||||
let ivBuffer = new Uint8Array(0)
|
||||
if (config.iv !== undefined && config.iv !== null) {
|
||||
// need iv
|
||||
ivBuffer = Crypt.stringToArrayBufferInUtf8(config.iv)
|
||||
if (ivBuffer.length !== 16) {
|
||||
throw new Error('iv should be a 16 bytes string')
|
||||
}
|
||||
}
|
||||
this.iv = ivBuffer
|
||||
/**
|
||||
* sm4's encrypt mode
|
||||
* @member {Enum} mode
|
||||
*/
|
||||
this.mode = 'cbc'
|
||||
if (['cbc', 'ecb'].indexOf(config.mode) >= 0) {
|
||||
// set encrypt mode. default is cbc
|
||||
this.mode = config.mode
|
||||
}
|
||||
/**
|
||||
* sm4's cipher data type
|
||||
* @member {Enum} outType
|
||||
*/
|
||||
this.mode = 'ecb'
|
||||
this.cipherType = 'base64'
|
||||
if (['base64', 'text'].indexOf(config.outType) >= 0) {
|
||||
// set encrypt mode. default is cbc
|
||||
this.cipherType = config.outType
|
||||
}
|
||||
/**
|
||||
* sm4's encrypt round key array
|
||||
* @member {Uint32Array} encryptRoundKeys
|
||||
*/
|
||||
this.encryptRoundKeys = new Uint32Array(32)
|
||||
// spawn 32 round keys
|
||||
this.spawnEncryptRoundKeys()
|
||||
|
||||
/**
|
||||
* sm4's decrypt round key array
|
||||
* @member {Uint32Array} encryptRoundKeys
|
||||
*/
|
||||
this.decryptRoundKeys = Uint32Array.from(this.encryptRoundKeys)
|
||||
this.decryptRoundKeys.reverse()
|
||||
}
|
||||
|
||||
/**
|
||||
* general sm4 encrypt/decrypt algorithm for a 16 bytes block using roundKey
|
||||
*
|
||||
* @param {Uint32Array} blockData
|
||||
* @param {Uint32Array} roundKeys
|
||||
* @return {Uint32Array} return a 16 bytes cipher block
|
||||
* @memberof SM4
|
||||
*/
|
||||
doBlockCrypt(blockData, roundKeys) {
|
||||
let xBlock = new Uint32Array(36)
|
||||
xBlock.set(blockData, 0)
|
||||
// loop to process 32 rounds crypt
|
||||
for (let i = 0; i < 32; i++) {
|
||||
xBlock[i + 4] = xBlock[i] ^ this.tTransform1(xBlock[i + 1] ^ xBlock[i + 2] ^ xBlock[i + 3] ^ roundKeys[i])
|
||||
}
|
||||
let yBlock = new Uint32Array(4)
|
||||
// reverse last 4 xBlock member
|
||||
yBlock[0] = xBlock[35]
|
||||
yBlock[1] = xBlock[34]
|
||||
yBlock[2] = xBlock[33]
|
||||
@@ -179,111 +104,51 @@ class SM4 {
|
||||
return yBlock
|
||||
}
|
||||
|
||||
/**
|
||||
* spawn round key array for encrypt. reverse this key array when decrypt.
|
||||
* every round key's length is 32 bytes.
|
||||
* there are 32 round keys.
|
||||
* @return {Uint32Array}
|
||||
* @memberof SM4
|
||||
*/
|
||||
spawnEncryptRoundKeys() {
|
||||
// extract mk in key
|
||||
let mk = new Uint32Array(4)
|
||||
mk[0] = this.key[0] << 24 | this.key[1] << 16 | this.key[2] << 8 | this.key[3]
|
||||
mk[1] = this.key[4] << 24 | this.key[5] << 16 | this.key[6] << 8 | this.key[7]
|
||||
mk[2] = this.key[8] << 24 | this.key[9] << 16 | this.key[10] << 8 | this.key[11]
|
||||
mk[3] = this.key[12] << 24 | this.key[13] << 16 | this.key[14] << 8 | this.key[15]
|
||||
// calculate the K array
|
||||
let k = new Uint32Array(36)
|
||||
k[0] = mk[0] ^ FK[0]
|
||||
k[1] = mk[1] ^ FK[1]
|
||||
k[2] = mk[2] ^ FK[2]
|
||||
k[3] = mk[3] ^ FK[3]
|
||||
// loop to spawn 32 round keys
|
||||
for (let i = 0; i < 32; i++) {
|
||||
k[i + 4] = k[i] ^ this.tTransform2(k[i + 1] ^ k[i + 2] ^ k[i + 3] ^ CK[i])
|
||||
this.encryptRoundKeys[i] = k[i + 4]
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* left rotate x by y bits
|
||||
*
|
||||
* @param {*} x
|
||||
* @param {Number} y
|
||||
* @returns
|
||||
* @memberof SM4
|
||||
*/
|
||||
rotateLeft(x, y) {
|
||||
return x << y | x >>> (32 - y)
|
||||
}
|
||||
|
||||
/**
|
||||
* L transform function for encrypt
|
||||
*
|
||||
* @param {Uint32Number} b
|
||||
* @returns {Uint32Number}
|
||||
* @memberof SM4
|
||||
*/
|
||||
linearTransform1(b) {
|
||||
return b ^ this.rotateLeft(b, 2) ^ this.rotateLeft(b, 10) ^ this.rotateLeft(b, 18) ^ this.rotateLeft(b, 24)
|
||||
}
|
||||
|
||||
/**
|
||||
* L' transform function for key expand
|
||||
*
|
||||
* @param {Uint32Number} b
|
||||
* @returns {Uint32Number}
|
||||
* @memberof SM4
|
||||
*/
|
||||
linearTransform2(b) {
|
||||
return b ^ this.rotateLeft(b, 13) ^ this.rotateLeft(b, 23)
|
||||
}
|
||||
|
||||
/**
|
||||
* τ transform function
|
||||
*
|
||||
* @param {Uint32Number} a
|
||||
* @returns {Uint32Number}
|
||||
* @memberof SM4
|
||||
*/
|
||||
tauTransform(a) {
|
||||
return Sbox[a >>> 24 & 0xff] << 24 | Sbox[a >>> 16 & 0xff] << 16 | Sbox[a >>> 8 & 0xff] << 8 | Sbox[a & 0xff]
|
||||
}
|
||||
|
||||
/**
|
||||
* mix replacement T transform for encrypt
|
||||
*
|
||||
* @param {Uint32Number} z
|
||||
* @returns {Uint32Number}
|
||||
* @memberof SM4
|
||||
*/
|
||||
tTransform1(z) {
|
||||
let b = this.tauTransform(z)
|
||||
let c = this.linearTransform1(b)
|
||||
return c
|
||||
}
|
||||
|
||||
/**
|
||||
* mix replacement T transform for key expand
|
||||
*
|
||||
* @param {Uint32Number} z
|
||||
* @returns {Uint32Number}
|
||||
* @memberof SM4
|
||||
*/
|
||||
tTransform2(z) {
|
||||
let b = this.tauTransform(z)
|
||||
let c = this.linearTransform2(b)
|
||||
return c
|
||||
}
|
||||
|
||||
/**
|
||||
* padding the array length to multiple of BLOCK
|
||||
*
|
||||
* @param {ByteArray} originalBuffer
|
||||
* @returns {ByteArray}
|
||||
* @memberof SM4
|
||||
*/
|
||||
padding(originalBuffer) {
|
||||
if (originalBuffer === null) {
|
||||
return null
|
||||
@@ -295,13 +160,6 @@ class SM4 {
|
||||
return paddedBuffer
|
||||
}
|
||||
|
||||
/**
|
||||
* depadding the byte array to its original length
|
||||
*
|
||||
* @param {ByteArray} paddedBuffer
|
||||
* @returns {ByteArray}
|
||||
* @memberof SM4
|
||||
*/
|
||||
dePadding(paddedBuffer) {
|
||||
if (paddedBuffer === null) {
|
||||
return null
|
||||
@@ -311,16 +169,8 @@ class SM4 {
|
||||
return originalBuffer
|
||||
}
|
||||
|
||||
/**
|
||||
* exctract uint32 array block from uint8 array
|
||||
*
|
||||
* @param {Uint8Array} uint8Array
|
||||
* @param {Number} baseIndex
|
||||
* @returns {Uint32Array}
|
||||
* @memberof SM4
|
||||
*/
|
||||
uint8ToUint32Block(uint8Array, baseIndex = 0) {
|
||||
let block = new Uint32Array(4)// make Uint8Array to Uint32Array block
|
||||
let block = new Uint32Array(4)
|
||||
block[0] = uint8Array[baseIndex] << 24 | uint8Array[baseIndex + 1] << 16 | uint8Array[baseIndex + 2] << 8 | uint8Array[baseIndex + 3]
|
||||
block[1] = uint8Array[baseIndex + 4] << 24 | uint8Array[baseIndex + 5] << 16 | uint8Array[baseIndex + 6] << 8 | uint8Array[baseIndex + 7]
|
||||
block[2] = uint8Array[baseIndex + 8] << 24 | uint8Array[baseIndex + 9] << 16 | uint8Array[baseIndex + 10] << 8 | uint8Array[baseIndex + 11]
|
||||
@@ -328,46 +178,31 @@ class SM4 {
|
||||
return block
|
||||
}
|
||||
|
||||
/**
|
||||
* encrypt the string plaintext
|
||||
*
|
||||
* @param {String} plaintext
|
||||
* @memberof SM4
|
||||
* @return {String} ciphertext
|
||||
*/
|
||||
encrypt(plaintext) {
|
||||
e(plaintext) {
|
||||
let plainByteArray = Crypt.stringToArrayBufferInUtf8(plaintext)
|
||||
let padded = this.padding(plainByteArray)
|
||||
let blockTimes = padded.length / UINT8_BLOCK
|
||||
let outArray = new Uint8Array(padded.length)
|
||||
if (this.mode === 'cbc') {
|
||||
// CBC mode
|
||||
if (this.iv === null || this.iv.length !== 16) {
|
||||
throw new Error('iv error')
|
||||
}
|
||||
// init chain with iv (transform to uint32 block)
|
||||
let chainBlock = this.uint8ToUint32Block(this.iv)
|
||||
for (let i = 0; i < blockTimes; i++) {
|
||||
// extract the 16 bytes block data for this round to encrypt
|
||||
let roundIndex = i * UINT8_BLOCK
|
||||
let block = this.uint8ToUint32Block(padded, roundIndex)
|
||||
// xor the chain block
|
||||
chainBlock[0] = chainBlock[0] ^ block[0]
|
||||
chainBlock[1] = chainBlock[1] ^ block[1]
|
||||
chainBlock[2] = chainBlock[2] ^ block[2]
|
||||
chainBlock[3] = chainBlock[3] ^ block[3]
|
||||
// use chain block to crypt
|
||||
let cipherBlock = this.doBlockCrypt(chainBlock, this.encryptRoundKeys)
|
||||
// make the cipher block be part of next chain block
|
||||
chainBlock = cipherBlock
|
||||
for (let l = 0; l < UINT8_BLOCK; l++) {
|
||||
outArray[roundIndex + l] = cipherBlock[parseInt(l / 4)] >> ((3 - l) % 4 * 8) & 0xff
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// this will be ECB mode
|
||||
for (let i = 0; i < blockTimes; i++) {
|
||||
// extract the 16 bytes block data for this round to encrypt
|
||||
let roundIndex = i * UINT8_BLOCK
|
||||
let block = this.uint8ToUint32Block(padded, roundIndex)
|
||||
let cipherBlock = this.doBlockCrypt(block, this.encryptRoundKeys)
|
||||
@@ -376,80 +211,54 @@ class SM4 {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// cipher array to string
|
||||
if (this.cipherType === 'base64') {
|
||||
return Crypt.arrayBufferToBase64(outArray)
|
||||
} else {
|
||||
// text
|
||||
return Crypt.utf8ArrayBufferToString(outArray)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* decrypt the string ciphertext
|
||||
*
|
||||
* @param {String} ciphertext
|
||||
* @memberof SM4
|
||||
*/
|
||||
decrypt(ciphertext) {
|
||||
// get cipher byte array
|
||||
d(ciphertext) {
|
||||
let cipherByteArray = new Uint8Array()
|
||||
if (this.cipherType === 'base64') {
|
||||
// cipher is base64 string
|
||||
cipherByteArray = Crypt.base64ToArrayBuffer(ciphertext)
|
||||
} else {
|
||||
// cipher is text
|
||||
cipherByteArray = Crypt.stringToArrayBufferInUtf8(ciphertext)
|
||||
}
|
||||
let blockTimes = cipherByteArray.length / UINT8_BLOCK
|
||||
let outArray = new Uint8Array(cipherByteArray.length)
|
||||
// decrypt the ciphertext by block
|
||||
if (this.mode === 'cbc') {
|
||||
// todo CBC mode
|
||||
if (this.iv === null || this.iv.length !== 16) {
|
||||
throw new Error('iv error')
|
||||
}
|
||||
// init chain with iv (transform to uint32 block)
|
||||
let chainBlock = this.uint8ToUint32Block(this.iv)
|
||||
for (let i = 0; i < blockTimes; i++) {
|
||||
// extract the 16 bytes block data for this round to encrypt
|
||||
let roundIndex = i * UINT8_BLOCK
|
||||
// make Uint8Array to Uint32Array block
|
||||
let block = this.uint8ToUint32Block(cipherByteArray, roundIndex)
|
||||
// reverse the round keys to decrypt
|
||||
let plainBlockBeforeXor = this.doBlockCrypt(block, this.decryptRoundKeys)
|
||||
// xor the chain block
|
||||
let plainBlock = new Uint32Array(4)
|
||||
plainBlock[0] = chainBlock[0] ^ plainBlockBeforeXor[0]
|
||||
plainBlock[1] = chainBlock[1] ^ plainBlockBeforeXor[1]
|
||||
plainBlock[2] = chainBlock[2] ^ plainBlockBeforeXor[2]
|
||||
plainBlock[3] = chainBlock[3] ^ plainBlockBeforeXor[3]
|
||||
// make the cipher block be part of next chain block
|
||||
chainBlock = block
|
||||
for (let l = 0; l < UINT8_BLOCK; l++) {
|
||||
outArray[roundIndex + l] = plainBlock[parseInt(l / 4)] >> ((3 - l) % 4 * 8) & 0xff
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// ECB mode
|
||||
for (let i = 0; i < blockTimes; i++) {
|
||||
// extract the 16 bytes block data for this round to encrypt
|
||||
let roundIndex = i * UINT8_BLOCK
|
||||
// make Uint8Array to Uint32Array block
|
||||
let block = this.uint8ToUint32Block(cipherByteArray, roundIndex)
|
||||
// reverse the round keys to decrypt
|
||||
let plainBlock = this.doBlockCrypt(block, this.decryptRoundKeys)
|
||||
for (let l = 0; l < UINT8_BLOCK; l++) {
|
||||
outArray[roundIndex + l] = plainBlock[parseInt(l / 4)] >> ((3 - l) % 4 * 8) & 0xff
|
||||
}
|
||||
}
|
||||
}
|
||||
// depadding the decrypted data
|
||||
let depaddedPlaintext = this.dePadding(outArray)
|
||||
// transform data to utf8 string
|
||||
return Crypt.utf8ArrayBufferToString(depaddedPlaintext)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SM4
|
||||
module.exports = R
|
||||
Reference in New Issue
Block a user