Hash   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 0
dl 0
loc 85
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A sha1() 0 4 1
A sha256() 0 4 1
A sha384() 0 4 1
A sha512() 0 4 1
A getLength() 0 4 1
A hash() 0 4 1
A name() 0 4 1
A t() 0 4 1
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\Core\Util;
15
16
/**
17
 * @internal
18
 */
19
class Hash
20
{
21
    /**
22
     * Hash Parameter.
23
     *
24
     * @var string
25
     */
26
    private $hash;
27
28
    /**
29
     * DER encoding T.
30
     *
31
     * @var string
32
     */
33
    private $t;
34
35
    /**
36
     * Hash Length.
37
     *
38
     * @var int
39
     */
40
    private $length;
41
42
    private function __construct(string $hash, int $length, string $t)
43
    {
44
        $this->hash = $hash;
45
        $this->length = $length;
46
        $this->t = $t;
47
    }
48
49
    /**
50
     * @return Hash
51
     */
52
    public static function sha1(): self
53
    {
54
        return new self('sha1', 20, "\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14");
55
    }
56
57
    /**
58
     * @return Hash
59
     */
60
    public static function sha256(): self
61
    {
62
        return new self('sha256', 32, "\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20");
63
    }
64
65
    /**
66
     * @return Hash
67
     */
68
    public static function sha384(): self
69
    {
70
        return new self('sha384', 48, "\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30");
71
    }
72
73
    /**
74
     * @return Hash
75
     */
76
    public static function sha512(): self
77
    {
78
        return new self('sha512', 64, "\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40");
79
    }
80
81
    public function getLength(): int
82
    {
83
        return $this->length;
84
    }
85
86
    /**
87
     * Compute the HMAC.
88
     */
89
    public function hash(string $text): string
90
    {
91
        return hash($this->hash, $text, true);
92
    }
93
94
    public function name(): string
95
    {
96
        return $this->hash;
97
    }
98
99
    public function t(): string
100
    {
101
        return $this->t;
102
    }
103
}
104