-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.unicrypt.php
74 lines (49 loc) · 1.7 KB
/
class.unicrypt.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
<?php
/**
* Simple Encryption/Decryption class based on the OpenSSL Library
* Written by Janus Christian Skov 2018
*/
class unicrypt {
private $secret_key = 'add-unique-key-here';
private $init_vector_key = 'add-unique-key-here';
# Add a valid AES cipher
private $cipher = 'AES-192-CFB1'; // by default
# Parcing the secret key
public function set_secret_key($key) {
$this->secret_key = $key;
}
# Collecting the secret key hashed
public function get_secret_key() {
return hash('sha256', $this->secret_key);
}
# Parcing the initialization vector key
public function set_init_vector_key($iv) {
$this->init_vector_key = $iv;
}
# Collecting the initialization vector key hashed and shorten
public function get_init_vector_key() {
return substr(hash('sha256',$this->init_vector_key), 0, 16);
}
# parcing the AES cipher method
public function set_cipher($cipher) {
$this->cipher = $cipher;
}
# Collecting the cipher method
public function get_cipher() {
return $this->cipher;
}
# OpenSSL encryption with keys and cipher method, encoded with MIME base64 as binary wrapper
public function encrypt($string) {
$cipher = $this->get_cipher();
$secret_key = $this->get_secret_key();
$init_vector = $this->get_init_vector_key();
return base64_encode(openssl_encrypt($string, $cipher, $secret_key, 0, $init_vector));
}
# OpenSSL decryption with secret key, init vector key and a chosen cipher method
public function decrypt($string) {
$cipher = $this->get_cipher();
$secret_key = $this->get_secret_key();
$init_vector = $this->get_init_vector_key();
return openssl_decrypt(base64_decode($string), $cipher, $secret_key, 0, $init_vector);
}
}