-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchecksum_utils.php
More file actions
90 lines (78 loc) · 2.37 KB
/
checksum_utils.php
File metadata and controls
90 lines (78 loc) · 2.37 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
<?php
class ApiCrypter
{
private $iv = '$$appsfly.io##$$'; #Same as in JAVA
#Same as in JAVA
public function __construct() {
}
public function encrypt($str,$key) {
$str = $this->pkcs5_pad($str);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
mcrypt_generic_init($td, $key, $iv);
$encrypted = mcrypt_generic($td, $str);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return base64_encode($encrypted);
}
public function decrypt($code,$key) {
//$code = $this->hex2bin($code);
$iv = $this->iv;
$td = mcrypt_module_open('rijndael-128', '', 'cbc', $iv);
mcrypt_generic_init($td, $key, $iv);
$decrypted = mdecrypt_generic($td, base64_decode($code));
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$ut = utf8_encode(trim($decrypted));
return $this->pkcs5_unpad($ut);
}
protected function pkcs5_pad ($text) {
$blocksize = 16;
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
protected function pkcs5_unpad($text) {
$pad = ord($text{strlen($text)-1});
if ($pad > strlen($text)) {
return false;
}
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
return false;
}
return substr($text, 0, -1 * $pad);
}
public function getSalt($length){
return $this->random_str($length,'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
}
protected function random_str($length,$keyspace) {
$str = '';
$max = mb_strlen($keyspace, '8bit') - 1;
if ($max < 1) {
throw new Exception('$keyspace must be at least two characters long');
}
for ($i = 0; $i < $length; ++$i) {
$str .= $keyspace[random_int(0, $max)];
}
return $str;
}
public function getHash($salt, $data){
return hash('sha256', $salt.$data);
}
public function getCheckSum($data,$key){
$salt = $this->getSalt(8);
$hash = $this->getHash($salt,$data);
$checksum = $hash.$salt;
return $this->encrypt($checksum,$key);
}
public function verifyChecksum($data,$checksum,$key){
$checksum = $this->decrypt($checksum,$key);
$salt = substr($checksum,strlen($checksum)-8);
$sha = substr($checksum,0,strlen($checksum)-8);
if (strcmp($sha,$this->getHash($salt,$data)) == 0){
return TRUE;
}else{
return FALSE;
}
}
}
?>