-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmdline_example.php
116 lines (98 loc) · 2.72 KB
/
cmdline_example.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env php
<?php
/**
* Command-line example usage of OpenSSL-File-Encrypt class.
*
* Processes up to 1.8GB on CLI, subject to memory availability.
*
* Usage:
* php <thisfilename> -e|-d <filename>
*
* @author Martin Latter
* @copyright Martin Latter 20/02/2018
* @version 0.08
* @license GNU GPL version 3.0 (GPL v3); http://www.gnu.org/licenses/gpl.html
* @link https://github.com/Tinram/OpenSSL-File-Encrypt.git
*/
declare(strict_types=1);
require('classes/openssl_file.class.php');
#### CONFIG SECURITY ####
define('MY_KEY_STRETCHES', 2 ** 18);
define('MY_SALT', 'Ⓩ♢┱♘Ⓠ◢⒭☮☺◶♥╂⒣♻Ⓟ▐◦☩⒕♁Ⓚ⚑└◵▴ⓥ▸┊⚰┘┓─┙⚡◞☩♡▭▁◟◓╇╡◨Ⓑ♋┥⚑▽♘╨⒮ⓡ╕▴╫⓬Ⓕ◸◎◦ⓚⒷ♱');
#########################
define('DUB_EOL', PHP_EOL . PHP_EOL);
define('LINUX', (stripos(php_uname(), 'linux') !== false) ? true : false);
if ( ! extension_loaded('openssl'))
{
die(PHP_EOL . ' OpenSSL library not available!' . DUB_EOL);
}
$sUsage =
PHP_EOL . ' ' . basename(__FILE__, '.php') .
DUB_EOL . "\tusage: php " . basename(__FILE__) . ' -e|-d <file> ' . ( ! LINUX ? '<password>' : '') . DUB_EOL;
$sMode = null;
$aOptions = getopt('h::e::d::', ['help::', 'h::']);
if (count($aOptions) !== 0)
{
$sOpt = key($aOptions);
switch ($sOpt)
{
case 'h':
die($sUsage);
break;
case 'e':
case 'd':
$sMode = $sOpt;
break;
}
}
else
{
die($sUsage);
}
if ( ! isset($_SERVER['argv'][2]))
{
echo PHP_EOL . ' missing filename!' . PHP_EOL;
die($sUsage);
}
$sFilename = $_SERVER['argv'][2];
if ( ! file_exists($sFilename))
{
die(PHP_EOL . ' \'' . $sFilename . '\' does not exist in this directory!' . DUB_EOL);
}
if (LINUX)
{
echo ' password: ';
`/bin/stty -echo`;
$sPassword = trim(fgets(STDIN));
`/bin/stty echo`;
if ($sMode === 'e')
{
echo PHP_EOL . ' re-enter password: ';
`/bin/stty -echo`;
$sPassword2 = trim(fgets(STDIN));
`/bin/stty echo`;
if ($sPassword !== $sPassword2)
{
die(PHP_EOL . ' entered passwords do not match!' . DUB_EOL);
}
}
}
else
{
if ( ! isset($_SERVER['argv'][3]))
{
die(PHP_EOL . ' missing password!' . DUB_EOL . "\tusage: " . basename(__FILE__) . ' -e|-d <file> <password>' . DUB_EOL);
}
else
{
$sPassword = $_SERVER['argv'][3];
}
}
if ($sMode === 'e')
{
echo OpenSSLFile::encrypt($sFilename, $sPassword, MY_KEY_STRETCHES, MY_SALT);
}
else if ($sMode === 'd')
{
echo OpenSSLFile::decrypt($sFilename, $sPassword, MY_KEY_STRETCHES, MY_SALT);
}