Skip to content

Commit

Permalink
Properly left-pad
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Jul 31, 2021
1 parent 9dc9aa5 commit 3902872
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ECDSA/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Mdanter\Ecc\Crypto\Signature\SignatureInterface;
use Mdanter\Ecc\Exception\SignatureDecodeException;
use Mdanter\Ecc\Util\BinaryString;
use ParagonIE\ConstantTime\Binary;

/**
* Class Signature
Expand All @@ -23,7 +24,7 @@ public function toString(int $length = 0): string
{
$r = gmp_strval($this->getR(), 16);
$s = gmp_strval($this->getS(), 16);
$len = max(strlen($r), strlen($s), $length >> 1);
$len = max(Binary::safeStrlen($r), Binary::safeStrlen($s), $length);
return str_pad($r, $len, '0', STR_PAD_LEFT) .
str_pad($s, $len, '0', STR_PAD_LEFT);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/K256Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@ public function testSign()
$sig = $this->ecc->sign($message, $sk);
$this->assertTrue($this->ecc->verify($message, $pk, $sig));
$this->assertFalse($this->ecc->verify('samplf', $pk, $sig));

$sig = $this->ecc->sign($message, $sk, true);
$this->assertTrue($this->ecc->verify($message, $pk, $sig, true));
}
}
3 changes: 3 additions & 0 deletions tests/P256Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@ public function testSign()
$sig = $this->ecc->sign($message, $sk);
$this->assertTrue($this->ecc->verify($message, $pk, $sig));
$this->assertFalse($this->ecc->verify('samplf', $pk, $sig));

$sig = $this->ecc->sign($message, $sk, true);
$this->assertTrue($this->ecc->verify($message, $pk, $sig, true));
}
}
3 changes: 3 additions & 0 deletions tests/P384Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@ public function testSign()
$sig = $this->ecc->sign($message, $sk);
$this->assertTrue($this->ecc->verify($message, $pk, $sig));
$this->assertFalse($this->ecc->verify('samplf', $pk, $sig));

$sig = $this->ecc->sign($message, $sk, true);
$this->assertTrue($this->ecc->verify($message, $pk, $sig, true));
}
}

0 comments on commit 3902872

Please sign in to comment.