-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
230 lines (227 loc) · 6.87 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict'
const deepCopy = sa => JSON.parse(JSON.stringify(sa))
const rc = function (t) {
if (t % 255 === 0) return 1
let R = [1, 0, 0, 0, 0, 0, 0, 0]
for (let i = 1; i <= t % 255; i++) {
R = [0, ...R]
R[0] = R[0] ^ R[8]
R[4] ^= R[8]
R[5] ^= R[8]
R[6] ^= R[8]
// truncn(x) -> [x[0], x[n-1]]
R = R.slice(0, 8)
}
return R[0]
}
const RC = [
'1000000000000000000000000000000000000000000000000000000000000000',
'0100000100000001000000000000000000000000000000000000000000000000',
'0101000100000001000000000000000000000000000000000000000000000001',
'0000000000000001000000000000000100000000000000000000000000000001',
'1101000100000001000000000000000000000000000000000000000000000000',
'1000000000000000000000000000000100000000000000000000000000000000',
'1000000100000001000000000000000100000000000000000000000000000001',
'1001000000000001000000000000000000000000000000000000000000000001',
'0101000100000000000000000000000000000000000000000000000000000000',
'0001000100000000000000000000000000000000000000000000000000000000',
'1001000000000001000000000000000100000000000000000000000000000000',
'0101000000000000000000000000000100000000000000000000000000000000',
'1101000100000001000000000000000100000000000000000000000000000000',
'1101000100000000000000000000000000000000000000000000000000000001',
'1001000100000001000000000000000000000000000000000000000000000001',
'1100000000000001000000000000000000000000000000000000000000000001',
'0100000000000001000000000000000000000000000000000000000000000001',
'0000000100000000000000000000000000000000000000000000000000000001',
'0101000000000001000000000000000000000000000000000000000000000000',
'0101000000000000000000000000000100000000000000000000000000000001',
'1000000100000001000000000000000100000000000000000000000000000001',
'0000000100000001000000000000000000000000000000000000000000000001',
'1000000000000000000000000000000100000000000000000000000000000000',
'0001000000000001000000000000000100000000000000000000000000000001'
]
function mod (a, b) {
if (a > 0) return a % b
while (a < 0) {
a += b
}
return a
}
function StrArrXOR (a, b) {
if (a.length !== b.length) throw new Error('无法对不同长度的两串字符进行异或')
let res = ''
for (let i = 0; i < a.length; i++) {
res += a[i] ^ b[i]
}
return res
}
function bufferXOR (a, b) {
if (a.length !== b.length) throw new Error('无法对不同长度的两个Buffer进行异或')
for (let i = 0; i < a.length; i++) {
a[i] ^= b[i]
}
return a
}
function bin2hex4 (data) {
let res = ''
let count = 0
while (data[count]) {
let num = 0
let arr = data.slice(count, count + 4).split('').reverse()
arr.forEach((v, i) => {
num += +v * 2 ** (3 - i)
})
res += num.toString(16)
count += 4
}
return res
}
function bin2hex8 (data) {
let res = ''
data.match(/.{8}/g).forEach((byte, index) => {
let [a, b] = [...byte.match(/.{4}/g)]
res += '' + parseInt(b.split('').reverse().join(''), 2).toString(16) + parseInt(a.split('').reverse().join(''), 2).toString(16)
})
return res
}
// 注意:2进制字符串转16进制时,如果用8位为单位转换的话,需要注意00000000或者0000xxxx等类型,会丢失高四位的0,比如00000001会被转为1而不是01
function bin2byte (data) {
let arr = []
for (let i = 1; ;i++) {
let byte = data.slice((i - 1) * 8, i * 8)
if (!byte) break
arr.push(byte.split('').reverse())
}
return arr
}
function arr2string (arr) {
let s = ''
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 5; y++) {
s += arr[y][x].join('')
}
}
return s
}
function sa2log (sa) {
let temp = arr2string(sa)
let t = bin2byte(temp)
let res = ''
for (let i = 0; i < t.length; i++) {
let byte = t[i]
res += bin2hex4(byte.slice(0, 4).reverse().join('')) + bin2hex4(byte.slice(4).reverse().join('')) + ((i + 1) % 16 === 0 ? '\n' : ' ')
}
return res
}
function trans2BitString (data) {
let res = ''
for (let u of data) {
res += u.toString(2).padStart(8, '0').split('').reverse().join('')
}
return res
}
module.exports = {
rc,
mod,
sa2log,
StrArrXOR,
bufferXOR,
bin2hex4,
bin2hex8,
bin2byte,
arr2string,
trans2BitString,
// map1 means θ
map1: function (sa = this.sa) {
let C = [[], [], [], [], []]; let D = [[], [], [], [], []]
for (let x = 0; x < 5; x++) {
for (let z = 0; z < this.w; z++) {
C[x][z] = sa[x][0][z] ^ sa[x][1][z] ^ sa[x][2][z] ^ sa[x][3][z] ^ sa[x][4][z]
}
}
for (let x = 0; x < 5; x++) {
for (let z = 0; z < this.w; z++) {
D[x][z] = C[mod((x - 1), 5)][z] ^ C[mod((x + 1), 5)][mod((z - 1), this.w)]
}
}
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 5; y++) {
for (let z = 0; z >= 0 && z < this.w; z++) {
sa[x][y][z] = sa[x][y][z] ^ D[x][z]
}
}
}
return sa
},
// map2 means ρ
map2: function (sa = this.sa) {
// 巨坑。。。需要深拷贝sa!
// step 1
let newSa = deepCopy(sa)
// step 2
let [x, y] = [1, 0]
// step 3
for (let t = 0; t < 24; t++) {
// a
for (let z = 0; z < this.w; z++) {
let temp = mod(z - (t + 1) * (t + 2) / 2, this.w)
newSa[x][y][z] = sa[x][y][temp]
}
// b
[x, y] = [y, mod((2 * x + 3 * y), 5)]
}
return newSa
},
// map3 means π
map3: function (sa = this.sa) {
let newSa = deepCopy(sa)
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 5; y++) {
for (let z = 0; z < this.w; z++) {
newSa[x][y][z] = sa[(x + 3 * y) % 5][x][z]
}
}
}
// let testres = sa2log(newSa)
return newSa
},
// map4 means χ
map4: function (sa = this.sa) {
let newSa = deepCopy(sa)
for (let x = 0; x < 5; x++) {
for (let y = 0; y < 5; y++) {
for (let z = 0; z < this.w; z++) {
newSa[x][y][z] = sa[x][y][z] ^ ((sa[(x + 1) % 5][y][z] ^ 1) * sa[(x + 2) % 5][y][z])
}
}
}
// let testres = sa2log(newSa)
return newSa
},
// map5 means ι
map5: function (sa = this.sa, ir) {
for (let z = 0; z < this.w; z++) {
sa[0][0][z] ^= RC[ir][z]
}
return sa
},
/**
* positive int x
* non-negative int m
* @returns {string} P that m + len(P) = k·x
*/
pad101: function (x, m) {
if (x <= 0 || m < 0) throw new Error(`Pad Error: Invalid input x: ${x} or m: ${m}`)
// j = (-m-2) mod x
let j = mod(-m - 2, x)
return '1' + '0'.repeat(j) + '1'
}
}
/*
计算RC
const rcRes = '1000000010110001111010000111111110010000101001111101010101110000011000101011001100101111110111100110111011100101010010100010010110100011001110011110001101100001000101110101111011011111000011010011010110110101000001001110110010010011000000111010010001110001'
let RC = '0'.repeat(this.w).split('')
for (let j = 0; j <= +this.l; j++) {
RC[2 ** j - 1] = rcRes[j + 7 * ir]
}
*/