Subversion Repositories oidplus

Rev

Rev 846 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 846 Rev 874
Line 19... Line 19...
19
 *
19
 *
20
 *    echo base64_encode($hash->hash('abcdefg'));
20
 *    echo base64_encode($hash->hash('abcdefg'));
21
 * ?>
21
 * ?>
22
 * </code>
22
 * </code>
23
 *
23
 *
-
 
24
 * @category  Crypt
-
 
25
 * @package   Hash
24
 * @author    Jim Wigginton <terrafrost@php.net>
26
 * @author    Jim Wigginton <terrafrost@php.net>
25
 * @copyright 2015 Jim Wigginton
27
 * @copyright 2015 Jim Wigginton
26
 * @author    Andreas Fischer <bantu@phpbb.com>
28
 * @author    Andreas Fischer <bantu@phpbb.com>
27
 * @copyright 2015 Andreas Fischer
29
 * @copyright 2015 Andreas Fischer
28
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
30
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
Line 36... Line 38...
36
use phpseclib3\Exception\UnsupportedAlgorithmException;
38
use phpseclib3\Exception\UnsupportedAlgorithmException;
37
use phpseclib3\Math\BigInteger;
39
use phpseclib3\Math\BigInteger;
38
use phpseclib3\Math\PrimeField;
40
use phpseclib3\Math\PrimeField;
39
 
41
 
40
/**
42
/**
-
 
43
 * @package Hash
41
 * @author  Jim Wigginton <terrafrost@php.net>
44
 * @author  Jim Wigginton <terrafrost@php.net>
42
 * @author  Andreas Fischer <bantu@phpbb.com>
45
 * @author  Andreas Fischer <bantu@phpbb.com>
-
 
46
 * @access  public
43
 */
47
 */
