Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <nguyennv1981@gmail.com>
  • Loading branch information
nguyennv committed Nov 5, 2024
1 parent 76af13a commit dbefd70
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
21 changes: 21 additions & 0 deletions src/Enum/KeyAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace OpenPGP\Enum;

use OpenPGP\Common\Config;

/**
* Key algorithm enum
*
Expand Down Expand Up @@ -139,4 +141,23 @@ public function forEncryption(): bool
default => true,
};
}

/**
* Get key version
*
* @return int
*/
public function keyVersion(): int
{
return match ($this) {
self::X25519,
self::X448,
self::Ed25519,
self::Ed448
=> KeyVersion::V6->value,
default => Config::useV6Key()
? KeyVersion::V6->value
: KeyVersion::V4->value,
};
}
}
12 changes: 1 addition & 11 deletions src/Packet/SecretKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,9 @@ public static function generate(
$rsaKeySize,
$curve
);
$version = match ($keyAlgorithm) {
KeyAlgorithm::X25519,
KeyAlgorithm::X448,
KeyAlgorithm::Ed25519,
KeyAlgorithm::Ed448
=> KeyVersion::V6->value,
default => Config::useV6Key()
? KeyVersion::V6->value
: KeyVersion::V4->value,
};
return new self(
new PublicKey(
$version,
$keyAlgorithm->keyVersion(),
$time ?? new \DateTime(),
$keyAlgorithm,
$keyMaterial->getPublicMaterial()
Expand Down
13 changes: 1 addition & 12 deletions src/Packet/SecretSubkey.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
AeadAlgorithm,
Ecc,
KeyAlgorithm,
KeyVersion,
RSAKeySize,
S2kUsage,
SymmetricAlgorithm
Expand Down Expand Up @@ -112,19 +111,9 @@ public static function generate(
$keyMaterial = self::generateKeyMaterial(
$keyAlgorithm, $rsaKeySize, $curve
);
$version = match ($keyAlgorithm) {
KeyAlgorithm::X25519,
KeyAlgorithm::X448,
KeyAlgorithm::Ed25519,
KeyAlgorithm::Ed448
=> KeyVersion::V6->value,
default => Config::useV6Key()
? KeyVersion::V6->value
: KeyVersion::V4->value,
};
return new self(
new PublicSubkey(
$version,
$keyAlgorithm->keyVersion(),
$time ?? new \DateTime(),
$keyAlgorithm,
$keyMaterial->getPublicMaterial()
Expand Down

0 comments on commit dbefd70

Please sign in to comment.