Skip to content

Commit cb95421

Browse files
committed
ES256K Support added (experimental)
1 parent 7e0f00a commit cb95421

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

ES256K.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2019 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Component\Signature\Algorithm;
15+
16+
final class ES256K extends ECDSA
17+
{
18+
public function name(): string
19+
{
20+
return 'ES256K';
21+
}
22+
23+
protected function getHashAlgorithm(): string
24+
{
25+
return 'sha256';
26+
}
27+
28+
protected function getSignaturePartLength(): int
29+
{
30+
return 64;
31+
}
32+
}

Tests/P256KSignatureTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* The MIT License (MIT)
7+
*
8+
* Copyright (c) 2014-2019 Spomky-Labs
9+
*
10+
* This software may be modified and distributed under the terms
11+
* of the MIT license. See the LICENSE file for details.
12+
*/
13+
14+
namespace Jose\Component\Signature\Algorithm\Signature;
15+
16+
use Base64Url\Base64Url;
17+
use Jose\Component\Core\JWK;
18+
use Jose\Component\Signature\Algorithm\ES256K;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @group unit
23+
* @group NewAlgorithm
24+
*
25+
* @covers \Jose\Component\Signature\Algorithm\ES256K
26+
*
27+
* @internal
28+
*/
29+
class P256KSignatureTest extends TestCase
30+
{
31+
/**
32+
* @test
33+
*/
34+
public function es256KVerify()
35+
{
36+
$key = $this->getKey();
37+
$algorithm = new ES256K();
38+
$data = 'Hello';
39+
40+
static::assertTrue($algorithm->verify($key, $data, hex2bin('9c75b9d171d9690a37f2474d4bfab5c234911cb150950ea5cbfc9aedda5ec360725cc47978de95b4efb2a3ed617c7b36b1cd0a26b536662a79d0f3ae873a7924')));
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function es256KSignAndVerify()
47+
{
48+
$key = $this->getKey();
49+
$algorithm = new ES256K();
50+
$data = 'Hello';
51+
52+
static::assertEquals('ES256K', $algorithm->name());
53+
54+
$signature = $algorithm->sign($key, $data);
55+
56+
static::assertTrue($algorithm->verify($key, $data, $signature));
57+
}
58+
59+
private function getKey(): JWK
60+
{
61+
return new JWK([
62+
'kty' => 'EC',
63+
'crv' => 'P-256K',
64+
'd' => Base64Url::encode(hex2bin('D1592A94BBB9B5D94CDC425FC7DA80B6A47863AE973A9D581FD9D8F29690B659')),
65+
'x' => Base64Url::encode(hex2bin('4B4DF318DE05BB8F3A115BF337F9BCBC55CA14B917B46BCB557D3C9A158D4BE0')),
66+
'y' => Base64Url::encode(hex2bin('627EB75731A8BBEBC7D9A3C57EC4D7DA2CBA6D2A28E7F45134921861FE1CF5D9')),
67+
]);
68+
}
69+
}

0 commit comments

Comments
 (0)