-
Notifications
You must be signed in to change notification settings - Fork 11
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 #708 from atsign-foundation/604-support-password-p…
…rotected-atKeys-at-chops feat: at_chops : Support for password protected atKeys file
- Loading branch information
Showing
16 changed files
with
493 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
library at_chops; | ||
|
||
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'; | ||
|
||
// 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/impl/aes_key.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'; | ||
|
||
// 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'; | ||
export 'src/algorithm/algo_type.dart'; | ||
export 'src/algorithm/at_iv.dart'; | ||
export 'src/algorithm/aes_encryption_algo.dart'; | ||
export 'src/algorithm/rsa_encryption_algo.dart'; | ||
export 'src/algorithm/default_signing_algo.dart'; | ||
export 'src/algorithm/pkam_signing_algo.dart'; | ||
export 'src/algorithm/ecc_signing_algo.dart'; | ||
export 'src/key/at_key_pair.dart'; | ||
export 'src/key/at_public_key.dart'; | ||
export 'src/key/at_private_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
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,4 +1,17 @@ | ||
// ignore: constant_identifier_names | ||
import 'package:at_commons/at_commons.dart'; | ||
|
||
enum SigningAlgoType { ecc_secp256r1, rsa2048, rsa4096 } | ||
|
||
enum HashingAlgoType { sha256, sha512, md5 } | ||
enum HashingAlgoType { | ||
sha256, | ||
sha512, | ||
md5, | ||
argon2id; | ||
|
||
static HashingAlgoType fromString(String name) { | ||
return HashingAlgoType.values.firstWhere( | ||
(algo) => algo.name == name.toLowerCase(), | ||
orElse: () => throw AtException('Invalid hashing algo type')); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/at_chops/lib/src/algorithm/argon2id_hashing_algo.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,52 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:at_chops/src/algorithm/at_algorithm.dart'; | ||
import 'package:at_chops/src/model/hash_params.dart'; | ||
import 'package:cryptography/cryptography.dart'; | ||
|
||
/// A class that implements the Argon2id hashing algorithm for password hashing. | ||
/// | ||
/// This class provides a method to hash a given password using the Argon2id | ||
/// algorithm, which is a memory-hard, CPU-intensive key derivation function | ||
/// suitable for password hashing and encryption key derivation. | ||
/// | ||
/// The class uses the `cryptography` package's `Argon2id` algorithm for deriving | ||
/// a key from a password and encodes the result into a Base64 string. | ||
class Argon2idHashingAlgo implements AtHashingAlgorithm<String, String> { | ||
/// Hashes a given password using the Argon2id algorithm. | ||
/// | ||
/// The [password] parameter is required, and it represents the password or | ||
/// passphrase to be hashed. | ||
/// | ||
/// The [hashParams] parameter is optional. It allows customizing the Argon2id | ||
/// parameters, such as: | ||
/// - [HashParams.parallelism]: The degree of parallelism (threads) to use. | ||
/// - [HashParams.memory]: The amount of memory (in KB) to use. | ||
/// - [HashParams.iterations]: The number of iterations (time cost) to apply. | ||
/// - [HashParams.hashLength]: The length of the resulting hash (in bytes). | ||
/// | ||
/// If [hashParams] is not provided, default values will be used. | ||
/// | ||
/// The method returns a [Future] that resolves to a Base64-encoded string | ||
/// representing the hashed value of the input password. | ||
/// | ||
/// Throws: | ||
/// - [ArgumentError] if the provided password is null or empty. | ||
/// | ||
/// Returns a Base64-encoded string representing the derived key. | ||
@override | ||
Future<String> hash(String password, {ArgonHashParams? hashParams}) async { | ||
hashParams ??= ArgonHashParams(); | ||
final argon2id = Argon2id( | ||
parallelism: hashParams.parallelism, | ||
memory: hashParams.memory, | ||
iterations: hashParams.iterations, | ||
hashLength: hashParams.hashLength); | ||
|
||
SecretKey secretKey = await argon2id.deriveKeyFromPassword( | ||
password: password, nonce: password.codeUnits); | ||
|
||
return Base64Encoder().convert(await secretKey.extractBytes()); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
packages/at_chops/lib/src/algorithm/at_hashing_algo_factory.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,29 @@ | ||
import 'package:at_chops/src/algorithm/algo_type.dart'; | ||
import 'package:at_chops/src/algorithm/argon2id_hashing_algo.dart'; | ||
import 'package:at_chops/src/algorithm/at_algorithm.dart'; | ||
import 'package:at_chops/src/algorithm/default_hashing_algo.dart'; | ||
import 'package:at_commons/at_commons.dart'; | ||
|
||
/// A factory class for creating instances of different hashing algorithms | ||
/// based on the specified [HashingAlgoType]. | ||
/// | ||
/// The [AtHashingAlgorithmFactory] class provides a static method | ||
/// [getHashingAlgorithm] which returns the appropriate hashing algorithm | ||
/// implementation corresponding to the provided [HashingAlgoType]. | ||
class AtHashingAlgorithmFactory { | ||
/// Returns an instance of [AtHashingAlgorithm] based on the provided [HashingAlgoType]. | ||
/// | ||
/// The method supports the following hashing algorithms: | ||
/// - [HashingAlgoType.md5]: returns an instance of [DefaultHash] (MD5 hashing). | ||
/// - [HashingAlgoType.argon2id]: returns an instance of [Argon2idHashingAlgo] (Argon2id hashing). | ||
/// | ||
/// Throws an [AtException] if an unsupported hashing algorithm is passed. | ||
static AtHashingAlgorithm getHashingAlgorithm(HashingAlgoType algoType) { | ||
switch (algoType) { | ||
case HashingAlgoType.argon2id: | ||
return Argon2idHashingAlgo(); | ||
default: | ||
throw AtException('Unsupported hashing algorithm'); | ||
} | ||
} | ||
} |
5 changes: 3 additions & 2 deletions
5
packages/at_chops/lib/src/algorithm/default_hashing_algo.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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import 'package:at_chops/src/algorithm/at_algorithm.dart'; | ||
import 'package:at_chops/src/model/hash_params.dart'; | ||
import 'package:crypto/crypto.dart'; | ||
|
||
class DefaultHash implements AtHashingAlgorithm { | ||
class DefaultHash implements AtHashingAlgorithm<List<int>, String> { | ||
@override | ||
String hash(List<int> data) { | ||
String hash(List<int> data, {HashParams? hashParams}) { | ||
return md5.convert(data).toString(); | ||
} | ||
} |
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.