@@ -185,37 +185,37 @@ export function crc16CCITTFALSE(data: number[] | Uint8Array | Buffer): number {
185
185
return calcCRC ( data , 16 , 0x1021 , 0xffff ) ;
186
186
}
187
187
188
+ function aes128MmoHashUpdate ( result : Buffer , data : Buffer , dataSize : number ) : void {
189
+ while ( dataSize >= AES_MMO_128_BLOCK_SIZE ) {
190
+ const cipher = createCipheriv ( 'aes-128-ecb' , result , null ) ;
191
+ const block = data . subarray ( 0 , AES_MMO_128_BLOCK_SIZE ) ;
192
+ const encryptedBlock = Buffer . concat ( [ cipher . update ( block ) , cipher . final ( ) ] ) ;
193
+
194
+ // XOR encrypted and plaintext
195
+ for ( let i = 0 ; i < AES_MMO_128_BLOCK_SIZE ; i ++ ) {
196
+ result [ i ] = encryptedBlock [ i ] ^ block [ i ] ;
197
+ }
198
+
199
+ data = data . subarray ( AES_MMO_128_BLOCK_SIZE ) ;
200
+ dataSize -= AES_MMO_128_BLOCK_SIZE ;
201
+ }
202
+ }
203
+
188
204
/**
189
205
* AES-128-MMO (Matyas-Meyer-Oseas) hashing (using node 'crypto' built-in with 'aes-128-ecb')
190
206
*
191
207
* Used for Install Codes - see Document 13-0402-13 - 10.1
192
208
*/
193
209
export function aes128MmoHash ( data : Buffer ) : Buffer {
194
- const update = ( result : Buffer , data : Buffer , dataSize : number ) : boolean => {
195
- while ( dataSize >= AES_MMO_128_BLOCK_SIZE ) {
196
- const cipher = createCipheriv ( 'aes-128-ecb' , result , null ) ;
197
- const block = data . subarray ( 0 , AES_MMO_128_BLOCK_SIZE ) ;
198
- const encryptedBlock = Buffer . concat ( [ cipher . update ( block ) , cipher . final ( ) ] ) ;
199
-
200
- // XOR encrypted and plaintext
201
- for ( let i = 0 ; i < AES_MMO_128_BLOCK_SIZE ; i ++ ) {
202
- result [ i ] = encryptedBlock [ i ] ^ block [ i ] ;
203
- }
204
-
205
- data = data . subarray ( AES_MMO_128_BLOCK_SIZE ) ;
206
- dataSize -= AES_MMO_128_BLOCK_SIZE ;
207
- }
208
-
209
- return true ;
210
- } ;
211
-
212
210
const hashResult = Buffer . alloc ( AES_MMO_128_BLOCK_SIZE ) ;
213
211
const temp = Buffer . alloc ( AES_MMO_128_BLOCK_SIZE ) ;
214
212
let remainingLength = data . length ;
215
213
let position = 0 ;
216
214
217
215
for ( position ; remainingLength >= AES_MMO_128_BLOCK_SIZE ; ) {
218
- update ( hashResult , data . subarray ( position , position + AES_MMO_128_BLOCK_SIZE ) , data . length ) ;
216
+ const chunk = data . subarray ( position , position + AES_MMO_128_BLOCK_SIZE ) ;
217
+
218
+ aes128MmoHashUpdate ( hashResult , chunk , chunk . length ) ;
219
219
220
220
position += AES_MMO_128_BLOCK_SIZE ;
221
221
remainingLength -= AES_MMO_128_BLOCK_SIZE ;
@@ -230,14 +230,14 @@ export function aes128MmoHash(data: Buffer): Buffer {
230
230
231
231
// if appending the bit string will push us beyond the 16-byte boundary, hash that block and append another 16-byte block
232
232
if ( AES_MMO_128_BLOCK_SIZE - remainingLength < 3 ) {
233
- update ( hashResult , temp , AES_MMO_128_BLOCK_SIZE ) ;
233
+ aes128MmoHashUpdate ( hashResult , temp , AES_MMO_128_BLOCK_SIZE ) ;
234
234
temp . fill ( 0 ) ;
235
235
}
236
236
237
237
temp [ AES_MMO_128_BLOCK_SIZE - 2 ] = ( data . length >> 5 ) & 0xff ;
238
238
temp [ AES_MMO_128_BLOCK_SIZE - 1 ] = ( data . length << 3 ) & 0xff ;
239
239
240
- update ( hashResult , temp , AES_MMO_128_BLOCK_SIZE ) ;
240
+ aes128MmoHashUpdate ( hashResult , temp , AES_MMO_128_BLOCK_SIZE ) ;
241
241
242
242
const result = Buffer . alloc ( AES_MMO_128_BLOCK_SIZE ) ;
243
243
0 commit comments