-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.php
More file actions
73 lines (62 loc) · 1.96 KB
/
encryption.php
File metadata and controls
73 lines (62 loc) · 1.96 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
<?php
/**
* PHP Encryption API
*
* @version v0.9.0
* @author Raymund John Ang <raymund@open-nis.org>
* @license MIT License
*/
require_once __DIR__ . '/Basic.php'; // BasicPHP class library
define('PASS_PHRASE', 'MySecret12345'); // Encryption key
/*
|--------------------------------------------------------------------------
| Middleware
|--------------------------------------------------------------------------
*/
/* Require POST method */
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
Basic::apiResponse(405, "Method should be 'POST'.");
exit();
}
$body = file_get_contents('php://input'); // Request body
/* Require request body (not enctype="multipart/form-data") */
if ( empty($body) ) {
Basic::apiResponse(400, 'The request should have a body, and must not be enctype="multipart/form-data".');
exit();
}
/* Require request body to be in JSON format */
$body_array = json_decode($body, TRUE); // Convert JSON body string into array
if (! is_array($body_array)) {
Basic::apiResponse(400, 'The request body should be in JSON format.');
exit();
}
/* Require parameter "action" */
if (! isset($_GET['action']) || empty($_GET['action'])) {
Basic::apiResponse(400, 'Please set "action" parameter to either "encrypt" or "decrypt".');
exit();
}
/*
|--------------------------------------------------------------------------
| Execute Method
|--------------------------------------------------------------------------
*/
// Execute method using "switch" to prevent Function Injection Attack
switch ($_GET['action']) {
case 'encrypt':
$data = array();
foreach($body_array as $key => $value) {
$data[$key] = Basic::encrypt($value, PASS_PHRASE);
}
echo json_encode($data);
break;
case 'decrypt':
$data = array();
foreach($body_array as $key => $value) {
$data[$key] = Basic::decrypt($value, PASS_PHRASE);
}
echo json_encode($data);
break;
default:
Basic::apiResponse(400, 'Please set "action" parameter to either "encrypt" or "decrypt".');
exit();
}