sh-tools 1.2.2 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/packages/api/index.js +85 -0
- package/packages/api/smSecret/base64.js +168 -0
- package/packages/api/smSecret/hex.js +92 -0
- package/packages/api/smSecret/index.js +13 -0
- package/packages/api/smSecret/sm3.js +276 -0
- package/packages/api/smSecret/util.js +92 -0
- package/packages/index.js +5 -0
- package/packages/utils/boolean.js +10 -0
- package/packages/utils/color.js +395 -0
- package/packages/utils/dom.js +111 -0
- package/packages/utils/index.js +25 -0
- package/packages/utils/number.js +24 -0
- package/packages/utils/object.js +1 -0
- package/packages/utils/other.js +311 -0
- package/packages/utils/pattern.js +43 -0
- package/packages/utils/string.js +227 -0
- package/packages/utils/time.js +1 -0
- package/packages/utils/validate.js +139 -0
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sh-tools",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"description": "基于xe-ajax和xe-utils二次封装",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "packages/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"lib": "vue-cli-service build --target lib packages/index.js --name sh-tools --dest lib"
|
|
8
8
|
},
|
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
"license": "ISC",
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"hot-formula-parser": "^4.0.0",
|
|
16
|
-
"view-ui-plus": "^1.3.
|
|
16
|
+
"view-ui-plus": "^1.3.14",
|
|
17
17
|
"xe-ajax": "^4.0.5",
|
|
18
|
-
"xe-utils": "^3.5.
|
|
18
|
+
"xe-utils": "^3.5.11"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@babel/core": "^7.12.16",
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import XEAjax from 'xe-ajax'
|
|
2
|
+
import { Notice } from 'view-ui-plus'
|
|
3
|
+
|
|
4
|
+
class HttpRequest {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.instance = XEAjax
|
|
7
|
+
this.options = options
|
|
8
|
+
this.setOptions(options)
|
|
9
|
+
this.interceptors()
|
|
10
|
+
}
|
|
11
|
+
setOptions(options) {
|
|
12
|
+
this.instance.setup(options)
|
|
13
|
+
}
|
|
14
|
+
// 继承类重写方法 用于处理个性化业务 预留
|
|
15
|
+
setRequest(request) {}
|
|
16
|
+
setResponse(response) {}
|
|
17
|
+
request(options) {
|
|
18
|
+
return this.instance.ajax(options)
|
|
19
|
+
}
|
|
20
|
+
get(url, params, options) {
|
|
21
|
+
return this.instance.get(url, params, options)
|
|
22
|
+
}
|
|
23
|
+
post(url, params, options) {
|
|
24
|
+
return this.instance.post(url, params, options)
|
|
25
|
+
}
|
|
26
|
+
put(url, params, options) {
|
|
27
|
+
return this.instance.put(url, params, options)
|
|
28
|
+
}
|
|
29
|
+
delete(url, params, options) {
|
|
30
|
+
return this.instance.delete(url, params, options)
|
|
31
|
+
}
|
|
32
|
+
patch(url, params, options) {
|
|
33
|
+
return this.instance.patch(url, params, options)
|
|
34
|
+
}
|
|
35
|
+
head(url, params, options) {
|
|
36
|
+
return this.instance.head(url, params, options)
|
|
37
|
+
}
|
|
38
|
+
jsonp(url, params, options) {
|
|
39
|
+
return this.instance.jsonp(url, params, options)
|
|
40
|
+
}
|
|
41
|
+
// 拦截器
|
|
42
|
+
interceptors() {
|
|
43
|
+
// 请求拦截
|
|
44
|
+
this.instance.interceptors.request.use((request, next) => {
|
|
45
|
+
this.setRequest(request)
|
|
46
|
+
next()
|
|
47
|
+
})
|
|
48
|
+
// 响应拦截
|
|
49
|
+
this.instance.interceptors.response.use((response, next) => {
|
|
50
|
+
this.responseNext(response, next)
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
// 错误处理
|
|
54
|
+
async responseNext(response, next) {
|
|
55
|
+
const { status, statusText, headers } = response
|
|
56
|
+
const duration = 5
|
|
57
|
+
let resBody = null
|
|
58
|
+
try {
|
|
59
|
+
resBody = await response.json()
|
|
60
|
+
} catch (e) {
|
|
61
|
+
resBody = response.__response || {}
|
|
62
|
+
}
|
|
63
|
+
let errorMsg = resBody.error || resBody.msg || resBody.message || resBody.result || statusText || '系统错误'
|
|
64
|
+
switch (status) {
|
|
65
|
+
case 0:
|
|
66
|
+
Notice.error({ title: '请求错误', desc: errorMsg, duration: duration })
|
|
67
|
+
break
|
|
68
|
+
case 200:
|
|
69
|
+
break
|
|
70
|
+
case 401:
|
|
71
|
+
Notice.error({ title: '系统提示 401', desc: '未授权,请重新登录', duration: duration })
|
|
72
|
+
return
|
|
73
|
+
case 504:
|
|
74
|
+
Notice.error({ title: '系统提示 网络超时', desc: errorMsg, duration: duration })
|
|
75
|
+
break
|
|
76
|
+
default:
|
|
77
|
+
Notice.error({ title: `系统提示 ${status}`, desc: errorMsg, duration: duration })
|
|
78
|
+
break
|
|
79
|
+
}
|
|
80
|
+
this.setResponse(resBody)
|
|
81
|
+
next({ status: 200, body: resBody })
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default HttpRequest
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
const isPlainObject = value => {
|
|
2
|
+
if (!value || typeof value !== 'object' || {}.toString.call(value) !== '[object Object]') {
|
|
3
|
+
return false
|
|
4
|
+
}
|
|
5
|
+
var proto = Object.getPrototypeOf(value)
|
|
6
|
+
if (proto === null) {
|
|
7
|
+
return true
|
|
8
|
+
}
|
|
9
|
+
var Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor
|
|
10
|
+
return typeof Ctor === 'function' && Ctor instanceof Ctor && Function.prototype.toString.call(Ctor) === Function.prototype.toString.call(Object)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const base64 = {
|
|
14
|
+
_keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
|
|
15
|
+
encode: function (input) {
|
|
16
|
+
if (!input && input !== 0) {
|
|
17
|
+
return input
|
|
18
|
+
}
|
|
19
|
+
var output = ''
|
|
20
|
+
var chr1, chr2, chr3, enc1, enc2, enc3, enc4
|
|
21
|
+
var i = 0
|
|
22
|
+
input = this._utf8_encode(input)
|
|
23
|
+
while (i < input.length) {
|
|
24
|
+
chr1 = input.charCodeAt(i++)
|
|
25
|
+
chr2 = input.charCodeAt(i++)
|
|
26
|
+
chr3 = input.charCodeAt(i++)
|
|
27
|
+
enc1 = chr1 >> 2
|
|
28
|
+
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
|
|
29
|
+
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
|
|
30
|
+
enc4 = chr3 & 63
|
|
31
|
+
if (isNaN(chr2)) {
|
|
32
|
+
enc3 = enc4 = 64
|
|
33
|
+
} else if (isNaN(chr3)) {
|
|
34
|
+
enc4 = 64
|
|
35
|
+
}
|
|
36
|
+
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4)
|
|
37
|
+
}
|
|
38
|
+
return output
|
|
39
|
+
},
|
|
40
|
+
decode: function (input) {
|
|
41
|
+
if (!input && input !== 0) {
|
|
42
|
+
return input
|
|
43
|
+
}
|
|
44
|
+
var output = ''
|
|
45
|
+
var chr1, chr2, chr3
|
|
46
|
+
var enc1, enc2, enc3, enc4
|
|
47
|
+
var i = 0
|
|
48
|
+
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
|
|
49
|
+
while (i < input.length) {
|
|
50
|
+
enc1 = this._keyStr.indexOf(input.charAt(i++))
|
|
51
|
+
enc2 = this._keyStr.indexOf(input.charAt(i++))
|
|
52
|
+
enc3 = this._keyStr.indexOf(input.charAt(i++))
|
|
53
|
+
enc4 = this._keyStr.indexOf(input.charAt(i++))
|
|
54
|
+
chr1 = (enc1 << 2) | (enc2 >> 4)
|
|
55
|
+
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
|
|
56
|
+
chr3 = ((enc3 & 3) << 6) | enc4
|
|
57
|
+
output = output + String.fromCharCode(chr1)
|
|
58
|
+
if (enc3 !== 64) {
|
|
59
|
+
output = output + String.fromCharCode(chr2)
|
|
60
|
+
}
|
|
61
|
+
if (enc4 !== 64) {
|
|
62
|
+
output = output + String.fromCharCode(chr3)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
output = this._utf8_decode(output)
|
|
66
|
+
return output
|
|
67
|
+
},
|
|
68
|
+
_utf8_encode: function (string) {
|
|
69
|
+
if (!string && string !== 0) {
|
|
70
|
+
return string
|
|
71
|
+
}
|
|
72
|
+
string = String(string)
|
|
73
|
+
// console.log(string);
|
|
74
|
+
string = string.replace(/\r\n/g, '\n')
|
|
75
|
+
var utftext = ''
|
|
76
|
+
for (var n = 0; n < string.length; n++) {
|
|
77
|
+
var c = string.charCodeAt(n)
|
|
78
|
+
if (c < 128) {
|
|
79
|
+
utftext += String.fromCharCode(c)
|
|
80
|
+
} else if (c > 127 && c < 2048) {
|
|
81
|
+
utftext += String.fromCharCode((c >> 6) | 192)
|
|
82
|
+
utftext += String.fromCharCode((c & 63) | 128)
|
|
83
|
+
} else {
|
|
84
|
+
utftext += String.fromCharCode((c >> 12) | 224)
|
|
85
|
+
utftext += String.fromCharCode(((c >> 6) & 63) | 128)
|
|
86
|
+
utftext += String.fromCharCode((c & 63) | 128)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return utftext
|
|
90
|
+
},
|
|
91
|
+
_utf8_decode: function (utftext) {
|
|
92
|
+
if (!utftext && utftext !== 0) {
|
|
93
|
+
return utftext
|
|
94
|
+
}
|
|
95
|
+
var stringtext = ''
|
|
96
|
+
var i = 0
|
|
97
|
+
var c = 0
|
|
98
|
+
var c1 = 0
|
|
99
|
+
var c2 = 0
|
|
100
|
+
while (i < utftext.length) {
|
|
101
|
+
c = utftext.charCodeAt(i)
|
|
102
|
+
if (c < 128) {
|
|
103
|
+
stringtext += String.fromCharCode(c)
|
|
104
|
+
i++
|
|
105
|
+
} else if (c > 191 && c < 224) {
|
|
106
|
+
c2 = utftext.charCodeAt(i + 1)
|
|
107
|
+
stringtext += String.fromCharCode(((c & 31) << 6) | (c2 & 63))
|
|
108
|
+
i += 2
|
|
109
|
+
} else {
|
|
110
|
+
c2 = utftext.charCodeAt(i + 1)
|
|
111
|
+
let c3 = utftext.charCodeAt(i + 2)
|
|
112
|
+
stringtext += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
|
|
113
|
+
i += 3
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return stringtext
|
|
117
|
+
},
|
|
118
|
+
/**
|
|
119
|
+
* 递归条用对参数进行编码
|
|
120
|
+
* @param parameters
|
|
121
|
+
* @returns {*}
|
|
122
|
+
*/
|
|
123
|
+
encrypt: function (parameters, isRecursive) {
|
|
124
|
+
if (!parameters) {
|
|
125
|
+
return parameters
|
|
126
|
+
}
|
|
127
|
+
var curParam
|
|
128
|
+
if (isRecursive) {
|
|
129
|
+
// 递归调用
|
|
130
|
+
curParam = parameters
|
|
131
|
+
} else {
|
|
132
|
+
curParam = JSON.parse(JSON.stringify(parameters))
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(curParam)) {
|
|
135
|
+
// 是数组
|
|
136
|
+
curParam.forEach((row, index) => {
|
|
137
|
+
if (!row && row !== 0) {
|
|
138
|
+
// 非空的不处理
|
|
139
|
+
return
|
|
140
|
+
}
|
|
141
|
+
if (isPlainObject(row) || Array.isArray(row)) {
|
|
142
|
+
// 对象和数组需要递归
|
|
143
|
+
this.encrypt(row, true)
|
|
144
|
+
} else {
|
|
145
|
+
// 其他直接加密
|
|
146
|
+
curParam[index] = this.encode(row)
|
|
147
|
+
}
|
|
148
|
+
})
|
|
149
|
+
} else if (isPlainObject(curParam)) {
|
|
150
|
+
// 对象
|
|
151
|
+
for (var key in curParam) {
|
|
152
|
+
if (!curParam[key] && curParam[key] !== 0) {
|
|
153
|
+
// 非空的不处理
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
if (isPlainObject(curParam[key]) || Array.isArray(curParam[key])) {
|
|
157
|
+
// 对象和数组需要递归
|
|
158
|
+
this.encrypt(curParam[key], true)
|
|
159
|
+
} else {
|
|
160
|
+
curParam[key] = this.encode(curParam[key])
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return curParam
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export default base64
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const hex = {
|
|
2
|
+
encode: function (b, pos, len) {
|
|
3
|
+
var hexCh = new Array(len * 2)
|
|
4
|
+
var hexCode = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
|
|
5
|
+
for (var i = pos, j = 0; i < len + pos; i++, j++) {
|
|
6
|
+
hexCh[j] = hexCode[(b[i] & 0xff) >> 4]
|
|
7
|
+
hexCh[++j] = hexCode[b[i] & 0x0f]
|
|
8
|
+
}
|
|
9
|
+
return hexCh.join('')
|
|
10
|
+
},
|
|
11
|
+
decode: function (hex) {
|
|
12
|
+
if (hex == null || hex === '') {
|
|
13
|
+
return null
|
|
14
|
+
}
|
|
15
|
+
if (hex.length % 2 !== 0) {
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
var ascLen = hex.length / 2
|
|
19
|
+
var hexCh = this.toCharCodeArray(hex)
|
|
20
|
+
var asc = new Array(ascLen)
|
|
21
|
+
for (var i = 0; i < ascLen; i++) {
|
|
22
|
+
if (hexCh[2 * i] >= 0x30 && hexCh[2 * i] <= 0x39) {
|
|
23
|
+
asc[i] = (hexCh[2 * i] - 0x30) << 4
|
|
24
|
+
} else if (hexCh[2 * i] >= 0x41 && hexCh[2 * i] <= 0x46) {
|
|
25
|
+
// A-F : 0x41-0x46
|
|
26
|
+
asc[i] = (hexCh[2 * i] - 0x41 + 10) << 4
|
|
27
|
+
} else if (hexCh[2 * i] >= 0x61 && hexCh[2 * i] <= 0x66) {
|
|
28
|
+
// a-f : 0x61-0x66
|
|
29
|
+
asc[i] = (hexCh[2 * i] - 0x61 + 10) << 4
|
|
30
|
+
} else {
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
if (hexCh[2 * i + 1] >= 0x30 && hexCh[2 * i + 1] <= 0x39) {
|
|
34
|
+
asc[i] = asc[i] | (hexCh[2 * i + 1] - 0x30)
|
|
35
|
+
} else if (hexCh[2 * i + 1] >= 0x41 && hexCh[2 * i + 1] <= 0x46) {
|
|
36
|
+
asc[i] = asc[i] | (hexCh[2 * i + 1] - 0x41 + 10)
|
|
37
|
+
} else if (hexCh[2 * i + 1] >= 0x61 && hexCh[2 * i + 1] <= 0x66) {
|
|
38
|
+
asc[i] = asc[i] | (hexCh[2 * i + 1] - 0x61 + 10)
|
|
39
|
+
} else {
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return asc
|
|
44
|
+
},
|
|
45
|
+
utf8StrToHex: function (utf8Str) {
|
|
46
|
+
var ens = encodeURIComponent(utf8Str)
|
|
47
|
+
var es = unescape(ens)
|
|
48
|
+
var esLen = es.length
|
|
49
|
+
// Convert
|
|
50
|
+
var words = []
|
|
51
|
+
for (var i = 0; i < esLen; i++) {
|
|
52
|
+
words[i] = es.charCodeAt(i).toString(16)
|
|
53
|
+
}
|
|
54
|
+
return words.join('')
|
|
55
|
+
},
|
|
56
|
+
utf8StrToBytes: function (utf8Str) {
|
|
57
|
+
var ens = encodeURIComponent(utf8Str)
|
|
58
|
+
var es = unescape(ens)
|
|
59
|
+
var esLen = es.length
|
|
60
|
+
// Convert
|
|
61
|
+
var words = []
|
|
62
|
+
for (var i = 0; i < esLen; i++) {
|
|
63
|
+
words[i] = es.charCodeAt(i)
|
|
64
|
+
}
|
|
65
|
+
return words
|
|
66
|
+
},
|
|
67
|
+
hexToUtf8Str: function (utf8Str) {
|
|
68
|
+
var utf8Byte = this.decode(utf8Str)
|
|
69
|
+
var latin1Chars = []
|
|
70
|
+
for (var i = 0; i < utf8Byte.length; i++) {
|
|
71
|
+
latin1Chars.push(String.fromCharCode(utf8Byte[i]))
|
|
72
|
+
}
|
|
73
|
+
return decodeURIComponent(escape(latin1Chars.join('')))
|
|
74
|
+
},
|
|
75
|
+
bytesToUtf8Str: function (bytesArray) {
|
|
76
|
+
var utf8Byte = bytesArray
|
|
77
|
+
var latin1Chars = []
|
|
78
|
+
for (var i = 0; i < utf8Byte.length; i++) {
|
|
79
|
+
latin1Chars.push(String.fromCharCode(utf8Byte[i]))
|
|
80
|
+
}
|
|
81
|
+
return decodeURIComponent(escape(latin1Chars.join('')))
|
|
82
|
+
},
|
|
83
|
+
toCharCodeArray: function (chs) {
|
|
84
|
+
var chArr = new Array(chs.length)
|
|
85
|
+
for (var i = 0; i < chs.length; i++) {
|
|
86
|
+
chArr[i] = chs.charCodeAt(i)
|
|
87
|
+
}
|
|
88
|
+
return chArr
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export default hex
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import hex from './hex'
|
|
2
|
+
import SM3Digest from './sm3'
|
|
3
|
+
|
|
4
|
+
function smSecret(inputtext) {
|
|
5
|
+
var dataBy = hex.utf8StrToBytes(inputtext)
|
|
6
|
+
var sm3 = new SM3Digest()
|
|
7
|
+
sm3.update(dataBy, 0, dataBy.length) // 数据很多的话,可以分多次update
|
|
8
|
+
var sm3Hash = sm3.doFinal() // 得到的数据是个byte数组
|
|
9
|
+
var sm3HashHex = hex.encode(sm3Hash, 0, sm3Hash.length) // 编码成16进制可见字符
|
|
10
|
+
return sm3HashHex
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default smSecret
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { arrayCopy, longToByte, intToByte, intArrayToByteArray, byteToInt, byteArrayToIntArray } from './util'
|
|
2
|
+
/*
|
|
3
|
+
* sm3-1.0.js
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2019 RuXing Liang
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* @name sm3-1.0.js
|
|
9
|
+
* @author RuXing Liang
|
|
10
|
+
* @version 1.0.0 (2019-04-16)
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
// 加密数据不能超过500M
|
|
14
|
+
|
|
15
|
+
function SM3Digest() {
|
|
16
|
+
this.ivByte = new Array(
|
|
17
|
+
0x73,
|
|
18
|
+
0x80,
|
|
19
|
+
0x16,
|
|
20
|
+
0x6f,
|
|
21
|
+
0x49,
|
|
22
|
+
0x14,
|
|
23
|
+
0xb2,
|
|
24
|
+
0xb9,
|
|
25
|
+
0x17,
|
|
26
|
+
0x24,
|
|
27
|
+
0x42,
|
|
28
|
+
0xd7,
|
|
29
|
+
0xda,
|
|
30
|
+
0x8a,
|
|
31
|
+
0x06,
|
|
32
|
+
0x00,
|
|
33
|
+
0xa9,
|
|
34
|
+
0x6f,
|
|
35
|
+
0x30,
|
|
36
|
+
0xbc,
|
|
37
|
+
0x16,
|
|
38
|
+
0x31,
|
|
39
|
+
0x38,
|
|
40
|
+
0xaa,
|
|
41
|
+
0xe3,
|
|
42
|
+
0x8d,
|
|
43
|
+
0xee,
|
|
44
|
+
0x4d,
|
|
45
|
+
0xb0,
|
|
46
|
+
0xfb,
|
|
47
|
+
0x0e,
|
|
48
|
+
0x4e
|
|
49
|
+
)
|
|
50
|
+
this.iv = byteArrayToIntArray(this.ivByte)
|
|
51
|
+
this.tj = new Array(64)
|
|
52
|
+
this.BLOCK_BYTE_LEN = 64
|
|
53
|
+
|
|
54
|
+
this.vbuf = new Array(8)
|
|
55
|
+
// 数据缓冲区
|
|
56
|
+
this.dataBuf = new Array(64)
|
|
57
|
+
// 缓冲区长度
|
|
58
|
+
this.dataBufLen = 0
|
|
59
|
+
// 缓冲区总长度
|
|
60
|
+
this.totalLen = 0 // 事实上需要long,后续需改进
|
|
61
|
+
|
|
62
|
+
for (var i = 0; i < 64; i++) {
|
|
63
|
+
if (i <= 15) {
|
|
64
|
+
this.tj[i] = 0x79cc4519
|
|
65
|
+
} else {
|
|
66
|
+
this.tj[i] = 0x7a879d8a
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
arrayCopy(this.iv, 0, this.vbuf, 0, this.vbuf.length)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
SM3Digest.prototype = {
|
|
73
|
+
ffj: function (x, y, z, i) {
|
|
74
|
+
var tmp
|
|
75
|
+
if (i <= 15) {
|
|
76
|
+
tmp = x ^ y ^ z
|
|
77
|
+
} else {
|
|
78
|
+
tmp = (x & y) | (x & z) | (y & z)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return tmp
|
|
82
|
+
},
|
|
83
|
+
ggj: function (x, y, z, i) {
|
|
84
|
+
var tmp = 0
|
|
85
|
+
if (i <= 15) {
|
|
86
|
+
tmp = x ^ y ^ z
|
|
87
|
+
} else {
|
|
88
|
+
tmp = (x & y) | (~x & z)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return tmp
|
|
92
|
+
},
|
|
93
|
+
p0: function (x) {
|
|
94
|
+
// 这里的公式是:对于一个二进制有n位的数字循环左移(循环右移)m位,
|
|
95
|
+
// 可以将此数字左移(无符号右移)m位的结果与此数字 无符号 右移(左移)n-m位的结果进行或操作。
|
|
96
|
+
return x ^ ((x << 9) | (x >>> (32 - 9))) ^ ((x << 17) | (x >>> (32 - 17)))
|
|
97
|
+
},
|
|
98
|
+
p1: function (x) {
|
|
99
|
+
// 这里的公式是:对于一个二进制有n位的数字循环左移(循环右移)m位,
|
|
100
|
+
// 可以将此数字左移(无符号右移)m位的结果与此数字 无符号 右移(左移)n-m位的结果进行或操作。
|
|
101
|
+
return x ^ ((x << 15) | (x >>> (32 - 15))) ^ ((x << 23) | (x >>> (32 - 23)))
|
|
102
|
+
},
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 循环左移
|
|
106
|
+
*/
|
|
107
|
+
cycleLeft: function (x, moveLen) {
|
|
108
|
+
return (x << moveLen) | (x >>> (32 - moveLen))
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 消息填充函数
|
|
113
|
+
* @param data
|
|
114
|
+
* @return
|
|
115
|
+
*/
|
|
116
|
+
padding: function (data) {
|
|
117
|
+
var k = 0
|
|
118
|
+
var len = data.length
|
|
119
|
+
var padding
|
|
120
|
+
|
|
121
|
+
k = 64 - ((len + 1 + 8) % 64)
|
|
122
|
+
if (k >= 64) {
|
|
123
|
+
k = 0
|
|
124
|
+
}
|
|
125
|
+
padding = new Array(k + 1 + len + 8)
|
|
126
|
+
padding[len] = 1 << 7
|
|
127
|
+
|
|
128
|
+
arrayCopy(data, 0, padding, 0, len)
|
|
129
|
+
arrayCopy(longToByte(this.totalLen << 3), 0, padding, len + k + 1, 8)
|
|
130
|
+
|
|
131
|
+
return padding
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 对数据进行分组迭代,每64个字节迭代一次
|
|
136
|
+
* <br>1、对消息进行分组,由于是int类型,则每16个分一组,对每一组再调用{@link #expand}进行拓展
|
|
137
|
+
* <br>2、使用上一轮的迭代结果V,调用{@link #cf}进行本轮迭代
|
|
138
|
+
* <br>3、最后一轮迭代结果复制进缓冲区vbuf
|
|
139
|
+
* @param message
|
|
140
|
+
*/
|
|
141
|
+
iterate: function (message) {
|
|
142
|
+
var len = message.length
|
|
143
|
+
var n = parseInt(len / 16)
|
|
144
|
+
var v, b
|
|
145
|
+
var ep
|
|
146
|
+
|
|
147
|
+
v = this.vbuf
|
|
148
|
+
b = new Array(16)
|
|
149
|
+
|
|
150
|
+
for (var i = 0; i < n; i++) {
|
|
151
|
+
arrayCopy(message, i * 16, b, 0, b.length)
|
|
152
|
+
ep = this.expand(b)
|
|
153
|
+
v = this.cf(v, ep[0], ep[1])
|
|
154
|
+
}
|
|
155
|
+
arrayCopy(v, 0, this.vbuf, 0, v.length)
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 消息数据拓展函数
|
|
160
|
+
* @param b
|
|
161
|
+
* @return
|
|
162
|
+
*/
|
|
163
|
+
expand: function (b) {
|
|
164
|
+
var w1 = new Array(68)
|
|
165
|
+
var w2 = new Array(64)
|
|
166
|
+
|
|
167
|
+
arrayCopy(b, 0, w1, 0, b.length)
|
|
168
|
+
|
|
169
|
+
for (let i = 16; i < w1.length; i++) {
|
|
170
|
+
w1[i] = this.p1(w1[i - 16] ^ w1[i - 9] ^ this.cycleLeft(w1[i - 3], 15)) ^ this.cycleLeft(w1[i - 13], 7) ^ w1[i - 6]
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
for (let i = 0; i < w2.length; i++) {
|
|
174
|
+
w2[i] = w1[i] ^ w1[i + 4]
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return new Array(w1, w2)
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* 迭代压缩函数
|
|
182
|
+
*
|
|
183
|
+
* @param v
|
|
184
|
+
* @param w1
|
|
185
|
+
* @param w2
|
|
186
|
+
* @return
|
|
187
|
+
*/
|
|
188
|
+
cf: function (v, w1, w2) {
|
|
189
|
+
var result
|
|
190
|
+
var a, b, c, d, e, f, g, h, ss1, ss2, tt1, tt2
|
|
191
|
+
a = v[0]
|
|
192
|
+
b = v[1]
|
|
193
|
+
c = v[2]
|
|
194
|
+
d = v[3]
|
|
195
|
+
e = v[4]
|
|
196
|
+
f = v[5]
|
|
197
|
+
g = v[6]
|
|
198
|
+
h = v[7]
|
|
199
|
+
|
|
200
|
+
for (var i = 0; i < 64; i++) {
|
|
201
|
+
ss1 = this.cycleLeft(this.cycleLeft(a, 12) + e + this.cycleLeft(this.tj[i], i), 7)
|
|
202
|
+
ss2 = ss1 ^ this.cycleLeft(a, 12)
|
|
203
|
+
tt1 = this.ffj(a, b, c, i) + d + ss2 + w2[i]
|
|
204
|
+
tt2 = this.ggj(e, f, g, i) + h + ss1 + w1[i]
|
|
205
|
+
d = c
|
|
206
|
+
c = this.cycleLeft(b, 9)
|
|
207
|
+
b = a
|
|
208
|
+
a = tt1
|
|
209
|
+
h = g
|
|
210
|
+
g = this.cycleLeft(f, 19)
|
|
211
|
+
f = e
|
|
212
|
+
e = this.p0(tt2)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
result = new Array(8)
|
|
216
|
+
result[0] = a ^ v[0]
|
|
217
|
+
result[1] = b ^ v[1]
|
|
218
|
+
result[2] = c ^ v[2]
|
|
219
|
+
result[3] = d ^ v[3]
|
|
220
|
+
result[4] = e ^ v[4]
|
|
221
|
+
result[5] = f ^ v[5]
|
|
222
|
+
result[6] = g ^ v[6]
|
|
223
|
+
result[7] = h ^ v[7]
|
|
224
|
+
|
|
225
|
+
return result
|
|
226
|
+
},
|
|
227
|
+
|
|
228
|
+
digest: function (data) {
|
|
229
|
+
var mac
|
|
230
|
+
|
|
231
|
+
var padding = this.padding(data)
|
|
232
|
+
var paddingInt = byteArrayToIntArray(padding)
|
|
233
|
+
this.iterate(paddingInt)
|
|
234
|
+
var macInt = this.vbuf
|
|
235
|
+
mac = intArrayToByteArray(macInt)
|
|
236
|
+
return mac
|
|
237
|
+
},
|
|
238
|
+
|
|
239
|
+
update: function (data, pos, len) {
|
|
240
|
+
var loop = parseInt((len + this.dataBufLen) / 64) // 向下取整
|
|
241
|
+
this.totalLen += len
|
|
242
|
+
|
|
243
|
+
if (len + this.dataBufLen < this.BLOCK_BYTE_LEN) {
|
|
244
|
+
arrayCopy(data, 0, this.dataBuf, this.dataBufLen, len)
|
|
245
|
+
this.dataBufLen = len + this.dataBufLen
|
|
246
|
+
} else {
|
|
247
|
+
var dataInt
|
|
248
|
+
arrayCopy(data, 0, this.dataBuf, this.dataBufLen, this.BLOCK_BYTE_LEN - this.dataBufLen)
|
|
249
|
+
dataInt = byteArrayToIntArray(this.dataBuf)
|
|
250
|
+
this.iterate(dataInt)
|
|
251
|
+
for (var i = 1; i < loop; i++) {
|
|
252
|
+
arrayCopy(data, i * this.BLOCK_BYTE_LEN - this.dataBufLen, this.dataBuf, 0, this.BLOCK_BYTE_LEN)
|
|
253
|
+
dataInt = byteArrayToIntArray(this.dataBuf)
|
|
254
|
+
this.iterate(dataInt)
|
|
255
|
+
}
|
|
256
|
+
arrayCopy(data, loop * this.BLOCK_BYTE_LEN - this.dataBufLen, this.dataBuf, 0, len - (loop * this.BLOCK_BYTE_LEN - this.dataBufLen))
|
|
257
|
+
this.dataBufLen = len - (loop * this.BLOCK_BYTE_LEN - this.dataBufLen)
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
doFinal: function () {
|
|
262
|
+
var mac
|
|
263
|
+
var finalData = new Array(this.dataBufLen)
|
|
264
|
+
|
|
265
|
+
arrayCopy(this.dataBuf, 0, finalData, 0, this.dataBufLen)
|
|
266
|
+
// 对不足64字节的数据进行填充
|
|
267
|
+
var paddingArr = this.padding(finalData)
|
|
268
|
+
var paddingInt = byteArrayToIntArray(paddingArr)
|
|
269
|
+
this.iterate(paddingInt)
|
|
270
|
+
var macInt = this.vbuf
|
|
271
|
+
mac = intArrayToByteArray(macInt)
|
|
272
|
+
return mac
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export default SM3Digest
|