-
Notifications
You must be signed in to change notification settings - Fork 1
/
reference.php
132 lines (94 loc) · 3.46 KB
/
reference.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
const HYBRID64 = "ybndrfg8ejkmcpqxot1uwisza345h769AHvWPEBZMTIDNYJRSlLKFXC2GVOQU0-_";
function hybrid64_encode($binaryData) {
$encodedString = "";
// Handle all of the pairs.
for ($i = 0; $i + 1 < strlen($binaryData); $i += 2) {
$int1 = ord($binaryData[$i]);
$int2 = ord($binaryData[$i+1]);
$char1 = HYBRID64[$int1 >> 3];
$char2 = HYBRID64[(($int1 & 0b111) << 2) | ($int2 >> 6)];
$char3 = HYBRID64[$int2 & 0b111111];
$encodedString .= $char1 . $char2 . $char3;
}
// Handle a trailing byte. $i will either be zero or length - 1.
if ($i != strlen($binaryData)) {
$int1 = ord($binaryData[strlen($binaryData) - 1]);
$char1 = HYBRID64[$int1 >> 3];
$char2 = HYBRID64[($int1 & 0b111) << 2];
$encodedString .= $char1 . $char2;
}
return $encodedString;
}
function hybrid64_decode($encodedString) {
$decodedData = "";
// Handle all of the triplets.
for ($i = 0; $i + 2 < strlen($encodedString); $i += 3) {
$int1 = strpos(HYBRID64, $encodedString[$i]);
$int2 = strpos(HYBRID64, $encodedString[$i+1]);
$int3 = strpos(HYBRID64, $encodedString[$i+2]);
$byte1 = chr(($int1 << 3) | ($int2 >> 2));
$byte2 = chr((($int2 & 0b11) << 6) | $int3);
$decodedData .= $byte1 . $byte2;
}
// Handle a trailing pair. $i will either be zero or length - 2.
if ($i != strlen($encodedString)) {
$int1 = strpos(HYBRID64, $encodedString[strlen($encodedString)-2]);
$int2 = strpos(HYBRID64, $encodedString[strlen($encodedString)-1]);
$byte1 = chr(($int1 << 3) | ($int2 >> 2));
$decodedData .= $byte1;
}
return $decodedData;
}
function hybrid64_ascii_encode($binaryData) {
$encodedString = "";
// Handle all of the pairs.
for ($i = 0; $i + 1 < strlen($binaryData); $i += 2) {
$int1 = ord($binaryData[$i]);
$int2 = ord($binaryData[$i+1]);
// XOR the second byte to switch which groups of ASCII are always in
// zbase32.
$int2 ^= 0b00100000;
$char1 = HYBRID64[$int1 >> 3];
$char2 = HYBRID64[(($int1 & 0b111) << 2) | ($int2 >> 6)];
$char3 = HYBRID64[$int2 & 0b111111];
$encodedString .= $char1 . $char2 . $char3;
}
// Handle a trailing byte. $i will either be zero or length - 1.
if ($i != strlen($binaryData)) {
$int1 = ord($binaryData[strlen($binaryData) - 1]);
$char1 = HYBRID64[$int1 >> 3];
$char2 = HYBRID64[($int1 & 0b111) << 2];
$encodedString .= $char1 . $char2;
}
return $encodedString;
}
function hybrid64_ascii_decode($encodedString) {
$decodedData = "";
// Handle all of the triplets.
for ($i = 0; $i + 2 < strlen($encodedString); $i += 3) {
$int1 = strpos(HYBRID64, $encodedString[$i]);
$int2 = strpos(HYBRID64, $encodedString[$i+1]);
$int3 = strpos(HYBRID64, $encodedString[$i+2]);
// XOR the third character's index to undo the XORing done in the encoder.
$int3 ^= 0b00100000;
$byte1 = chr(($int1 << 3) | ($int2 >> 2));
$byte2 = chr((($int2 & 0b11) << 6) | $int3);
$decodedData .= $byte1 . $byte2;
}
// Handle a trailing pair. $i will either be zero or length - 2.
if ($i != strlen($encodedString)) {
$int1 = strpos(HYBRID64, $encodedString[strlen($encodedString)-2]);
$int2 = strpos(HYBRID64, $encodedString[strlen($encodedString)-1]);
$byte1 = chr(($int1 << 3) | ($int2 >> 2));
$decodedData .= $byte1;
}
return $decodedData;
}
$testString = "hybrid64";
$encoded = hybrid64_encode($testString);
$decoded = hybrid64_decode($encoded);
echo $encoded, "\n", $decoded, "\n";
$encoded = hybrid64_ascii_encode($testString);
$decoded = hybrid64_ascii_decode($encoded);
echo $encoded, "\n", $decoded, "\n";