Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Crypto\Polyfill;
3
 
4
/**
5
 * Class ByteArray
6
 * @package Aws\Crypto\Polyfill
7
 */
8
class ByteArray extends \SplFixedArray
9
{
10
    use NeedsTrait;
11
 
12
    /**
13
     * ByteArray constructor.
14
     *
15
     * @param int|string|int[] $size
16
     *     If you pass in an integer, it creates a ByteArray of that size.
17
     *     If you pass in a string or array, it converts it to an array of
18
     *       integers between 0 and 255.
19
     * @throws \InvalidArgumentException
20
     */
21
    public function __construct($size = 0)
22
    {
23
        $arr = null;
24
        // Integer? This behaves just like SplFixedArray.
25
        if (\is_array($size)) {
26
            // Array? We need to pass the count to parent::__construct() then populate
27
            $arr = $size;
28
            $size = \count($arr);
29
        } elseif (\is_string($size)) {
30
            // We need to avoid mbstring.func_overload
31
            if (\is_callable('\\mb_str_split')) {
32
                $tmp = \mb_str_split($size, 1, '8bit');
33
            } else {
34
                $tmp = \str_split($size, 1);
35
            }
36
            // Let's convert each character to an 8-bit integer and store in $arr
37
            $arr = [];
38
            if (!empty($tmp)) {
39
                foreach ($tmp as $t) {
40
                    if (strlen($t) < 1) {
41
                        continue;
42
                    }
43
                    $arr []= \unpack('C', $t)[1] & 0xff;
44
                }
45
            }
46
            $size = \count($arr);
47
        } elseif ($size instanceof ByteArray) {
48
            $arr = $size->toArray();
49
            $size = $size->count();
50
        } elseif (!\is_int($size)) {
51
            throw new \InvalidArgumentException(
52
                'Argument must be an integer, string, or array of integers.'
53
            );
54
        }
55
 
56
        parent::__construct($size);
57
 
58
        if (!empty($arr)) {
59
            // Populate this object with values from constructor argument
60
            foreach ($arr as $i => $v) {
61
                $this->offsetSet($i, $v);
62
            }
63
        } else {
64
            // Initialize to zero.
65
            for ($i = 0; $i < $size; ++$i) {
66
                $this->offsetSet($i, 0);
67
            }
68
        }
69
    }
70
 
71
    /**
72
     * Encode an integer into a byte array. 32-bit (unsigned), big endian byte order.
73
     *
74
     * @param int $num
75
     * @return self
76
     */
77
    public static function enc32be($num)
78
    {
79
        return new ByteArray(\pack('N', $num));
80
    }
81
 
82
    /**
83
     * @param ByteArray $other
84
     * @return bool
85
     */
86
    public function equals(ByteArray $other)
87
    {
88
        if ($this->count() !== $other->count()) {
89
            return false;
90
        }
91
        $d = 0;
92
        for ($i = $this->count() - 1; $i >= 0; --$i) {
93
            $d |= $this[$i] ^ $other[$i];
94
        }
95
        return $d === 0;
96
    }
97
 
98
    /**
99
     * @param ByteArray $array
100
     * @return ByteArray
101
     */
102
    public function exclusiveOr(ByteArray $array)
103
    {
104
        self::needs(
105
            $this->count() === $array->count(),
106
            'Both ByteArrays must be equal size for exclusiveOr()'
107
        );
108
        $out = clone $this;
109
        for ($i = 0; $i < $this->count(); ++$i) {
110
            $out[$i] = $array[$i] ^ $out[$i];
111
        }
112
        return $out;
113
    }
114
 
115
    /**
116
     * Returns a new ByteArray incremented by 1 (big endian byte order).
117
     *
118
     * @param int $increase
119
     * @return self
120
     */
121
    public function getIncremented($increase = 1)
122
    {
123
        $clone = clone $this;
124
        $index = $clone->count();
125
        while ($index > 0) {
126
            --$index;
127
            $tmp = ($clone[$index] + $increase) & PHP_INT_MAX;
128
            $clone[$index] = $tmp & 0xff;
129
            $increase = $tmp >> 8;
130
        }
131
        return $clone;
132
    }
133
 
134
    /**
135
     * Sets a value. See SplFixedArray for more.
136
     *
137
     * @param int $index
138
     * @param int $newval
139
     * @return void
140
     */
141
    public function offsetSet($index, $newval)
142
    {
143
        parent::offsetSet($index, $newval & 0xff);
144
    }
145
 
146
    /**
147
     * Return a copy of this ByteArray, bitshifted to the right by 1.
148
     * Used in Gmac.
149
     *
150
     * @return self
151
     */
152
    public function rshift()
153
    {
154
        $out = clone $this;
155
        for ($j = $this->count() - 1; $j > 0; --$j) {
156
            $out[$j] = (($out[$j - 1] & 1) << 7) | ($out[$j] >> 1);
157
        }
158
        $out[0] >>= 1;
159
        return $out;
160
    }
161
 
162
    /**
163
     * Constant-time conditional select. This is meant to read like a ternary operator.
164
     *
165
     * $z = ByteArray::select(1, $x, $y); // $z is equal to $x
166
     * $z = ByteArray::select(0, $x, $y); // $z is equal to $y
167
     *
168
     * @param int $select
169
     * @param ByteArray $left
170
     * @param ByteArray $right
171
     * @return ByteArray
172
     */
173
    public static function select($select, ByteArray $left, ByteArray $right)
174
    {
175
        self::needs(
176
            $left->count() === $right->count(),
177
            'Both ByteArrays must be equal size for select()'
178
        );
179
        $rightLength = $right->count();
180
        $out = clone $right;
181
        $mask = (-($select & 1)) & 0xff;
182
        for ($i = 0; $i < $rightLength;  $i++) {
183
            $out[$i] = $out[$i] ^ (($left[$i] ^ $right[$i]) & $mask);
184
        }
185
        return $out;
186
    }
187
 
188
    /**
189
     * Overwrite values of this ByteArray based on a separate ByteArray, with
190
     * a given starting offset and length.
191
     *
192
     * See JavaScript's Uint8Array.set() for more information.
193
     *
194
     * @param ByteArray $input
195
     * @param int $offset
196
     * @param int|null $length
197
     * @return self
198
     */
199
    public function set(ByteArray $input, $offset = 0, $length = null)
200
    {
201
        self::needs(
202
            is_int($offset) && $offset >= 0,
203
            'Offset must be a positive integer or zero'
204
        );
205
        if (is_null($length)) {
206
            $length = $input->count();
207
        }
208
 
209
        $i = 0; $j = $offset;
210
        while ($i < $length && $j < $this->count()) {
211
            $this[$j] = $input[$i];
212
            ++$i;
213
            ++$j;
214
        }
215
        return $this;
216
    }
217
 
218
    /**
219
     * Returns a slice of this ByteArray.
220
     *
221
     * @param int $start
222
     * @param null $length
223
     * @return self
224
     */
225
    public function slice($start = 0, $length = null)
226
    {
227
        return new ByteArray(\array_slice($this->toArray(), $start, $length));
228
    }
229
 
230
    /**
231
     * Mutates the current state and sets all values to zero.
232
     *
233
     * @return void
234
     */
235
    public function zeroize()
236
    {
237
        for ($i = $this->count() - 1; $i >= 0; --$i) {
238
            $this->offsetSet($i, 0);
239
        }
240
    }
241
 
242
    /**
243
     * Converts the ByteArray to a raw binary string.
244
     *
245
     * @return string
246
     */
247
    public function toString()
248
    {
249
        $count = $this->count();
250
        if ($count === 0) {
251
            return '';
252
        }
253
        $args = $this->toArray();
254
        \array_unshift($args, \str_repeat('C', $count));
255
        // constant-time, PHP <5.6 equivalent to pack('C*', ...$args);
256
        return \call_user_func_array('\\pack', $args);
257
    }
258
}