]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - extlib/ParagonIE/ConstantTime/Hex.php
Merge branch 'nightly' into 'master'
[quix0rs-gnu-social.git] / extlib / ParagonIE / ConstantTime / Hex.php
1 <?php
2 namespace ParagonIE\ConstantTime;
3
4 /**
5  *  Copyright (c) 2016 Paragon Initiative Enterprises.
6  *  Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
7  *
8  *  Permission is hereby granted, free of charge, to any person obtaining a copy
9  *  of this software and associated documentation files (the "Software"), to deal
10  *  in the Software without restriction, including without limitation the rights
11  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  *  copies of the Software, and to permit persons to whom the Software is
13  *  furnished to do so, subject to the following conditions:
14  *
15  *  The above copyright notice and this permission notice shall be included in all
16  *  copies or substantial portions of the Software.
17  *
18  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  *  SOFTWARE.
25  */
26
27 /**
28  * Class Hex
29  * @package ParagonIE\ConstantTime
30  */
31 abstract class Hex implements EncoderInterface
32 {
33     /**
34      * Convert a binary string into a hexadecimal string without cache-timing
35      * leaks
36      *
37      * @param string $bin_string (raw binary)
38      * @return string
39      */
40     public static function encode($bin_string)
41     {
42         $hex = '';
43         $len = Binary::safeStrlen($bin_string);
44         for ($i = 0; $i < $len; ++$i) {
45             $chunk = \unpack('C', Binary::safeSubstr($bin_string, $i, 2));
46             $c = $chunk[1] & 0xf;
47             $b = $chunk[1] >> 4;
48             $hex .= pack(
49                 'CC',
50                 (87 + $b + ((($b - 10) >> 8) & ~38)),
51                 (87 + $c + ((($c - 10) >> 8) & ~38))
52             );
53         }
54         return $hex;
55     }
56
57     /**
58      * Convert a binary string into a hexadecimal string without cache-timing
59      * leaks, returning uppercase letters (as per RFC 4648)
60      *
61      * @param string $bin_string (raw binary)
62      * @return string
63      */
64     public static function encodeUpper($bin_string)
65     {
66         $hex = '';
67         $len = Binary::safeStrlen($bin_string);
68         for ($i = 0; $i < $len; ++$i) {
69             $chunk = \unpack('C', Binary::safeSubstr($bin_string, $i, 2));
70             $c = $chunk[1] & 0xf;
71             $b = $chunk[1] >> 4;
72             $hex .= pack(
73                 'CC',
74                 (55 + $b + ((($b - 10) >> 8) & ~6)),
75                 (55 + $c + ((($c - 10) >> 8) & ~6))
76             );
77         }
78         return $hex;
79     }
80
81     /**
82      * Convert a hexadecimal string into a binary string without cache-timing
83      * leaks
84      *
85      * @param string $hex_string
86      * @return string (raw binary)
87      * @throws \RangeException
88      */
89     public static function decode($hex_string)
90     {
91         $hex_pos = 0;
92         $bin = '';
93         $c_acc = 0;
94         $hex_len = Binary::safeStrlen($hex_string);
95         $state = 0;
96         if (($hex_len & 1) !== 0) {
97             throw new \RangeException(
98                 'Expected an even number of hexadecimal characters'
99             );
100         }
101
102         $chunk = \unpack('C*', $hex_string);
103         while ($hex_pos < $hex_len) {
104             ++$hex_pos;
105             $c = $chunk[$hex_pos];
106             $c_num = $c ^ 48;
107             $c_num0 = ($c_num - 10) >> 8;
108             $c_alpha = ($c & ~32) - 55;
109             $c_alpha0 = (($c_alpha - 10) ^ ($c_alpha - 16)) >> 8;
110             if (($c_num0 | $c_alpha0) === 0) {
111                 throw new \RangeException(
112                     'hexEncode() only expects hexadecimal characters'
113                 );
114             }
115             $c_val = ($c_num0 & $c_num) | ($c_alpha & $c_alpha0);
116             if ($state === 0) {
117                 $c_acc = $c_val * 16;
118             } else {
119                 $bin .= \pack('C', $c_acc | $c_val);
120             }
121             $state ^= 1;
122         }
123         return $bin;
124     }
125 }