Skip to content

Commit

Permalink
chore: reorganize at_chops
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierChanth committed Feb 18, 2025
1 parent 2de26f5 commit 19a75e4
Show file tree
Hide file tree
Showing 70 changed files with 493 additions and 500 deletions.
18 changes: 10 additions & 8 deletions packages/at_chops/example/at_chops_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:at_chops/at_chops.dart';
import 'package:at_chops/src/algorithm/at_algorithm.dart';
import 'package:at_chops/src/key/at_rsa_key_pair.dart';
import 'package:at_chops/types.dart';
import 'package:encrypt/encrypt.dart';

/// Usage:
/// Using new key pairs created at runtime
/// dart run at_chops_example.dart
/// or
/// Using key pairs from atKeys file
/// dart run at_chops_example.dart <path_to_atkeys_file>
/// dart run at_chops_example.dart (path_to_atkeys_file)
void main(List<String> args) async {
AtChops atChops;
if (args.isNotEmpty) {
Expand All @@ -36,7 +37,8 @@ void main(List<String> args) async {
atChops = AtChopsImpl(atChopsKeys);
}

var atEncryptionKeyPair = atChops.atChopsKeys.atEncryptionKeyPair;
var atEncryptionKeyPair =
atChops.atChopsKeys.atEncryptionKeyPair as AtRSAKeyPair?;
// 1 - Encryption and decryption using asymmetric key pair
final data = 'Hello World';
//1.1 encrypt the data using [atEncryptionKeyPair.publicKey]
Expand All @@ -56,7 +58,7 @@ void main(List<String> args) async {
signingInput.signingAlgoType = SigningAlgoType.rsa2048;
signingInput.hashingAlgoType = HashingAlgoType.sha512;
AtSigningAlgorithm signingAlgorithm =
DefaultSigningAlgo(atEncryptionKeyPair, signingInput.hashingAlgoType);
RSASigningAlgo(atEncryptionKeyPair, signingInput.hashingAlgoType);
signingInput.signingAlgorithm = signingAlgorithm;
// 2.2 sign the data
final dataSigningResult = atChops.sign(signingInput);
Expand All @@ -68,8 +70,8 @@ void main(List<String> args) async {
atEncryptionKeyPair!.atPublicKey.publicKey);
verificationInput.signingAlgoType = SigningAlgoType.rsa2048;
verificationInput.hashingAlgoType = HashingAlgoType.sha512;
AtSigningAlgorithm verifyAlgorithm = DefaultSigningAlgo(
atEncryptionKeyPair, verificationInput.hashingAlgoType);
AtSigningAlgorithm verifyAlgorithm =
RSASigningAlgo(atEncryptionKeyPair, verificationInput.hashingAlgoType);
verificationInput.signingAlgorithm = verifyAlgorithm;
// 2.4 verify the signature
AtSigningResult dataVerificationResult = atChops.verify(verificationInput);
Expand All @@ -78,12 +80,12 @@ void main(List<String> args) async {
}

AtChops _createAtChops(Map<String, String> atKeysDataMap) {
final atEncryptionKeyPair = AtEncryptionKeyPair.create(
final atEncryptionKeyPair = AtRSAKeyPair.create(
_decryptValue(atKeysDataMap[AuthKeyType.encryptionPublicKey]!,
atKeysDataMap[AuthKeyType.selfEncryptionKey]!)!,
_decryptValue(atKeysDataMap[AuthKeyType.encryptionPrivateKey]!,
atKeysDataMap[AuthKeyType.selfEncryptionKey]!)!);
final atPkamKeyPair = AtPkamKeyPair.create(
final atPkamKeyPair = AtRSAKeyPair.create(
_decryptValue(atKeysDataMap[AuthKeyType.pkamPublicKey]!,
atKeysDataMap[AuthKeyType.selfEncryptionKey]!)!,
_decryptValue(atKeysDataMap[AuthKeyType.pkamPrivateKey]!,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:at_chops/types.dart';
import 'package:crypto/crypto.dart';
import 'package:at_chops/at_chops.dart';

Expand Down
44 changes: 13 additions & 31 deletions packages/at_chops/lib/at_chops.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
library at_chops;
// export cryptography wrappers
export 'src/hashing/hashing.dart';
export 'src/encryption/encryption.dart';
export 'src/signing/signing.dart';
export 'src/padding/padding.dart';

export 'src/algorithm/aes_encryption_algo.dart';
export 'src/algorithm/algo_type.dart';
export 'src/algorithm/at_iv.dart';
export 'src/algorithm/default_signing_algo.dart';
export 'src/algorithm/ecc_signing_algo.dart';
export 'src/algorithm/pkam_signing_algo.dart';
export 'src/algorithm/rsa_encryption_algo.dart';
export 'src/at_chops_base.dart';
export 'src/at_chops_impl.dart';
// export inteface types
export 'src/key/key.dart';
export 'src/model/model.dart';

// Class to encrypt/decrypt atKeys file based on the password specified.
export 'src/at_keys_crypto.dart';
export 'src/key/at_key_pair.dart';
export 'src/key/at_private_key.dart';
export 'src/key/at_public_key.dart';
export 'src/key/impl/aes_key.dart';
export 'src/key/impl/at_chops_keys.dart';
export 'src/key/impl/at_encryption_key_pair.dart';
export 'src/key/impl/at_pkam_key_pair.dart';
export 'src/key/key_type.dart';
export 'src/metadata/at_signing_input.dart';
export 'src/metadata/encryption_metadata.dart';
export 'src/metadata/encryption_result.dart';
export 'src/metadata/signing_metadata.dart';
export 'src/metadata/signing_result.dart';

// A model class which represents the encrypted AtKeys with a passphrase.
export 'src/model/at_encrypted.dart';
// export at_platform cryptography wrappers
export 'src/at_platform/at_platform.dart';

// Class representing the hashing parameters to pass to an hashing algorithm.
export 'src/model/hash_params.dart' hide HashParams;
export 'src/util/at_chops_util.dart';
// misc
export 'src/at_chops_util.dart';
export 'src/at_keys_crypto.dart';
17 changes: 0 additions & 17 deletions packages/at_chops/lib/src/algorithm/algo_type.dart

This file was deleted.

18 changes: 0 additions & 18 deletions packages/at_chops/lib/src/algorithm/default_hashing_algo.dart

This file was deleted.

13 changes: 0 additions & 13 deletions packages/at_chops/lib/src/algorithm/padding/padding_params.dart

This file was deleted.

56 changes: 0 additions & 56 deletions packages/at_chops/lib/src/algorithm/pkam_signing_algo.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import 'dart:typed_data';

import 'package:at_chops/src/algorithm/at_iv.dart';
import 'package:at_chops/src/key/at_key_pair.dart';
import 'package:at_chops/src/key/impl/aes_key.dart';
import 'package:at_chops/src/key/impl/at_encryption_key_pair.dart';
import 'package:at_chops/src/key/impl/at_pkam_key_pair.dart';
import 'package:at_chops/src/key/key_type.dart';
import 'package:at_chops/src/encryption/initalisation_vector.dart';
import 'package:at_chops/src/key/at_rsa_key_pair.dart';
import 'package:at_chops/src/key/key.dart';
import 'package:better_cryptography/better_cryptography.dart';
import 'package:crypton/crypton.dart';
import 'package:encrypt/encrypt.dart';
Expand All @@ -18,11 +15,13 @@ class AtChopsUtil {
return InitialisationVector(iv.bytes);
}

/// DO NOT USE THIS IF YOU ARE IMPLEMENTING NEW FEATURES
@Deprecated("Preserved for backwards compatibility")
static InitialisationVector generateIVLegacy() {
return InitialisationVector(IV(Uint8List(16)).bytes);
}

static InitialisationVector generateIVFromBase64String(String ivBase64) {
static InitialisationVector ivFromBase64(String ivBase64) {
final iv = IV.fromBase64(ivBase64);
return InitialisationVector(iv.bytes);
}
Expand All @@ -33,16 +32,16 @@ class AtChopsUtil {
}

/// Generates AtEncryption asymmetric keypair with default size 2048 bits
static AtEncryptionKeyPair generateAtEncryptionKeyPair({int keySize = 2048}) {
static AtRSAKeyPair generateAtEncryptionKeyPair({int keySize = 2048}) {
final rsaKeyPair = RSAKeypair.fromRandom(keySize: keySize);
return AtEncryptionKeyPair.create(
return AtRSAKeyPair.create(
rsaKeyPair.publicKey.toString(), rsaKeyPair.privateKey.toString());
}

/// Generates AtEncryption asymmetric keypair with default size 2048 bits
static AtPkamKeyPair generateAtPkamKeyPair({int keySize = 2048}) {
static AtRSAKeyPair generateAtPkamKeyPair({int keySize = 2048}) {
final rsaKeyPair = RSAKeypair.fromRandom(keySize: keySize);
return AtPkamKeyPair.create(
return AtRSAKeyPair.create(
rsaKeyPair.publicKey.toString(), rsaKeyPair.privateKey.toString());
}

Expand All @@ -60,13 +59,13 @@ class AtChopsUtil {
static SymmetricKey generateSymmetricKey(EncryptionKeyType keyType) {
switch (keyType) {
case EncryptionKeyType.aes128:
return AESKey.generate(16);
return AtAESKey.generate(16);
case EncryptionKeyType.aes192:
return AESKey.generate(24);
return AtAESKey.generate(24);
case EncryptionKeyType.aes256:
return AESKey.generate(32);
return AtAESKey.generate(32);
default:
return AESKey.generate(32);
return AtAESKey.generate(32);
}
}
}
9 changes: 5 additions & 4 deletions packages/at_chops/lib/src/at_keys_crypto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:at_chops/at_chops.dart';
import 'package:at_chops/src/factory/at_hashing_algo_factory.dart';
import 'package:at_chops/src/model/hash_params.dart';
import 'package:at_chops/src/encryption/encryption.dart';
import 'package:at_chops/src/hashing/hashing.dart';
import 'package:at_chops/src/at_chops_util.dart';
import 'package:at_commons/at_commons.dart';

/// An abstract class that provides cryptographic operations for AtKeys using
Expand Down Expand Up @@ -65,7 +66,7 @@ class _AtKeysCryptoImpl implements AtKeysCrypto {
String hashKey =
await _getHashKey(passPhrase, _hashingAlgoType, hashParams: hashParams);

AESKey aesKey = AESKey(hashKey);
AtAESKey aesKey = AtAESKey(hashKey);
StringAESEncryptor atEncryptionAlgorithm = StringAESEncryptor(aesKey);

InitialisationVector iv = AtChopsUtil.generateRandomIV(16);
Expand All @@ -92,7 +93,7 @@ class _AtKeysCryptoImpl implements AtKeysCrypto {
// 1. Generate hash key based on the hashing algo type:
String hashKey =
await _getHashKey(passPhrase, _hashingAlgoType, hashParams: hashParams);
AESKey aesKey = AESKey(hashKey);
AtAESKey aesKey = AtAESKey(hashKey);
StringAESEncryptor atEncryptionAlgorithm = StringAESEncryptor(aesKey);

Uint8List iv = base64Decode(atEncrypted.iv!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import 'dart:async';
import 'dart:typed_data';

import 'package:at_chops/at_chops.dart';
import 'package:at_chops/src/algorithm/at_algorithm.dart';
import 'package:at_chops/src/algorithm/default_hashing_algo.dart';
import 'package:at_chops/src/factory/at_hashing_algo_factory.dart';
import 'package:at_chops/src/model/signing_result.dart';

/// Base class for all Cryptographic and Hashing Operations. Callers have to either implement
/// specific encryption, signing or hashing algorithms. Otherwise default implementation of specific algorithms will be used.
abstract class AtChops {
final AtChopsKeys _atChopsKeys;
AtChopsKeys get atChopsKeys;

AtChopsKeys get atChopsKeys => _atChopsKeys;
const AtChops.init();

AtChops(this._atChopsKeys);
factory AtChops(AtChopsKeys keys) {
return AtChopsImpl(keys);
}

/// Returns an instance of [AtHashingAlgorithm] based on the provided [hashingAlgoType].
///
Expand Down Expand Up @@ -87,7 +87,7 @@ abstract class AtChops {
AtSigningResult verify(AtSigningVerificationInput verifyInput);

/// Create a hash of input [signedData] using a [hashingAlgorithm].
/// Refer to [DefaultHash] for default implementation of hashing.
/// Refer to [Md5HashingAlgo] for default implementation of hashing.
String hash(Uint8List signedData, AtHashingAlgorithm hashingAlgorithm);

/// Reads a public key from a secure element or any other source
Expand Down
Loading

0 comments on commit 19a75e4

Please sign in to comment.