-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathOpaqueEncoder.php
More file actions
107 lines (92 loc) · 2.57 KB
/
Copy pathOpaqueEncoder.php
File metadata and controls
107 lines (92 loc) · 2.57 KB
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
<?php
/**
* Opaque ID encoder.
*
* Translates between 32-bit integers (such as resource IDs) and obfuscated
* scrambled values, as a one-to-one mapping. Supports hex and base64 url-safe
* string representations. Expects a secret integer key in the constructor.
*
* (c) 2011 Marek Z. @marekweb
*/
class OpaqueEncoder {
private $key;
private $extraChars = '.-';
const ENCODING_INT = 0;
const ENCODING_HEX = 1;
const ENCODING_BASE64 = 2;
/**
* @param $key Secret key used for lightweight encryption.
*/
public function __construct($key, $encoding = self::ENCODING_HEX) {
$this->key = $key;
$this->encoding = $encoding;
}
/**
* Produce an integer hash of a 16-bit integer, returning a transformed 16-bit integer.
*/
protected function transform($i) {
$i = ($this->key ^ $i) * 0x9e3b;
return $i >> ($i & 0xf) & 0xffff;
}
/**
* Reversibly transcode a 32-bit integer to a scrambled form, returning a new 32-bit integer.
*/
public function transcode($i) {
$r = $i & 0xffff;
$l = $i >> 16 & 0xffff ^ $this->transform($r);
return (($r ^ $this->transform($l)) << 16) + $l;
}
/**
* Encode a value according to the encoding mode selected upon instantiation.
*/
public function encode($i) {
switch ($this->encoding) {
case self::ENCODING_INT:
return $this->transcode($i);
case self::ENCODING_BASE64:
return $this->encodeBase64($i);
case self::ENCODING_HEX:
default:
return $this->encodeHex($i);
}
}
/**
* Decode a value according to the encoding mode selected upon instantiation.
*/
public function decode($s) {
switch ($this->encoding) {
case self::ENCODING_INT:
return $this->transcode($s);
case self::ENCODING_BASE64:
return $this->decodeBase64($s);
case self::ENCODING_HEX:
default:
return $this->decodeHex($s);
}
}
/**
* Transcode an integer and return it as an 8-character hex string.
*/
public function encodeHex($i) {
return dechex($this->transcode($i));
}
/**
* Transcode an integer and return it as a 6-character base64 string.
*/
public function encodeBase64($i) {
return strtr(substr(base64_encode(pack('N', $this->transcode($i))), 0, 6), '+/', $this->extraChars);
}
/**
* Decode an 8-character hex string, returning the original integer.
*/
public function decodeHex($s) {
return $this->transcode(hexdec($s));
}
/**
* Decode a 6-character base64 string, returning the original integer.
*/
public function decodeBase64($s) {
$unpacked = unpack('N', base64_decode(strtr($s, $this->extraChars, '+/')));
return $this->transcode($unpacked[1]);
}
}