Skip to content

Commit

Permalink
Refactor generate secret key
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 Oct 10, 2024
1 parent 3e97b77 commit d1055c4
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/Enum/MontgomeryCurve.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,22 @@ public function hkdfInfo(): string
public function generateSecretKey(): string
{
$size = $this->payloadSize();
$secret = Random::string($size);
if ($this === self::Curve25519) {
/// The lowest three bits must be 0
$secret[0] = $secret[0] & "\xf8";
// The highest bit must be 0 & the second highest bit must be 1
$secret[$size - 1] = ($secret[$size - 1] & "\x7f") | "\x40";
}
else {
// The two least significant bits of the first byte to 0
$secret[0] = $secret[0] & "\xfc";
// The most significant bit of the last byte to 1
$secret[$size - 1] = $secret[$size - 1] | "\x80";
}
do {
$secret = Random::string($size);
if ($this === self::Curve25519) {
/// The lowest three bits must be 0
$secret[0] = $secret[0] & "\xf8";
// The highest bit must be 0 & the second highest bit must be 1
$secret[$size - 1] = ($secret[$size - 1] & "\x7f") | "\x40";
}
else {
// The two least significant bits of the first byte to 0
$secret[0] = $secret[0] & "\xfc";
// The most significant bit of the last byte to 1
$secret[$size - 1] = $secret[$size - 1] | "\x80";
}
$d = Helper::bin2BigInt($secret);
} while ($d->getLengthInBytes() !== $size);
return $secret;
}
}

0 comments on commit d1055c4

Please sign in to comment.