Completed
Push — master ( 1a59c2...c73113 )
by Florent
02:55
created

Ed25519::verify()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Algorithm\Signature;
13
14
use Assert\Assertion;
15
use Base64Url\Base64Url;
16
use Jose\Algorithm\SignatureAlgorithmInterface;
17
use Jose\Object\JWKInterface;
18
19
/**
20
 * Class Ed25519.
21
 */
22
class Ed25519 implements SignatureAlgorithmInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function sign(JWKInterface $key, $data)
28
    {
29
        $this->checkKey($key);
30
        Assertion::true($key->has('d'), 'The key is not private');
31
32
        $secret = Base64Url::decode($key->get('d'));
33
        $public = Base64Url::decode($key->get('x'));
34
35
        $signature = ed25519_sign($data, $secret, $public);
36
37
        return $signature;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function verify(JWKInterface $key, $data, $signature)
44
    {
45
        $this->checkKey($key);
46
47
        $public = Base64Url::decode($key->get('x'));
48
49
        return ed25519_sign_open($data, $public, $signature);
50
    }
51
52
    /**
53
     * @param JWKInterface $key
54
     */
55
    private function checkKey(JWKInterface $key)
56
    {
57
        Assertion::eq($key->get('kty'), 'OKP', 'Wrong key type.');
58
        Assertion::true($key->has('x'), 'The key parameter "x" is missing.');
59
        Assertion::true($key->has('crv'), 'The key parameter "crv" is missing.');
60
        Assertion::eq($key->get('crv'), 'Ed25519', 'Unsupported curve');
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getAlgorithmName()
67
    {
68
        return 'Ed25519';
69
    }
70
}
71