@@ -118,7 +118,7 @@ export function chacha20_poly1305_encryptAndSeal(key: Buffer, nonce: Buffer, aad
118118 * @group Cryptography
119119 */
120120export function layerEncrypt ( data : Buffer , encryption : HAPEncryption ) : Buffer {
121- let result = Buffer . alloc ( 0 ) ;
121+ const chunks : Buffer [ ] = [ ] ;
122122 const total = data . length ;
123123 for ( let offset = 0 ; offset < total ; ) {
124124 const length = Math . min ( total - offset , 0x400 ) ;
@@ -131,9 +131,9 @@ export function layerEncrypt(data: Buffer, encryption: HAPEncryption): Buffer {
131131 const encrypted = chacha20_poly1305_encryptAndSeal ( encryption . accessoryToControllerKey , nonce , leLength , data . subarray ( offset , offset + length ) ) ;
132132 offset += length ;
133133
134- result = Buffer . concat ( [ result , leLength , encrypted . ciphertext , encrypted . authTag ] ) ;
134+ chunks . push ( leLength , encrypted . ciphertext , encrypted . authTag ) ;
135135 }
136- return result ;
136+ return Buffer . concat ( chunks ) ;
137137}
138138
139139/**
@@ -145,7 +145,7 @@ export function layerDecrypt(packet: Buffer, encryption: HAPEncryption): Buffer
145145 encryption . incompleteFrame = undefined ;
146146 }
147147
148- let result = Buffer . alloc ( 0 ) ;
148+ const chunks : Buffer [ ] = [ ] ;
149149 const total = packet . length ;
150150
151151 for ( let offset = 0 ; offset < total ; ) {
@@ -167,9 +167,9 @@ export function layerDecrypt(packet: Buffer, encryption: HAPEncryption): Buffer
167167 packet . subarray ( offset + 2 , offset + 2 + realDataLength ) ,
168168 packet . subarray ( offset + 2 + realDataLength , offset + 2 + realDataLength + 16 ) ,
169169 ) ;
170- result = Buffer . concat ( [ result , plaintext ] ) ;
170+ chunks . push ( plaintext ) ;
171171 offset += ( 18 + realDataLength ) ;
172172 }
173173
174- return result ;
174+ return Buffer . concat ( chunks ) ;
175175}
0 commit comments