-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* cryptorHeader for new cryptoModule * format! * added AesCbcCryptor, ICryptor * format! * * LegacyCryptor, * added: newCryptoModule * new crytoModule * test: crypto Module * dart fix * format!! again * * expose CryptoModule for users, * deprecation warning for `cipherKey` * code cleanup * show CryptoConfiguration for legacy cryptor configuration support * Update CODEOWNERS * * Added empty content handling while encry and decrypt, * Added more information in exception string for reason fo failure. * naming convention fix for CryptoModule factory. * code formatting. * removed unused class ILegacyCryptor * fix: encrypt/decrypt File methods * fix: explicit cipherKey at method params defaulting to legacy cryptor * added condition to apply user specified cryptoModule for encryption/decryption, * refactoring to fix broken format issue with decrypted hostory messages, * files encryption/ decryption conditions refined * update LICENSE * PubNub SDK v4.3.0 release. --------- Co-authored-by: PubNub Release Bot <[email protected]>
- Loading branch information
1 parent
639bb30
commit ba5935d
Showing
41 changed files
with
983 additions
and
189 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
* @are @mohitpubnub @parfeon | ||
.github/* @parfeon @are @mohitpubnub | ||
* @jguz-pubnub @mohitpubnub @parfeon | ||
.github/* @jguz-pubnub @parfeon @mohitpubnub | ||
README.md @techwritermat @kazydek |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'dart:io'; | ||
|
||
String getCryptoFilePath(String filename) { | ||
var assets = Directory( | ||
'../../service-contract-mock/contract/features/encryption/assets'); | ||
return '${assets.path}/$filename'; | ||
} | ||
|
||
bool listEquals<E>(List<E> list1, List<E> list2) { | ||
if (identical(list1, list2)) { | ||
return true; | ||
} | ||
|
||
if (list1.length != list2.length) { | ||
return false; | ||
} | ||
|
||
for (var i = 0; i < list1.length; i += 1) { | ||
if (list1[i] != list2[i]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
30 changes: 30 additions & 0 deletions
30
acceptance_tests/lib/src/steps/crypto_module/crypto_module_steps.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,30 @@ | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_given_cipher_key.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_given_legacy_crypto.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_given_multiple_cryptors.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_given_vector.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_then_decrypt_success.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_then_decrypted.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_then_outcome.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_when_decrypt.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_when_decrypt_as.dart'; | ||
import 'package:acceptance_tests/src/steps/crypto_module/step_when_encrypt.dart'; | ||
|
||
import '../../world.dart'; | ||
|
||
import 'package:gherkin/gherkin.dart'; | ||
|
||
import 'step_given_crypto_module.dart'; | ||
|
||
final List<StepDefinitionGeneric<PubNubWorld>> cryptoSteps = [ | ||
StepGivenCryptoModule(), | ||
StepGivenCipherKey(), | ||
StepGivenVector(), | ||
StepWhenDecryptFileAs(), | ||
StepThenDecryptedContentEquals(), | ||
StepGivenLegacyCryptoModule(), | ||
StepWhenDecryptFile(), | ||
StepThenOutcome(), | ||
StepWhenEncrypt(), | ||
ThenDecryptSuccessWithLegacy(), | ||
StepGivenCryptoModuleWithMultipleCryptors() | ||
]; |
13 changes: 13 additions & 0 deletions
13
acceptance_tests/lib/src/steps/crypto_module/step_given_cipher_key.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,13 @@ | ||
import 'package:gherkin/gherkin.dart'; | ||
|
||
import '../../world.dart'; | ||
|
||
class StepGivenCipherKey extends Given1WithWorld<String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp(r'with {string} cipher key'); | ||
|
||
@override | ||
Future<void> executeStep(String cipherKey) async { | ||
world.scenarioContext['cipherKey'] = cipherKey; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
acceptance_tests/lib/src/steps/crypto_module/step_given_crypto_module.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,13 @@ | ||
import 'package:gherkin/gherkin.dart'; | ||
|
||
import '../../world.dart'; | ||
|
||
class StepGivenCryptoModule extends Given1WithWorld<String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp(r'Crypto module with {string} cryptor'); | ||
|
||
@override | ||
Future<void> executeStep(String id) async { | ||
world.scenarioContext['cryptorId'] = id; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
acceptance_tests/lib/src/steps/crypto_module/step_given_legacy_crypto.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,17 @@ | ||
import 'package:gherkin/gherkin.dart'; | ||
|
||
import '../../world.dart'; | ||
|
||
class StepGivenLegacyCryptoModule | ||
extends Given2WithWorld<String, String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => | ||
RegExp(r'Legacy code with {string} cipher key and {vector} vector'); | ||
|
||
@override | ||
Future<void> executeStep(String cipherKey, String vector) async { | ||
world.scenarioContext['useRandomIntializationVector'] = | ||
vector == 'constant' ? false : true; | ||
world.scenarioContext['cipherKey'] = cipherKey; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
acceptance_tests/lib/src/steps/crypto_module/step_given_multiple_cryptors.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,17 @@ | ||
import 'package:gherkin/gherkin.dart'; | ||
|
||
import '../../world.dart'; | ||
|
||
class StepGivenCryptoModuleWithMultipleCryptors | ||
extends Given2WithWorld<String, String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp( | ||
r'Crypto module with default {string} and additional {string} cryptors'); | ||
|
||
@override | ||
Future<void> executeStep( | ||
String defaultCryptorId, String additionalCryptorId) async { | ||
world.scenarioContext['defaultCryptorId'] = defaultCryptorId; | ||
world.scenarioContext['additionalCryptorId'] = additionalCryptorId; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
acceptance_tests/lib/src/steps/crypto_module/step_given_vector.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,14 @@ | ||
import 'package:gherkin/gherkin.dart'; | ||
|
||
import '../../world.dart'; | ||
|
||
class StepGivenVector extends Given1WithWorld<String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp(r'with {string} vector'); | ||
|
||
@override | ||
Future<void> executeStep(String vector) async { | ||
world.scenarioContext['useRandomIntializationVector'] = | ||
vector == 'constant' ? false : true; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
acceptance_tests/lib/src/steps/crypto_module/step_then_decrypt_success.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,21 @@ | ||
import 'package:gherkin/gherkin.dart'; | ||
import 'package:pubnub/core.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../world.dart'; | ||
import '_utils.dart'; | ||
|
||
class ThenDecryptSuccessWithLegacy extends ThenWithWorld<PubNubWorld> { | ||
@override | ||
RegExp get pattern => | ||
RegExp(r'Successfully decrypt an encrypted file with legacy code'); | ||
|
||
@override | ||
Future<void> executeStep() async { | ||
ICryptoModule cryptoModule = world.scenarioContext['cryptoModule']; | ||
var decryptedData = | ||
cryptoModule.decrypt(world.scenarioContext['encryptedData']); | ||
this.expect( | ||
listEquals(decryptedData, world.scenarioContext['fileData']), isTrue); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
acceptance_tests/lib/src/steps/crypto_module/step_then_decrypted.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,22 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:gherkin/gherkin.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../world.dart'; | ||
import '_utils.dart'; | ||
|
||
class StepThenDecryptedContentEquals | ||
extends Then1WithWorld<String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => | ||
RegExp(r'Decrypted file content equal to the {string} file content'); | ||
|
||
@override | ||
Future<void> executeStep(String file) async { | ||
var sourceContent = | ||
File(getCryptoFilePath(file)).readAsBytesSync().toList(); | ||
var ec = world.scenarioContext['decryptedContent']; | ||
this.expect(listEquals(ec, sourceContent), isTrue); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
acceptance_tests/lib/src/steps/crypto_module/step_then_outcome.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,20 @@ | ||
import 'package:gherkin/gherkin.dart'; | ||
import 'package:pubnub/core.dart'; | ||
import 'package:test/expect.dart'; | ||
|
||
import '../../world.dart'; | ||
|
||
class StepThenOutcome extends Then1WithWorld<String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp(r'I receive {string}'); | ||
|
||
@override | ||
Future<void> executeStep(String expected) async { | ||
if (expected == 'success') { | ||
this.expect(world.latestException, isNull); | ||
} else { | ||
var outcome = world.latestException; | ||
this.expect((outcome as CryptoException).message, contains(expected)); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
acceptance_tests/lib/src/steps/crypto_module/step_when_decrypt.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,28 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:gherkin/gherkin.dart'; | ||
import 'package:pubnub/core.dart'; | ||
import 'package:pubnub/crypto.dart'; | ||
|
||
import '../../world.dart'; | ||
import '_utils.dart'; | ||
|
||
class StepWhenDecryptFile extends When1WithWorld<String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp(r'I decrypt {string} file'); | ||
|
||
@override | ||
Future<void> executeStep(String file) async { | ||
late ICryptoModule cryptoModule; | ||
var cipherKey = CipherKey.fromUtf8(world.scenarioContext['cipherKey']); | ||
if (world.scenarioContext['cryptorId'] == 'acrh') { | ||
cryptoModule = CryptoModule(defaultCryptor: AesCbcCryptor(cipherKey)); | ||
} | ||
var data = File(getCryptoFilePath(file)).readAsBytesSync().toList(); | ||
try { | ||
cryptoModule.decrypt(data); | ||
} catch (e) { | ||
world.latestException = e; | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
acceptance_tests/lib/src/steps/crypto_module/step_when_decrypt_as.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,57 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:gherkin/gherkin.dart'; | ||
import 'package:pubnub/core.dart'; | ||
import 'package:pubnub/crypto.dart'; | ||
|
||
import '../../world.dart'; | ||
import '_utils.dart'; | ||
|
||
class StepWhenDecryptFileAs | ||
extends When2WithWorld<String, String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp(r'I decrypt {string} file as {string}'); | ||
|
||
@override | ||
Future<void> executeStep(String file, String format) async { | ||
var cryptorId = world.scenarioContext['cryptorId']; | ||
var cipherKey = CipherKey.fromUtf8(world.scenarioContext['cipherKey']); | ||
late ICryptor cryptor; | ||
late ICryptoModule cryptoModule; | ||
if (cryptorId == 'legacy') { | ||
cryptor = LegacyCryptor(cipherKey, | ||
cryptoConfiguration: CryptoConfiguration( | ||
useRandomInitializationVector: | ||
world.scenarioContext['useRandomIntializationVector'])); | ||
cryptoModule = CryptoModule(defaultCryptor: cryptor); | ||
} else if (cryptorId == 'acrh') { | ||
cryptor = AesCbcCryptor(cipherKey); | ||
cryptoModule = CryptoModule(defaultCryptor: cryptor); | ||
} | ||
var defaultCryptorId = world.scenarioContext['defaultCryptorId']; | ||
if (defaultCryptorId == 'legacy') { | ||
var defaultCryptor = cryptor = LegacyCryptor(cipherKey, | ||
cryptoConfiguration: CryptoConfiguration( | ||
useRandomInitializationVector: | ||
world.scenarioContext['useRandomIntializationVector'])); | ||
var additionalCryptor = AesCbcCryptor(cipherKey); | ||
cryptoModule = CryptoModule( | ||
defaultCryptor: defaultCryptor, cryptors: [additionalCryptor]); | ||
} else if (defaultCryptorId == 'acrh') { | ||
var additionalCryptor = cryptor = LegacyCryptor(cipherKey, | ||
cryptoConfiguration: CryptoConfiguration( | ||
useRandomInitializationVector: | ||
world.scenarioContext['useRandomIntializationVector'])); | ||
var defaultCryptor = AesCbcCryptor(cipherKey); | ||
cryptoModule = CryptoModule( | ||
defaultCryptor: defaultCryptor, cryptors: [additionalCryptor]); | ||
} | ||
var fileData = File(getCryptoFilePath(file)).readAsBytesSync().toList(); | ||
try { | ||
world.scenarioContext['decryptedContent'] = | ||
cryptoModule.decrypt(fileData); | ||
} catch (e) { | ||
world.latestException = e; | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
acceptance_tests/lib/src/steps/crypto_module/step_when_encrypt.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,35 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:gherkin/gherkin.dart'; | ||
import 'package:pubnub/core.dart'; | ||
import 'package:pubnub/crypto.dart'; | ||
|
||
import '../../world.dart'; | ||
import '_utils.dart'; | ||
|
||
class StepWhenEncrypt extends When2WithWorld<String, String, PubNubWorld> { | ||
@override | ||
RegExp get pattern => RegExp(r'I encrypt {string} file as {string}'); | ||
|
||
@override | ||
Future<void> executeStep(String file, String format) async { | ||
if (format == 'binary') { | ||
var fileData = File(getCryptoFilePath(file)).readAsBytesSync().toList(); | ||
world.scenarioContext['fileData'] = fileData; | ||
|
||
var cryptor = LegacyCryptor( | ||
CipherKey.fromUtf8(world.scenarioContext['cipherKey']), | ||
cryptoConfiguration: CryptoConfiguration( | ||
useRandomInitializationVector: | ||
world.scenarioContext['useRandomIntializationVector'])); | ||
var cryptoModule = CryptoModule(defaultCryptor: cryptor); | ||
world.scenarioContext['cryptoModule'] = cryptoModule; | ||
try { | ||
var encryptedData = cryptoModule.encrypt(fileData); | ||
world.scenarioContext['encryptedData'] = encryptedData; | ||
} catch (e) { | ||
world.latestException = e; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.