@@ -190,6 +190,59 @@ test(SUITE, 'Buffer concat vs string concat produce same result', () => {
190190 expect ( bufResult ) . to . equal ( strResult ) ;
191191} ) ;
192192
193+ test ( SUITE , 'base64 string encoding with multi-block plaintext' , ( ) => {
194+ const testKey = Buffer . from (
195+ 'KTnGEDonslhj/qGvf6rj4HSnO32T7dvjAs5PntTDB0s=' ,
196+ 'base64' ,
197+ ) ;
198+ const testIv = Buffer . from ( '2pXx2krk1wU8RI6AQjuPUg==' , 'base64' ) ;
199+ // 32 bytes = 2 AES blocks; update() returns 32 bytes, 32 % 3 = 2 remainder
200+ const text = 'A' . repeat ( 32 ) ;
201+
202+ const cipher1 = createCipheriv ( 'aes-256-cbc' , testKey , testIv ) ;
203+ const bufResult = Buffer . concat ( [
204+ cipher1 . update ( Buffer . from ( text , 'utf8' ) ) ,
205+ cipher1 . final ( ) ,
206+ ] ) . toString ( 'base64' ) ;
207+
208+ const cipher2 = createCipheriv ( 'aes-256-cbc' , testKey , testIv ) ;
209+ const strResult =
210+ cipher2 . update ( text , 'utf8' , 'base64' ) + cipher2 . final ( 'base64' ) ;
211+
212+ expect ( bufResult ) . to . equal ( strResult ) ;
213+ } ) ;
214+
215+ test ( SUITE , 'base64 encoding at exactly one block boundary' , ( ) => {
216+ // 16 bytes = exactly one AES block; update() returns 16 bytes, 16 % 3 = 1
217+ const text = 'A' . repeat ( 16 ) ;
218+
219+ const cipher1 = createCipheriv ( 'aes-128-cbc' , key16 , iv ) ;
220+ const bufResult = Buffer . concat ( [
221+ cipher1 . update ( Buffer . from ( text , 'utf8' ) ) ,
222+ cipher1 . final ( ) ,
223+ ] ) . toString ( 'base64' ) ;
224+
225+ const cipher2 = createCipheriv ( 'aes-128-cbc' , key16 , iv ) ;
226+ const strResult =
227+ cipher2 . update ( text , 'utf8' , 'base64' ) + cipher2 . final ( 'base64' ) ;
228+
229+ expect ( bufResult ) . to . equal ( strResult ) ;
230+ } ) ;
231+
232+ test ( SUITE , 'base64 encoding encrypt/decrypt roundtrip with long input' , ( ) => {
233+ const longText = 'The quick brown fox jumps over the lazy dog. ' . repeat ( 5 ) ;
234+
235+ const cipher = createCipheriv ( 'aes-256-cbc' , key32 , iv ) ;
236+ const encrypted =
237+ cipher . update ( longText , 'utf8' , 'base64' ) + cipher . final ( 'base64' ) ;
238+
239+ const decipher = createDecipheriv ( 'aes-256-cbc' , key32 , iv ) ;
240+ const decrypted =
241+ decipher . update ( encrypted , 'base64' , 'utf8' ) + decipher . final ( 'utf8' ) ;
242+
243+ expect ( decrypted ) . to . equal ( longText ) ;
244+ } ) ;
245+
193246test ( SUITE , 'update with hex input and output encoding' , ( ) => {
194247 const cipher1 = createCipheriv ( 'aes-128-cbc' , key16 , iv ) ;
195248 const bufResult = Buffer . concat ( [
0 commit comments