Skip to content

Commit

Permalink
test aes
Browse files Browse the repository at this point in the history
  • Loading branch information
alextekartik committed Dec 10, 2024
1 parent 580b1ed commit 44c1c2a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app_crypto/lib/src/aes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
14 changes: 13 additions & 1 deletion app_crypto/test/aes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <String>{};
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));

Expand Down
10 changes: 6 additions & 4 deletions app_crypto/test/raw_aes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ 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));
final encrypter = Encrypter(AES(key));
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));
Expand All @@ -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');
});
}

0 comments on commit 44c1c2a

Please sign in to comment.