This repository has been archived by the owner on May 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
FieldElement.php
87 lines (76 loc) · 1.89 KB
/
FieldElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* FieldElement
*
*
* SplFixedArray with more functions.
*
*
* @author Devi Mandiri <devi.mandiri@gmail.com>
* @link https://github.com/devi/Salt
*
*/
class FieldElement extends SplFixedArray {
public function toString() {
$this->rewind();
$buf = "";
while ($this->valid()) {
$buf .= chr($this->current());
$this->next();
}
$this->rewind();
return $buf;
}
public function toHex() {
$this->rewind();
$hextable = "0123456789abcdef";
$buf = "";
while ($this->valid()) {
$c = $this->current();
$buf .= $hextable[$c>>4];
$buf .= $hextable[$c&0x0f];
$this->next();
}
$this->rewind();
return $buf;
}
public function toBase64() {
return base64_encode($this->toString());
}
public function toJson() {
return json_encode($this->toString());
}
public function slice($offset, $length = null) {
$length = $length ? $length : $this->getSize()-$offset;
$slice = new FieldElement($length);
for ($i = 0;$i < $length;++$i) {
$slice[$i] = $this->offsetGet($i+$offset);
}
return $slice;
}
public function copy($src, $size, $offset = 0, $srcOffset = 0) {
for ($i = 0;$i < $size;++$i) {
$this->offsetSet($i+$offset, $src[$i+$srcOffset]);
}
}
public static function fromArray($array, $save_indexes = true) {
$l = count($array);
$fe = new FieldElement($l);
$array = $save_indexes ? $array : array_values($array);
foreach ($array as $k => $v) $fe[$k] = $v;
return $fe;
}
public static function fromString($str) {
return static::fromArray(unpack("C*", $str), false);
}
public static function fromHex($hex) {
$hex = preg_replace('/[^0-9a-f]/', '', $hex);
return static::fromString(pack("H*", $hex));
}
public static function fromBase64($base64) {
return FieldElement::fromString(base64_decode($base64, true));
}
public static function fromJson($json) {
return FieldElement::fromArray(json_decode($json, true));
}
}