-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOSSL.php
94 lines (75 loc) · 1.97 KB
/
OSSL.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
<?php
namespace rogoss\OSSL;
require_once __DIR__ . "/Encrypter.php";
require_once __DIR__ . "/Decrypter.php";
require_once __DIR__ . "/KeyGen.php";
require_once __DIR__ . "/Exception.php";
class OSSL
{
public static function KeyGen(): KeyGen
{
return new KeyGen();
}
public static function Decrypter(): Decrypter
{
return new Decrypter();
}
public static function Encrypter(): Encrypter
{
return new Encrypter();
}
/**
* Symetric - Encryption
*
* @param string $sPassPhrase
* @param string $iv 0 to 16 characters (less than 0 is padded with $sPassPhrase
* @return
*/
public static function BasicCrypter($sPassPhrase, $iv = ""): OSSL
{
$i = new static();
$i->sPassPhrase = $sPassPhrase;
$i->sIV = $iv;
while (strlen($i->sIV) < 16)
$i->sIV .= $sPassPhrase;
$i->sIV = substr($i->sIV, 0, 16);
return $i;
}
private $sPassPhrase = "";
private $sIV = "";
private $bJSON = false;
/**
* Enable this, if you want to en-/decrypt Arrays or objects
* @return
*/
public function json() : static {
$this->bJSON = true;
return $this;
}
public function encrypt($sData, $bRaw = false)
{
if ($this->bJSON) $sData = json_encode($sData);
return ($tmp = openssl_encrypt(
$sData,
'aes-128-cbc',
$this->sPassPhrase,
$bRaw ? OPENSSL_RAW_DATA : 0,
$this->sIV
)) ? $tmp : null ;
}
public function decrypt($sData, $bRaw = false)
{
$sDec = openssl_decrypt(
$sData,
'aes-128-cbc',
$this->sPassPhrase,
$bRaw ? OPENSSL_RAW_DATA : 0,
$this->sIV
);
if ($sDec === false)
return null;
if ($this->bJSON) $sDec = json_decode($sDec, true);
return $sDec;
}
private function __construct() {}
}