44
class Hash
48
class Hash
45
{
49
{
46
    /**
50
    /**
47
     * Padding Types
51
     * Padding Types
48
     *
52
     *
-
 
53
     * @access private
49
     */
54
     */
50
    const PADDING_KECCAK = 1;
55
    const PADDING_KECCAK = 1;
51
 
56
 
52
    /**
57
    /**
53
     * Padding Types
58
     * Padding Types
54
     *
59
     *
-
 
60
     * @access private
55
     */
61
     */
56
    const PADDING_SHA3 = 2;
62
    const PADDING_SHA3 = 2;
57
 
63
 
58
    /**
64
    /**
59
     * Padding Types
65
     * Padding Types
60
     *
66
     *
-
 
67
     * @access private
61
     */
68
     */
62
    const PADDING_SHAKE = 3;
69
    const PADDING_SHAKE = 3;
63
 
70
 
64
    /**
71
    /**
65
     * Padding Type
72
     * Padding Type
66
     *
73
     *
67
     * Only used by SHA3
74
     * Only used by SHA3
68
     *
75
     *
69
     * @var int
76
     * @var int
-
 
77
     * @access private
70
     */
78
     */
71
    private $paddingType = 0;
79
    private $paddingType = 0;
72
 
80
 
73
    /**
81
    /**
74
     * Hash Parameter
82
     * Hash Parameter
75
     *
83
     *
76
     * @see self::setHash()
84
     * @see self::setHash()
77
     * @var int
85
     * @var int
-
 
86
     * @access private
78
     */
87
     */
79
    private $hashParam;
88
    private $hashParam;
80
 
89
 
81
    /**
90
    /**
82
     * Byte-length of hash output (Internal HMAC)
91
     * Byte-length of hash output (Internal HMAC)
83
     *
92
     *
84
     * @see self::setHash()
93
     * @see self::setHash()
85
     * @var int
94
     * @var int
-
 
95
     * @access private
86
     */
96
     */
87
    private $length;
97
    private $length;
88
 
98
 
89
    /**
99
    /**
90
     * Hash Algorithm
100
     * Hash Algorithm
91
     *
101
     *
92
     * @see self::setHash()
102
     * @see self::setHash()
93
     * @var string
103
     * @var string
-
 
104
     * @access private
94
     */
105
     */
95
    private $algo;
106
    private $algo;
96
 
107
 
97
    /**
108
    /**
98
     * Key
109
     * Key
99
     *
110
     *
100
     * @see self::setKey()
111
     * @see self::setKey()
101
     * @var string
112
     * @var string
-
 
113
     * @access private
102
     */
114
     */
103
    private $key = false;
115
    private $key = false;
104
 
116
 
105
    /**
117
    /**
106
     * Nonce
118
     * Nonce
107
     *
119
     *
108
     * @see self::setNonce()
120
     * @see self::setNonce()
109
     * @var string
121
     * @var string
-
 
122
     * @access private
110
     */
123
     */
111
    private $nonce = false;
124
    private $nonce = false;
112
 
125
 
113
    /**
126
    /**
114
     * Hash Parameters
127
     * Hash Parameters
115
     *
128
     *
116
     * @var array
129
     * @var array
-
 
130
     * @access private
117
     */
131
     */
118
    private $parameters = [];
132
    private $parameters = [];
119
 
133
 
120
    /**
134
    /**
121
     * Computed Key
135
     * Computed Key
122
     *
136
     *
123
     * @see self::_computeKey()
137
     * @see self::_computeKey()
124
     * @var string
138
     * @var string
-
 
139
     * @access private
125
     */
140
     */
126
    private $computedKey = false;
141
    private $computedKey = false;
127
 
142
 
128
    /**
143
    /**
129
     * Outer XOR (Internal HMAC)
144
     * Outer XOR (Internal HMAC)
130
     *
145
     *
131
     * Used only for sha512/*
146
     * Used only for sha512/*
132
     *
147
     *
133
     * @see self::hash()
148
     * @see self::hash()
134
     * @var string
149
     * @var string
-
 
150
     * @access private
135
     */
151
     */
136
    private $opad;
152
    private $opad;
137
 
153
 
138
    /**
154
    /**
139
     * Inner XOR (Internal HMAC)
155
     * Inner XOR (Internal HMAC)
140
     *
156
     *
141
     * Used only for sha512/*
157
     * Used only for sha512/*
142
     *
158
     *
143
     * @see self::hash()
159
     * @see self::hash()
144
     * @var string
160
     * @var string
-
 
161
     * @access private
145
     */
162
     */
146
    private $ipad;
163
    private $ipad;
147
 
164
 
148
    /**
165
    /**
149
     * Recompute AES Key
166
     * Recompute AES Key
150
     *
167
     *
151
     * Used only for umac
168
     * Used only for umac
152
     *
169
     *
153
     * @see self::hash()
170
     * @see self::hash()
154
     * @var boolean
171
     * @var boolean
-
 
172
     * @access private
155
     */
173
     */
156
    private $recomputeAESKey;
174
    private $recomputeAESKey;
157
 
175
 
158
    /**
176
    /**
159
     * umac cipher object
177
     * umac cipher object
160
     *
178
     *
161
     * @see self::hash()
179
     * @see self::hash()
162
     * @var \phpseclib3\Crypt\AES
180
     * @var \phpseclib3\Crypt\AES
-
 
181
     * @access private
163
     */
182
     */
164
    private $c;
183
    private $c;
165
 
184
 
166
    /**
185
    /**
167
     * umac pad
186
     * umac pad
168
     *
187
     *
169
     * @see self::hash()
188
     * @see self::hash()
170
     * @var string
189
     * @var string
-
 
190
     * @access private
171
     */
191
     */
172
    private $pad;
192
    private $pad;
173
 
193
 
174
    /**#@+
194
    /**#@+
175
     * UMAC variables
195
     * UMAC variables
Line 189... Line 209...
189
 
209
 
190
    /**
210
    /**
191
     * Default Constructor.
211
     * Default Constructor.
192
     *
212
     *
193
     * @param string $hash
213
     * @param string $hash
-
 
214
     * @access public
194
     */
215
     */
195
    public function __construct($hash = 'sha256')
216
    public function __construct($hash = 'sha256')
196
    {
217
    {
197
        $this->setHash($hash);
218
        $this->setHash($hash);
198
    }
219
    }
Line 200... Line 221...
200
    /**
221
    /**
201
     * Sets the key for HMACs
222
     * Sets the key for HMACs
202
     *
223
     *
203
     * Keys can be of any length.
224
     * Keys can be of any length.
204
     *
225
     *
-
 
226
     * @access public
205
     * @param string $key
227
     * @param string $key
206
     */
228
     */
207
    public function setKey($key = false)
229
    public function setKey($key = false)
208
    {
230
    {
209
        $this->key = $key;
231
        $this->key = $key;
Line 214... Line 236...
214
    /**
236
    /**
215
     * Sets the nonce for UMACs
237
     * Sets the nonce for UMACs
216
     *
238
     *
217
     * Keys can be of any length.
239
     * Keys can be of any length.
218
     *
240
     *
-
 
241
     * @access public
219
     * @param string $nonce
242
     * @param string $nonce
220
     */
243
     */
221
    public function setNonce($nonce = false)
244
    public function setNonce($nonce = false)
222
    {
245
    {
223
        switch (true) {
246
        switch (true) {
Line 239... Line 262...
239
     *
262
     *
240
     * As documented in https://www.reddit.com/r/PHP/comments/9nct2l/symfonypolyfill_hash_pbkdf2_correct_fix_for/
263
     * As documented in https://www.reddit.com/r/PHP/comments/9nct2l/symfonypolyfill_hash_pbkdf2_correct_fix_for/
241
     * when doing an HMAC multiple times it's faster to compute the hash once instead of computing it during
264
     * when doing an HMAC multiple times it's faster to compute the hash once instead of computing it during
242
     * every call
265
     * every call
243
     *
266
     *
-
 
267
     * @access private
244
     */
268
     */
245
    private function computeKey()
269
    private function computeKey()
246
    {
270
    {
247
        if ($this->key === false) {
271
        if ($this->key === false) {
248
            $this->computedKey = false;
272
            $this->computedKey = false;
Line 262... Line 286...
262
    /**
286
    /**
263
     * Gets the hash function.
287
     * Gets the hash function.
264
     *
288
     *
265
     * As set by the constructor or by the setHash() method.
289
     * As set by the constructor or by the setHash() method.
266
     *
290
     *
-
 
291
     * @access public
267
     * @return string
292
     * @return string
268
     */
293
     */
269
    public function getHash()
294
    public function getHash()
270
    {
295
    {
271
        return $this->hashParam;
296
        return $this->hashParam;
272
    }
297
    }
273
 
298
 
274
    /**
299
    /**
275
     * Sets the hash function.
300
     * Sets the hash function.
276
     *
301
     *
-
 
302
     * @access public
277
     * @param string $hash
303
     * @param string $hash
278
     */
304
     */
279
    public function setHash($hash)
305
    public function setHash($hash)
280
    {
306
    {
281
        $this->hashParam = $hash = strtolower($hash);
307
        $this->hashParam = $hash = strtolower($hash);
Line 765... Line 791...
765
    }
791
    }
766
 
792
 
767
    /**
793
    /**
768
     * Compute the Hash / HMAC / UMAC.
794
     * Compute the Hash / HMAC / UMAC.
769
     *
795
     *
-
 
796
     * @access public
770
     * @param string $text
797
     * @param string $text
771
     * @return string
798
     * @return string
772
     */
799
     */
773
    public function hash($text)
800
    public function hash($text)
774
    {
801
    {
Line 853... Line 880...
853
    }
880
    }
854
 
881
 
855
    /**
882
    /**
856
     * Returns the hash length (in bits)
883
     * Returns the hash length (in bits)
857
     *
884
     *
-
 
885
     * @access public
858
     * @return int
886
     * @return int
859
     */
887
     */
860
    public function getLength()
888
    public function getLength()
861
    {
889
    {
862
        return $this->length << 3;
890
        return $this->length << 3;
863
    }
891
    }
864
 
892
 
865
    /**
893
    /**
866
     * Returns the hash length (in bytes)
894
     * Returns the hash length (in bytes)
867
     *
895
     *
-
 
896
     * @access public
868
     * @return int
897
     * @return int
869
     */
898
     */
870
    public function getLengthInBytes()
899
    public function getLengthInBytes()
871
    {
900
    {
872
        return $this->length;
901
        return $this->length;
873
    }
902
    }
874
 
903
 
875
    /**
904
    /**
876
     * Returns the block length (in bits)
905
     * Returns the block length (in bits)
877
     *
906
     *
-
 
907
     * @access public
878
     * @return int
908
     * @return int
879
     */
909
     */
880
    public function getBlockLength()
910
    public function getBlockLength()
881
    {
911
    {
882
        return $this->blockSize;
912
        return $this->blockSize;
883
    }
913
    }
884
 
914
 
885
    /**
915
    /**
886
     * Returns the block length (in bytes)
916
     * Returns the block length (in bytes)
887
     *
917
     *
-
 
918
     * @access public
888
     * @return int
919
     * @return int
889
     */
920
     */
890
    public function getBlockLengthInBytes()
921
    public function getBlockLengthInBytes()
891
    {
922
    {
892
        return $this->blockSize >> 3;
923
        return $this->blockSize >> 3;
893
    }
924
    }
894
 
925
 
895
    /**
926
    /**
896
     * Pads SHA3 based on the mode
927
     * Pads SHA3 based on the mode
897
     *
928
     *
-
 
929
     * @access private
898
     * @param int $padLength
930
     * @param int $padLength
899
     * @param int $padType
931
     * @param int $padType
900
     * @return string
932
     * @return string
901
     */
933
     */
902
    private static function sha3_pad($padLength, $padType)
934
    private static function sha3_pad($padLength, $padType)
Line 935... Line 967...
935
     * In https://ws680.nist.gov/publication/get_pdf.cfm?pub_id=919061#page=16 KECCAK[C] is
967
     * In https://ws680.nist.gov/publication/get_pdf.cfm?pub_id=919061#page=16 KECCAK[C] is
936
     * defined as "the KECCAK instance with KECCAK-f[1600] as the underlying permutation and
968
     * defined as "the KECCAK instance with KECCAK-f[1600] as the underlying permutation and
937
     * capacity c". This is relevant because, altho the KECCAK standard defines a mode
969
     * capacity c". This is relevant because, altho the KECCAK standard defines a mode
938
     * (KECCAK-f[800]) designed for 32-bit machines that mode is incompatible with SHA3
970
     * (KECCAK-f[800]) designed for 32-bit machines that mode is incompatible with SHA3
939
     *
971
     *
-
 
972
     * @access private
940
     * @param string $p
973
     * @param string $p
941
     * @param int $c
974
     * @param int $c
942
     * @param int $r
975
     * @param int $r
943
     * @param int $d
976
     * @param int $d
944
     * @param int $padType
977
     * @param int $padType
Line 995... Line 1028...
995
    }
1028
    }
996
 
1029
 
997
    /**
1030
    /**
998
     * 32-bit block processing method for SHA3
1031
     * 32-bit block processing method for SHA3
999
     *
1032
     *
-
 
1033
     * @access private
1000
     * @param array $s
1034
     * @param array $s
1001
     */
1035
     */
1002
    private static function processSHA3Block32(&$s)
1036
    private static function processSHA3Block32(&$s)
1003
    {
1037
    {
1004
        static $rotationOffsets = [
1038
        static $rotationOffsets = [
Line 1104... Line 1138...
1104
    }
1138
    }
1105
 
1139
 
1106
    /**
1140
    /**
1107
     * Rotate 32-bit int
1141
     * Rotate 32-bit int
1108
     *
1142
     *
-
 
1143
     * @access private
1109
     * @param array $x
1144
     * @param array $x
1110
     * @param int $shift
1145
     * @param int $shift
1111
     */
1146
     */
1112
    private static function rotateLeft32($x, $shift)
1147
    private static function rotateLeft32($x, $shift)
1113
    {
1148
    {
Line 1125... Line 1160...
1125
    }
1160
    }
1126
 
1161
 
1127
    /**
1162
    /**
1128
     * Pure-PHP 64-bit implementation of SHA3
1163
     * Pure-PHP 64-bit implementation of SHA3
1129
     *
1164
     *
-
 
1165
     * @access private
1130
     * @param string $p
1166
     * @param string $p
1131
     * @param int $c
1167
     * @param int $c
1132
     * @param int $r
1168
     * @param int $r
1133
     * @param int $d
1169
     * @param int $d
1134
     * @param int $padType
1170
     * @param int $padType
Line 1184... Line 1220...
1184
    }
1220
    }
1185
 
1221
 
1186
    /**
1222
    /**
1187
     * 64-bit block processing method for SHA3
1223
     * 64-bit block processing method for SHA3
1188
     *
1224
     *
-
 
1225
     * @access private
1189
     * @param array $s
1226
     * @param array $s
1190
     */
1227
     */
1191
    private static function processSHA3Block64(&$s)
1228
    private static function processSHA3Block64(&$s)
1192
    {
1229
    {
1193
        static $rotationOffsets = [
1230
        static $rotationOffsets = [
Line 1270... Line 1307...
1270
    }
1307
    }
1271
 
1308
 
1272
    /**
1309
    /**
1273
     * Rotate 64-bit int
1310
     * Rotate 64-bit int
1274
     *
1311
     *
-
 
1312
     * @access private
1275
     * @param int $x
1313
     * @param int $x
1276
     * @param int $shift
1314
     * @param int $shift
1277
     */
1315
     */
1278
    private static function rotateLeft64($x, $shift)
1316
    private static function rotateLeft64($x, $shift)
1279
    {
1317
    {
Line 1281... Line 1319...
1281
    }
1319
    }
1282
 
1320
 
1283
    /**
1321
    /**
1284
     * Pure-PHP implementation of SHA512
1322
     * Pure-PHP implementation of SHA512
1285
     *
1323
     *
-
 
1324
     * @access private
1286
     * @param string $m
1325
     * @param string $m
1287
     * @param array $hash
1326
     * @param array $hash
1288
     * @return string
1327
     * @return string
1289
     */
1328
     */
1290
    private static function sha512($m, $hash)
1329
    private static function sha512($m, $hash)