Failed Conditions
Push — v7 ( 2dbae0...d25f5c )
by Florent
01:59
created

Hash::sha384()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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
final class Hash
17
{
18
    /**
19
     * Hash Parameter.
20
     *
21
     * @var string
22
     */
23
    private $hash;
24
25
    /**
26
     * Hash Length.
27
     *
28
     * @var int
29
     */
30
    private $length;
31
32
    /**
33
     * @return Hash
34
     */
35
    public static function sha1(): Hash
36
    {
37
        return new self('sha1', 20);
38
    }
39
40
    /**
41
     * @return Hash
42
     */
43
    public static function sha256(): Hash
44
    {
45
        return new self('sha256', 32);
46
    }
47
48
    /**
49
     * @return Hash
50
     */
51
    public static function sha384(): Hash
52
    {
53
        return new self('sha384', 48);
54
    }
55
56
    /**
57
     * @return Hash
58
     */
59
    public static function sha512(): Hash
60
    {
61
        return new self('sha512', 64);
62
    }
63
64
    /**
65
     * @param string $hash
66
     * @param int    $length
67
     */
68
    private function __construct(string $hash, int $length)
69
    {
70
        $this->hash = $hash;
71
        $this->length = $length;
72
    }
73
74
    /**
75
     * @return int
76
     */
77
    public function getLength(): int
78
    {
79
        return $this->length;
80
    }
81
82
    /**
83
     * Compute the HMAC.
84
     *
85
     * @param string $text
86
     *
87
     * @return string
88
     */
89
    public function hash(string $text): string
90
    {
91
        return hash($this->hash, $text, true);
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function name(): string
98
    {
99
        return $this->hash;
100
    }
101
}
102