Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Dec 11, 2024
1 parent ee80899 commit 82cd02b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
11 changes: 11 additions & 0 deletions lib/src/enum/symmetric_algorithm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ enum SymmetricAlgorithm {

const SymmetricAlgorithm(this.value);

static List<SymmetricAlgorithm> get preferredSymmetrics => [
aes128,
aes192,
aes256,
camellia128,
camellia192,
camellia256,
blowfish,
twofish,
];

int get keySize => switch (this) {
plaintext => 0,
idea || cast5 || blowfish || aes128 || camellia128 => 128,
Expand Down
16 changes: 6 additions & 10 deletions lib/src/message/literal_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,25 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
final Iterable<KeyInterface> encryptionKeys, [
final SymmetricAlgorithm defaultSymmetric = SymmetricAlgorithm.aes128,
]) {
var desiredSymmetrics = [
SymmetricAlgorithm.aes128,
SymmetricAlgorithm.aes192,
SymmetricAlgorithm.aes256,
];
var preferredSymmetrics = SymmetricAlgorithm.preferredSymmetrics;
for (final key in encryptionKeys) {
desiredSymmetrics = desiredSymmetrics
preferredSymmetrics = preferredSymmetrics
.where(
(symmetric) => key.preferredSymmetrics.contains(symmetric),
)
.toList();
}
final symmetric = desiredSymmetrics.firstOrNull ?? defaultSymmetric;
final symmetric = preferredSymmetrics.firstOrNull ?? defaultSymmetric;

var desiredAeads = [
var preferredAeads = [
AeadAlgorithm.ocb,
AeadAlgorithm.gcm,
AeadAlgorithm.eax,
];
var aeadProtect = Config.aeadProtect;
for (final key in encryptionKeys) {
if (key.aeadSupported) {
desiredAeads = desiredAeads
preferredAeads = preferredAeads
.where(
(aead) => key.preferredAeads(symmetric).contains(aead),
)
Expand All @@ -92,7 +88,7 @@ final class LiteralMessage extends BaseMessage implements LiteralMessageInterfac
break;
}
}
final aead = desiredAeads.firstOrNull ?? Config.preferredAead;
final aead = preferredAeads.firstOrNull ?? Config.preferredAead;

return SessionKey.produceKey(
symmetric,
Expand Down

0 comments on commit 82cd02b

Please sign in to comment.