-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.php
61 lines (50 loc) · 1.57 KB
/
crypto.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
<?php
class crypto
{
private static $encrypt_type = MCRYPT_3DES;
private static $encrypt_mode = MCRYPT_MODE_ECB;
private static $iv_size;
private static $iv;
public static function encrypt($salt, $cleartext)
{
self::set_values();
return self::base64url_encode(mcrypt_encrypt(self::$encrypt_type,self::pad($salt),$cleartext,self::$encrypt_mode,self::$iv));
}
public static function decrypt($salt, $ciphertext)
{
self::set_values();
return rtrim(mcrypt_decrypt(self::$encrypt_type,self::pad($salt),self::base64url_decode($ciphertext),self::$encrypt_mode,self::$iv),"\0");
}
private static function set_values()
{
self::$iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
self::$iv = mcrypt_create_iv(self::$iv_size, MCRYPT_RAND);
}
private static function base64url_encode($plainText)
{
$base64 = base64_encode($plainText);
$base64url = strtr($base64, '+/=', '-_*');
return ($base64url);
}
private static function base64url_decode($base64url)
{
$base64 = strtr($base64url, '-_*', '+/=');
$plainText = base64_decode($base64);
return ($plainText);
}
private static function pad($key)
{
// key is too large
if(strlen($key) > 32) return false;
// set sizes
$sizes = array(24,32);
// loop through sizes and pad key
foreach($sizes as $s){
while(strlen($key) < $s) $key = $key."\0";
if(strlen($key) == $s) break; // finish if the key matches a size
}
// return
return $key;
}
}
?>