8
8
let s: save_cpo = &cpo
9
9
set cpo &vim
10
10
11
+ function ! s: _vital_created (module) abort
12
+ let a: module .name = ' SHA1'
13
+ let a: module .hash_length = s: sha1hashsize * 8 " 160
14
+ endfunction
15
+
11
16
function ! s: _vital_loaded (V) abort
12
17
let s: V = a: V
13
- let s: bitwise = s: V .import (' Bitwise' )
18
+ let s: Bitwise = s: V .import (' Bitwise' )
19
+ let s: ByteArray = s: V .import (' Data.List.Byte' )
14
20
endfunction
15
21
16
22
function ! s: _vital_depends () abort
17
- return [' Bitwise' ]
23
+ return [' Bitwise' , ' Data.List.Byte ' ]
18
24
endfunction
19
25
20
26
function ! s: sum (data) abort
21
- let bytes = s: _str2bytes (a: data )
27
+ let bytes = s: ByteArray . from_string (a: data )
22
28
return s: sum_raw (bytes)
23
29
endfunction
24
30
25
31
function ! s: sum_raw (bytes) abort
26
- return s: _bytes2binstr (s: digest_raw (a: bytes ))
32
+ return s: ByteArray . to_hexstring (s: digest_raw (a: bytes ))
27
33
endfunction
28
34
29
35
function ! s: digest (data) abort
30
- let bytes = s: _str2bytes (a: data )
36
+ let bytes = s: ByteArray . from_string (a: data )
31
37
return s: digest_raw (bytes)
32
38
endfunction
33
39
@@ -77,7 +83,7 @@ let s:sha1context = {
77
83
\}
78
84
79
85
function ! s: _sha1circular_shift (bits, word) abort
80
- return s: bitwise . or ( s: bitwise . lshift32 ( a: word , a: bits), s: bitwise . rshift32 ( a: word , 32 - a: bits ) )
86
+ return s: Bitwise . rotate32l ( a: word , a: bits )
81
87
endfunction
82
88
83
89
function ! s: sha1context .init () dict abort
@@ -112,10 +118,10 @@ function! s:sha1context.result(digest) dict abort
112
118
endif
113
119
114
120
for i in range (s: sha1hashsize )
115
- let a: digest [i ] = s: _uint8 (
116
- \ s: bitwise .rshift32 (
117
- \ self .intermediatehash[s: bitwise .rshift32 (i , 2 )],
118
- \ 8 * (3 - s: bitwise .and (i , 0x03 ))
121
+ let a: digest [i ] = s: Bitwise . uint8 (
122
+ \ s: Bitwise .rshift32 (
123
+ \ self .intermediatehash[s: Bitwise .rshift32 (i , 2 )],
124
+ \ 8 * (3 - s: Bitwise .and (i , 0x03 ))
119
125
\ )
120
126
\ )
121
127
endfor
@@ -144,7 +150,7 @@ function! s:sha1context.input(bytes) dict abort
144
150
if self .Corrupted
145
151
break
146
152
endif
147
- call self .messageblock.push (s: _uint8 (x ))
153
+ call self .messageblock.push (s: Bitwise . uint8 (x ))
148
154
149
155
if self .messageblock.index == s: sha1blocksize
150
156
call self .process ()
@@ -171,14 +177,14 @@ function! s:sha1context.process() dict abort
171
177
" Initialize the first 16 words in the array W
172
178
"
173
179
for t in range (16 )
174
- let W[t ] = s: bitwise .lshift32 (self .messageblock.data[t * 4 ], 24 )
175
- let W[t ] = s: bitwise .or (W[t ], s: bitwise .lshift32 (self .messageblock.data[t * 4 + 1 ], 16 ))
176
- let W[t ] = s: bitwise .or (W[t ], s: bitwise .lshift32 (self .messageblock.data[t * 4 + 2 ], 8 ))
177
- let W[t ] = s: bitwise .or (W[t ], self .messageblock.data[t * 4 + 3 ])
180
+ let W[t ] = s: Bitwise .lshift32 (self .messageblock.data[t * 4 ], 24 )
181
+ let W[t ] = s: Bitwise .or (W[t ], s: Bitwise .lshift32 (self .messageblock.data[t * 4 + 1 ], 16 ))
182
+ let W[t ] = s: Bitwise .or (W[t ], s: Bitwise .lshift32 (self .messageblock.data[t * 4 + 2 ], 8 ))
183
+ let W[t ] = s: Bitwise .or (W[t ], self .messageblock.data[t * 4 + 3 ])
178
184
endfor
179
185
180
186
for t in range (16 , 79 )
181
- let W[t ] = s: _sha1circular_shift (1 , s: bitwise .xor (s: bitwise .xor (s: bitwise .xor (W[t - 3 ], W[t - 8 ]), W[t - 14 ]), W[t - 16 ]))
187
+ let W[t ] = s: _sha1circular_shift (1 , s: Bitwise .xor (s: Bitwise .xor (s: Bitwise .xor (W[t - 3 ], W[t - 8 ]), W[t - 14 ]), W[t - 16 ]))
182
188
endfor
183
189
184
190
let A = self .intermediatehash[0 ]
@@ -189,7 +195,7 @@ function! s:sha1context.process() dict abort
189
195
190
196
for t in range (20 )
191
197
let temp = s: _sha1circular_shift (5 ,A) +
192
- \ s: bitwise .or (s: bitwise .and (B, C), s: bitwise .and (s: bitwise .invert (B), D)) +
198
+ \ s: Bitwise .or (s: Bitwise .and (B, C), s: Bitwise .and (s: Bitwise .invert (B), D)) +
193
199
\ E + W[t ] + K[0 ]
194
200
let E = D
195
201
let D = C
@@ -199,7 +205,7 @@ function! s:sha1context.process() dict abort
199
205
endfor
200
206
201
207
for t in range (20 , 39 )
202
- let temp = s: _sha1circular_shift (5 ,A) + s: bitwise .xor (s: bitwise .xor (B, C), D) + E + W[t ] + K[1 ]
208
+ let temp = s: _sha1circular_shift (5 ,A) + s: Bitwise .xor (s: Bitwise .xor (B, C), D) + E + W[t ] + K[1 ]
203
209
let E = D
204
210
let D = C
205
211
let C = s: _sha1circular_shift (30 ,B)
@@ -209,7 +215,7 @@ function! s:sha1context.process() dict abort
209
215
210
216
for t in range (40 , 59 )
211
217
let temp = s: _sha1circular_shift (5 ,A) +
212
- \ s: bitwise .or (s: bitwise .or (s: bitwise .and (B, C), s: bitwise .and (B, D)), s: bitwise .and (C, D)) +
218
+ \ s: Bitwise .or (s: Bitwise .or (s: Bitwise .and (B, C), s: Bitwise .and (B, D)), s: Bitwise .and (C, D)) +
213
219
\ E + W[t ] + K[2 ]
214
220
let E = D
215
221
let D = C
@@ -220,7 +226,7 @@ function! s:sha1context.process() dict abort
220
226
221
227
for t in range (60 , 79 )
222
228
let temp = s: _sha1circular_shift (5 ,A) +
223
- \ s: bitwise .xor (s: bitwise .xor (B, C), D) + E + W[t ] + K[3 ]
229
+ \ s: Bitwise .xor (s: Bitwise .xor (B, C), D) + E + W[t ] + K[3 ]
224
230
let E = D
225
231
let D = C
226
232
let C = s: _sha1circular_shift (30 ,B)
@@ -266,14 +272,14 @@ function! s:sha1context.padding() dict abort
266
272
" Store the message length as the last 8 octets
267
273
"
268
274
" as data[-8]..data[-1]
269
- let self .messageblock.data[56 ] = s: _uint8 (s: bitwise .rshift32 (self .length.high, 24 ))
270
- let self .messageblock.data[57 ] = s: _uint8 (s: bitwise .rshift32 (self .length.high, 16 ))
271
- let self .messageblock.data[58 ] = s: _uint8 (s: bitwise .rshift32 (self .length.high, 8 ))
272
- let self .messageblock.data[59 ] = s: _uint8 ( self .length.high )
273
- let self .messageblock.data[60 ] = s: _uint8 (s: bitwise .rshift32 (self .length.low , 24 ))
274
- let self .messageblock.data[61 ] = s: _uint8 (s: bitwise .rshift32 (self .length.low , 16 ))
275
- let self .messageblock.data[62 ] = s: _uint8 (s: bitwise .rshift32 (self .length.low , 8 ))
276
- let self .messageblock.data[63 ] = s: _uint8 ( self .length.low )
275
+ let self .messageblock.data[56 ] = s: Bitwise . uint8 (s: Bitwise .rshift32 (self .length.high, 24 ))
276
+ let self .messageblock.data[57 ] = s: Bitwise . uint8 (s: Bitwise .rshift32 (self .length.high, 16 ))
277
+ let self .messageblock.data[58 ] = s: Bitwise . uint8 (s: Bitwise .rshift32 (self .length.high, 8 ))
278
+ let self .messageblock.data[59 ] = s: Bitwise . uint8 ( self .length.high )
279
+ let self .messageblock.data[60 ] = s: Bitwise . uint8 (s: Bitwise .rshift32 (self .length.low , 24 ))
280
+ let self .messageblock.data[61 ] = s: Bitwise . uint8 (s: Bitwise .rshift32 (self .length.low , 16 ))
281
+ let self .messageblock.data[62 ] = s: Bitwise . uint8 (s: Bitwise .rshift32 (self .length.low , 8 ))
282
+ let self .messageblock.data[63 ] = s: Bitwise . uint8 ( self .length.low )
277
283
278
284
call self .process ()
279
285
endfunction
@@ -305,38 +311,19 @@ function! s:sha1context.length.sizeset(data) dict abort
305
311
" 3.b high shift = 0x0000000l >> (32 - 3)
306
312
" endif
307
313
" 32/64bit work use shift
308
- let self .high = s: _uint32 (s: bitwise .rshift (len (a: data ), 32 - 3 ))
309
- let self .low = s: _uint32 (s: bitwise .lshift (len (a: data ), 3 ))
314
+ let self .high = s: Bitwise . uint32 (s: Bitwise .rshift (len (a: data ), 32 - 3 ))
315
+ let self .low = s: Bitwise . uint32 (s: Bitwise .lshift (len (a: data ), 3 ))
310
316
311
317
" SHA1 2^64 - 1 overflow check
312
- " 0xh0000000 is not 0, then overflow it(byte data are Vim List;it can contain 2^64 - 1 item)
313
- if (has (' num64' ) && (0 != s: _uint32 (s: bitwise .rshift (len (a: data ), 32 + (32 - 3 )))))
318
+ " 0xh0000000 is not 0, then overflow it(byte data are Vim List;it can contains 2^64 - 1 item)
319
+ if (has (' num64' ) && (0 != s: Bitwise . uint32 (s: Bitwise .rshift (len (a: data ), 32 + (32 - 3 )))))
314
320
let self .high = 0
315
321
let self .low = 0
316
322
return s: input_long
317
323
endif
318
324
return s: success
319
325
endfunction
320
326
321
- " ---------------------------------------------------------------------
322
- " misc
323
-
324
- function ! s: _uint8 (n ) abort
325
- return s: bitwise .and (a: n , 0xFF )
326
- endfunction
327
-
328
- function ! s: _uint32 (n ) abort
329
- return s: bitwise .and (a: n , 0xFFFFFFFF )
330
- endfunction
331
-
332
- function ! s: _str2bytes (str) abort
333
- return map (range (len (a: str )), ' char2nr(a:str[v:val])' )
334
- endfunction
335
-
336
- function ! s: _bytes2binstr (bytes) abort
337
- return join (map (copy (a: bytes ), ' printf('' %02x'' , v:val)' ), ' ' )
338
- endfunction
339
-
340
327
let &cpo = s: save_cpo
341
328
unlet s: save_cpo
342
329
0 commit comments