Skip to content

Commit

Permalink
added AesCbcCryptor, ICryptor
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Sep 27, 2023
1 parent 29f034f commit 430326d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pubnub/lib/src/core/crypto/crypto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,22 @@ abstract class ICryptoModule {
List<int> encryptFileData(CipherKey key, List<int> input);
List<int> decryptFileData(CipherKey key, List<int> input);
}

/// @nodoc
abstract class ICryptor {
String get identifier;
EncryptedData encrypt(List<int> input);
List<int> decrypt(EncryptedData input);
}

class EncryptedData {
List<int> _data;
List<int> _metadata;

List<int> get data => _data;
List<int> get metadata => _metadata;

EncryptedData._(this._data, this._metadata);

factory EncryptedData.from(data, metadata) => EncryptedData._(data, metadata);
}
39 changes: 39 additions & 0 deletions pubnub/lib/src/crypto/aesCbcCryptor.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:encrypt/encrypt.dart' as crypto;
import 'package:crypto/crypto.dart' show sha256;
import 'dart:typed_data' show Uint8List;

import 'package:pubnub/core.dart';
class AesCbcCryptor implements ICryptor {
static const BLOCK_SIZE = 16;
CipherKey cipherKey;

AesCbcCryptor(this.cipherKey);
@override
List<int> decrypt(EncryptedData encryptedData) {
var encrypter =
crypto.Encrypter(crypto.AES(_getKey(), mode: crypto.AESMode.cbc));
return encrypter.decryptBytes(
crypto.Encrypted(Uint8List.fromList(encryptedData.data.toList())),
iv: crypto.IV(Uint8List.fromList(encryptedData.metadata.toList())));
}

@override
EncryptedData encrypt(List<int> input) {
var encrypter =
crypto.Encrypter(crypto.AES(_getKey(), mode: crypto.AESMode.cbc));
var iv = _getIv();
var data = encrypter.encryptBytes(input, iv: iv).bytes.toList();
return EncryptedData.from(data, iv.bytes.toList());
}

@override
String get identifier => 'ACRH';

crypto.IV _getIv() {
return crypto.IV.fromSecureRandom(BLOCK_SIZE);
}

crypto.Key _getKey() {
return crypto.Key.fromBase16(sha256.convert(cipherKey.data).toString());
}
}
13 changes: 12 additions & 1 deletion pubnub/test/unit/crypto/crypto_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'dart:convert';

import 'package:pubnub/core.dart';
import 'package:pubnub/src/crypto/aesCbcCryptor.dart';
import 'package:pubnub/src/crypto/crypto.dart';

import 'package:test/test.dart';
Expand Down Expand Up @@ -28,8 +31,16 @@ void main() {
var headerData = [80, 78, 69, 68, 1, 65, 67, 82, 72, 16];
var expectedBytes = [...headerData, ...List<int>.filled(16, 0)];

CryptorHeaderV1? header = CryptorHeader.tryParse(encryptedDataWithHeader.codeUnits);
var header = CryptorHeader.tryParse(encryptedDataWithHeader.codeUnits);
expect(header!.data, equals(expectedBytes));
});

test('AesCbcCryptor should work as expected', () async {
var plainText = 'Hello there!';
var cryptor = AesCbcCryptor(CipherKey.fromUtf8('pubnubenigma'));
var encryptedData = cryptor.encrypt(plainText.codeUnits);
var decrypted = cryptor.decrypt(encryptedData);
expect(utf8.decode(decrypted), equals(plainText));
});
});
}

0 comments on commit 430326d

Please sign in to comment.