-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto.php
80 lines (74 loc) · 1.83 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* https://github.com/ErfanBahramali/Rubika-PHP
*/
class crypto
{
/**
* AES_256_CBC encrypt
* @param string $text text to encrypt
* @param string $key key of encrypt
* @return string encrypted text
*/
public static function aes_256_cbc_encrypt(string $text, string $key)
{
return base64_encode(openssl_encrypt($text, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, str_repeat(chr(0x0), 16)));
}
/**
* AES_256_CBC encrypt
* @param string $text text to decrypt
* @param string $key key of decrypt
* @return string decrypted text
*/
public static function aes_256_cbc_decrypt(string $text, string $key)
{
return openssl_decrypt(base64_decode($text), 'aes-256-cbc', $key, OPENSSL_RAW_DATA);
}
/**
* shift text and create new text
* @param string $text main text
* @return string shifted text
*/
public static function createSecretPassphrase(string $text)
{
$t = mb_substr($text, 0, 8);
$i = mb_substr($text, 8, 8);
$n = mb_substr($text, 16, 8) . $t . mb_substr($text, 24, 8) . $i;
for ($s = 0; $s < mb_strlen($n); $s++) {
$e = $n[$s];
if ($e >= "0" && $e <= "9") {
$char = ((((mb_ord($e[0]) - 48) + 5) % 10) + 48);
} else {
$char = ((((mb_ord($e[0]) - 97) + 9) % 26) + 97);
}
$t = mb_chr($char);
$n = self::replaceCharAt($n, $s, $t);
}
return $n;
}
/**
* @param string $text main text
* @param int $position char position
* @param string $newChar new character for replace
* @return string replaced text
*/
public static function replaceCharAt(string $text, int $position, string $newChar)
{
$text[$position] = $newChar;
return $text;
}
/**
* generate random string
* @param int $length random string length
* @return string generated random string
*/
public static function azRand(int $length = 32)
{
$chars = 'abcdefghijklmnopqrstuvwxyz';
$result = '';
for ($i = 0; $i < $length; $i++) {
$result .= $chars[mt_rand(0, (strlen($chars) - 1))];
}
return $result;
}
}