-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1209 from atsign-foundation/replace_encryption_me…
…thods_with_atchops feat: Replace encryption methods/key generation from EncryptionUtil with AtChops
- Loading branch information
Showing
7 changed files
with
381 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/at_client/test/at_chops_encryption_util_compatibility_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import 'package:at_chops/at_chops.dart'; | ||
import 'package:at_client/at_client.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
// Tests to verify whether encryption with EncryptionUtil, decryption with AtChops works and vice versa. | ||
void main() { | ||
test( | ||
'A test to verify encrypting AES key with encryption util and decryption with at_chops', | ||
() { | ||
// Generate RSA key pair. Generate AES key. Encrypt AES key using RSA public key using EncryptionUtil method | ||
// Decrypt encryptedAESKey using AtChops (uses RSA private key) | ||
var encryptionKeyPair = AtChopsUtil.generateAtEncryptionKeyPair(); | ||
var encryptionPublicKey = encryptionKeyPair.atPublicKey.publicKey; | ||
var aesKey = EncryptionUtil.generateAESKey(); | ||
var encryptedAesKey = | ||
EncryptionUtil.encryptKey(aesKey, encryptionPublicKey); | ||
AtChopsKeys atChopsKeys = AtChopsKeys.create(encryptionKeyPair, null); | ||
var atChops = AtChopsImpl(atChopsKeys); | ||
var decryptedAesKey = atChops | ||
.decryptString(encryptedAesKey, EncryptionKeyType.rsa2048) | ||
.result; | ||
expect(decryptedAesKey, aesKey); | ||
}); | ||
test( | ||
'A test to verify encrypting AES key with at_chops and decryption with EncryptionUtil', | ||
() { | ||
// Generate RSA key pair. Generate AES key. Encrypt AES key using AtChops(uses RSA public key) | ||
// Decrypt encryptedAESKey with EncryptionUtil using RSA private key | ||
var encryptionKeyPair = AtChopsUtil.generateAtEncryptionKeyPair(); | ||
var encryptionPrivateKey = encryptionKeyPair.atPrivateKey.privateKey; | ||
var aesKey = AtChopsUtil.generateSymmetricKey(EncryptionKeyType.aes256).key; | ||
|
||
AtChopsKeys atChopsKeys = AtChopsKeys.create(encryptionKeyPair, null); | ||
var atChops = AtChopsImpl(atChopsKeys); | ||
var encryptedAesKey = | ||
atChops.encryptString(aesKey, EncryptionKeyType.rsa2048).result; | ||
|
||
var decryptedAesKey = | ||
//ignore: deprecated_member_use_from_same_package | ||
EncryptionUtil.decryptKey(encryptedAesKey, encryptionPrivateKey); | ||
expect(decryptedAesKey, aesKey); | ||
}); | ||
|
||
test( | ||
'A test to verify data encryption with encryption util and decryption with at_chops', | ||
() { | ||
// Generate AES key. Encrypt data with EncryptionUtil using AES key | ||
// Create a AESEncryption algo object using AES key and pass it to AtChops. Decrypt the encrypted value with AtChops | ||
var aesKey = EncryptionUtil.generateAESKey(); | ||
var dataToEncrypt = '[email protected]'; | ||
var encryptedData = EncryptionUtil.encryptValue(dataToEncrypt, aesKey); | ||
var encryptionAlgo = AESEncryptionAlgo(AESKey(aesKey)); | ||
AtChopsKeys atChopsKeys = AtChopsKeys.create(null, null); | ||
var atChops = AtChopsImpl(atChopsKeys); | ||
var decryptedData = atChops | ||
.decryptString(encryptedData, EncryptionKeyType.aes256, | ||
encryptionAlgorithm: encryptionAlgo, | ||
iv: AtChopsUtil.generateIVLegacy()) | ||
.result; | ||
expect(decryptedData, dataToEncrypt); | ||
}); | ||
|
||
test( | ||
'A test to verify data encryption with at_chops and decryption with encryption_util', | ||
() { | ||
// Generate AES key. Encrypt data with AtChops using AES key | ||
// Decrypt the encrypted value with EncryptionUtil | ||
var aesKey = AtChopsUtil.generateSymmetricKey(EncryptionKeyType.aes256); | ||
var dataToEncrypt = '[email protected]'; | ||
var encryptionAlgo = AESEncryptionAlgo(AESKey(aesKey.key)); | ||
AtChopsKeys atChopsKeys = AtChopsKeys.create(null, null); | ||
var atChops = AtChopsImpl(atChopsKeys); | ||
var encryptedData = atChops | ||
.encryptString(dataToEncrypt, EncryptionKeyType.aes256, | ||
encryptionAlgorithm: encryptionAlgo, | ||
iv: AtChopsUtil.generateIVLegacy()) | ||
.result; | ||
var decryptedData = EncryptionUtil.decryptValue(encryptedData, aesKey.key); | ||
expect(decryptedData, dataToEncrypt); | ||
}); | ||
|
||
test( | ||
'A test to verify data(with emoji) encryption with encryption util and decryption with at_chops', | ||
() { | ||
// Generate AES key. Encrypt data with EncryptionUtil using AES key | ||
// Create a AESEncryption algo object using AES key and pass it to AtChops. Decrypt the encrypted value with AtChops | ||
var aesKey = EncryptionUtil.generateAESKey(); | ||
var dataToEncrypt = 'alice@🦄🛠'; | ||
var encryptedData = EncryptionUtil.encryptValue(dataToEncrypt, aesKey); | ||
var encryptionAlgo = AESEncryptionAlgo(AESKey(aesKey)); | ||
AtChopsKeys atChopsKeys = AtChopsKeys.create(null, null); | ||
var atChops = AtChopsImpl(atChopsKeys); | ||
var decryptedData = atChops | ||
.decryptString(encryptedData, EncryptionKeyType.aes256, | ||
encryptionAlgorithm: encryptionAlgo, | ||
iv: AtChopsUtil.generateIVLegacy()) | ||
.result; | ||
expect(decryptedData, dataToEncrypt); | ||
}); | ||
|
||
test( | ||
'A test to verify data(with emoji) encryption with at_chops and decryption with encryption_util', | ||
() { | ||
// Generate AES key. Encrypt data with AtChops using AES key | ||
// Decrypt the encrypted value with EncryptionUtil | ||
var aesKey = AtChopsUtil.generateSymmetricKey(EncryptionKeyType.aes256); | ||
var dataToEncrypt = 'alice@🦄🛠'; | ||
var encryptionAlgo = AESEncryptionAlgo(AESKey(aesKey.key)); | ||
AtChopsKeys atChopsKeys = AtChopsKeys.create(null, null); | ||
var atChops = AtChopsImpl(atChopsKeys); | ||
var encryptedData = atChops | ||
.encryptString(dataToEncrypt, EncryptionKeyType.aes256, | ||
encryptionAlgorithm: encryptionAlgo, | ||
iv: AtChopsUtil.generateIVLegacy()) | ||
.result; | ||
var decryptedData = EncryptionUtil.decryptValue(encryptedData, aesKey.key); | ||
expect(decryptedData, dataToEncrypt); | ||
}); | ||
} |
Oops, something went wrong.