-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleCrypt.php
More file actions
85 lines (65 loc) · 1.63 KB
/
SimpleCrypt.php
File metadata and controls
85 lines (65 loc) · 1.63 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
<?php
/**
* OO library for mcrypt functionality
*
* @author Glen Scott <glen@glenscott.co.uk>
*/
class SimpleCrypt {
/**
* @var string
*/
private $key;
/**
* @var string
*/
private $input;
/*
* @var string
*/
private $algorithm;
/*
* @var resource
*/
private $td;
/*
* @var string
*/
private $iv;
public function __construct( $algorithm, $key ) {
$this->algorithm = $algorithm;
$this->key = $key;
$this->open_module();
$this->create_init_vector();
}
public function __destruct() {
mcrypt_module_close( $this->td );
}
private function open_module() {
$this->td = mcrypt_module_open( $this->algorithm, null, 'cbc', null );
}
private function create_init_vector() {
$this->iv = mcrypt_create_iv( mcrypt_enc_get_iv_size( $this->td ), MCRYPT_RAND );
}
private function generic_init() {
return mcrypt_generic_init( $this->td, $this->key, $this->iv );
}
public function encrypt_data( $input ) {
$this->input = $input;
$this->generic_init();
$enc = mcrypt_generic( $this->td, $this->input );
mcrypt_generic_deinit( $this->td );
return $enc;
}
/**
* @param $enc string
*/
public function decrypt_data( $enc ) {
$ret = $this->generic_init();
$dec = mdecrypt_generic( $this->td, $enc );
mcrypt_generic_deinit( $this->td );
return $dec;
}
static public function get_available_algorithms() {
return mcrypt_list_algorithms();
}
}