/** * @param $base64 * @return array */ public function unserialize($base64) { $binary = base64_decode($base64, true); if ($binary === false) { throw new \InvalidArgumentException('Invalid base64'); } $values = []; $pos = 0; $end = strlen($binary); for ($i = 0; $i < 3; $i++) { if ($end - $pos < 4) { throw new \RuntimeException('Length marker too short'); } $length = unpack("N", substr($binary, $pos, 4))[1]; $pos += 4; if ($end - $pos < $length) { throw new \RuntimeException('Not enough data'); } $value = substr($binary, $pos, $length); $pos += $length; $values[$i] = $value; } $curveName = $values[1]; $pointHex = unpack("H*", $values[2])[1]; $curve = Curves::curve($curveName); $generator = Curves::generator($curveName); $point = $this->pointSerializer->unserialize($curve, $pointHex); $publicKey = new PublicKey($this->math, $generator, $point); return [$curve, $publicKey]; }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage Unknown or unsupported curve */ public function testRejectsInvalidCurve() { Curves::curve('not-a-curve'); }