• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / crypto / 8075159324

16 Jan 2024 08:05PM UTC coverage: 100.0%. Remained the same
8075159324

push

github

natanfelles
Update issues link

136 of 136 relevant lines covered (100.0%)

1.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/GenericHash.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Crypto Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Crypto;
11

12
use LengthException;
13
use RangeException;
14
use SensitiveParameter;
15
use SodiumException;
16

17
/**
18
 * Class GenericHash.
19
 *
20
 * @package crypto
21
 */
22
class GenericHash
23
{
24
    protected string $key;
25
    protected int $hashLength = \SODIUM_CRYPTO_GENERICHASH_BYTES;
26

27
    /**
28
     * GenericHash constructor.
29
     *
30
     * @param string $key
31
     * @param int $hashLength
32
     *
33
     * @see GenericHash::makeKey()
34
     *
35
     * @throws LengthException if key length is not between 16 and 64
36
     * @throws RangeException if the hashLength value is not in the range 16 to 64
37
     */
38
    public function __construct(
39
        #[SensitiveParameter]
40
        string $key,
41
        int $hashLength = \SODIUM_CRYPTO_GENERICHASH_BYTES
42
    ) {
43
        $this->validateKey($key);
3✔
44
        $this->validateHashLength($hashLength);
2✔
45
        $this->key = $key;
1✔
46
        $this->hashLength = $hashLength;
1✔
47
    }
48

49
    /**
50
     * Validates a key.
51
     *
52
     * @param string $key
53
     *
54
     * @throws LengthException if key length is not between 16 and 64
55
     */
56
    protected function validateKey(#[SensitiveParameter] string $key) : void
57
    {
58
        $length = \mb_strlen($key, '8bit');
3✔
59
        if ($length < \SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN
3✔
60
            || $length > \SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX
3✔
61
        ) {
62
            throw new LengthException(
1✔
63
                'GenericHash key must have a length between '
1✔
64
                . \SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN . ' and '
1✔
65
                . \SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX . ', '
1✔
66
                . $length . ' given'
1✔
67
            );
1✔
68
        }
69
    }
70

71
    /**
72
     * Validates a hash length.
73
     *
74
     * @param int $length
75
     *
76
     * @throws RangeException if the length value is not in the range 16 to 64
77
     */
78
    protected function validateHashLength(int $length) : void
79
    {
80
        if ($length < \SODIUM_CRYPTO_GENERICHASH_BYTES_MIN
2✔
81
            || $length > \SODIUM_CRYPTO_GENERICHASH_BYTES_MAX
2✔
82
        ) {
83
            throw new RangeException(
2✔
84
                'Hash length must be a value between ' . \SODIUM_CRYPTO_GENERICHASH_BYTES_MIN
2✔
85
                . ' and ' . \SODIUM_CRYPTO_GENERICHASH_BYTES_MAX . ', '
2✔
86
                . $length . ' given'
2✔
87
            );
2✔
88
        }
89
    }
90

91
    /**
92
     * Gets a message signature.
93
     *
94
     * @param string $message
95
     * @param int|null $hashLength A custom hash length or null to use the length set in
96
     * the constructor
97
     *
98
     * @throws RangeException if the hashLength is set and is not in the range 16 to 64
99
     * @throws SodiumException
100
     *
101
     * @return string
102
     */
103
    public function signature(
104
        #[SensitiveParameter]
105
        string $message,
106
        int $hashLength = null
107
    ) : string {
108
        return Utils::bin2base64(
1✔
109
            $this->makeHash($message, $hashLength),
1✔
110
            \SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING
1✔
111
        );
1✔
112
    }
113

114
    /**
115
     * Verifies if a message matches a signature.
116
     *
117
     * @param string $message
118
     * @param string $signature
119
     * @param int|null $hashLength A custom hash length or null to use the length set in
120
     * the constructor
121
     *
122
     * @throws RangeException if the hashLength is set and is not in the range 16 to 64
123
     * @throws SodiumException
124
     *
125
     * @return bool
126
     */
127
    public function verify(
128
        #[SensitiveParameter]
129
        string $message,
130
        #[SensitiveParameter]
131
        string $signature,
132
        int $hashLength = null
133
    ) : bool {
134
        return \hash_equals(
1✔
135
            $this->makeHash($message, $hashLength),
1✔
136
            Utils::base642bin($signature, \SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING)
1✔
137
        );
1✔
138
    }
139

140
    /**
141
     * Makes a hash to a message.
142
     *
143
     * @param string $message
144
     * @param int|null $length A custom length or null to use the length set in
145
     * the constructor
146
     *
147
     * @throws RangeException if the length is set and is not in the range 16 to 64
148
     * @throws SodiumException
149
     *
150
     * @return string
151
     */
152
    protected function makeHash(#[SensitiveParameter] string $message, int $length = null) : string
153
    {
154
        if ($length !== null) {
1✔
155
            $this->validateHashLength($length);
1✔
156
        }
157
        return \sodium_crypto_generichash(
1✔
158
            $message,
1✔
159
            $this->key,
1✔
160
            $length ?? $this->hashLength
1✔
161
        );
1✔
162
    }
163

164
    /**
165
     * Makes a key.
166
     *
167
     * @return string
168
     */
169
    public static function makeKey() : string
170
    {
171
        return \sodium_crypto_generichash_keygen();
3✔
172
    }
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc