From 44c1c2a229154f769e91654001d2523ae7c038d1 Mon Sep 17 00:00:00 2001 From: Alexandre Roux Date: Tue, 10 Dec 2024 14:57:12 +0100 Subject: [PATCH] test aes --- app_crypto/lib/src/aes.dart | 3 ++- app_crypto/test/aes_test.dart | 14 +++++++++++++- app_crypto/test/raw_aes_test.dart | 10 ++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app_crypto/lib/src/aes.dart b/app_crypto/lib/src/aes.dart index ac59ae0..64f0016 100644 --- a/app_crypto/lib/src/aes.dart +++ b/app_crypto/lib/src/aes.dart @@ -77,7 +77,8 @@ class AesWithIVEntrypter extends _AesStringEncrypter { /// /// returns a base 64 encrypted string. /// -/// Encryption used is AES. +/// Encryption used is AES. Calling multiple times will not return the same +/// result as a salt is added to the input. String aesEncrypt(String decoded, String password) => aesEncrypterFromPassword(password).encrypt(decoded); diff --git a/app_crypto/test/aes_test.dart b/app_crypto/test/aes_test.dart index ae96c6d..9e19ee2 100644 --- a/app_crypto/test/aes_test.dart +++ b/app_crypto/test/aes_test.dart @@ -14,7 +14,19 @@ void main() { expect(encrypter.decrypt(encrypted), decoded); } - test('aes encrypt decrypt', () { + test('aes encrypt different output', () { + var password = 'EA5eg5hQVuyPz3EaKqx4vcCJyQZKI5x7'; + var encryptSet = {}; + for (var i = 0; i < 1000; i++) { + var encrypted = aesEncrypt('test', password); + print('"test" aesEncrypt by $password: $encrypted'); + encryptSet.add(encrypted); + if (encryptSet.length > 1) { + break; + } + } + }); + test('raw aes encrypt decrypt', () { var password = r'E4x*$TwbkJC-xK4KGC4zJF9j*Rh&WLgR'; var encrypter = AesWithIVEntrypter(password, IV.allZerosOfLength(16)); diff --git a/app_crypto/test/raw_aes_test.dart b/app_crypto/test/raw_aes_test.dart index be45e2c..e4c55a4 100644 --- a/app_crypto/test/raw_aes_test.dart +++ b/app_crypto/test/raw_aes_test.dart @@ -3,7 +3,8 @@ import 'dart:typed_data'; import 'package:encrypt/encrypt.dart'; import 'package:test/test.dart'; -String aesEncrypt(String decoded, String password) { +/// Old bidirectional encryption +String legacyAesEncrypt(String decoded, String password) { final key = Key.fromUtf8(password); // final iv = IV.fromLength(16); final iv = IV(Uint8List(16)); @@ -11,7 +12,8 @@ String aesEncrypt(String decoded, String password) { return encrypter.encrypt(decoded, iv: iv).base64; } -String aesDecrypt(String encoded, String password) { +/// Old bidirectional encryption +String legacyAesDecrypt(String encoded, String password) { final key = Key.fromUtf8(password); // final iv = IV.fromLength(16); final iv = IV(Uint8List(16)); @@ -22,7 +24,7 @@ String aesDecrypt(String encoded, String password) { void main() { test('AES encrypt/decrypt', () { var password = r'E4x*$TwbkJC-xK4KGC4zJF9j*Rh&WLgR'; - expect(aesEncrypt('test', password), 'amGhyRRLUIoE59IiEys5Vw=='); - expect(aesDecrypt('amGhyRRLUIoE59IiEys5Vw==', password), 'test'); + expect(legacyAesEncrypt('test', password), 'amGhyRRLUIoE59IiEys5Vw=='); + expect(legacyAesDecrypt('amGhyRRLUIoE59IiEys5Vw==', password), 'test'); }); }