1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2017-present Ganbaro Digital Ltd |
7
|
|
|
* All rights reserved. |
8
|
|
|
* |
9
|
|
|
* Redistribution and use in source and binary forms, with or without |
10
|
|
|
* modification, are permitted provided that the following conditions |
11
|
|
|
* are met: |
12
|
|
|
* |
13
|
|
|
* * Redistributions of source code must retain the above copyright |
14
|
|
|
* notice, this list of conditions and the following disclaimer. |
15
|
|
|
* |
16
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
17
|
|
|
* notice, this list of conditions and the following disclaimer in |
18
|
|
|
* the documentation and/or other materials provided with the |
19
|
|
|
* distribution. |
20
|
|
|
* |
21
|
|
|
* * Neither the names of the copyright holders nor the names of his |
22
|
|
|
* contributors may be used to endorse or promote products derived |
23
|
|
|
* from this software without specific prior written permission. |
24
|
|
|
* |
25
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
26
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
27
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
28
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
29
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
30
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
31
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
32
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
33
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
34
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
35
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
36
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
37
|
|
|
* |
38
|
|
|
* @category Libraries |
39
|
|
|
* @package MessagingPipeline/Operations |
40
|
|
|
* @author Stuart Herbert <[email protected]> |
41
|
|
|
* @copyright 2017-present Ganbaro Digital Ltd www.ganbarodigital.com |
42
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
43
|
|
|
* @link http://ganbarodigital.github.io/php-mv-messaging-pipeline |
44
|
|
|
*/ |
45
|
|
|
|
46
|
|
|
namespace GanbaroDigital\MessagingPipeline\V1\Operations; |
47
|
|
|
|
48
|
|
|
use GanbaroDigital\MessagingPipeline\V1\Requirements\RequireValidEncryptionCipher; |
49
|
|
|
use GanbaroDigital\MessagingPipeline\V1\Requirements\RequireValidEncryptionIV; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* create an encrypted copy of a string |
53
|
|
|
*/ |
54
|
|
|
class EncryptString |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* create an encrypted copy of a string |
58
|
|
|
* |
59
|
|
|
* @param string $item |
60
|
|
|
* the string that you want to encrypt |
61
|
|
|
* @param string $fieldOrVarName |
62
|
|
|
* what do you call $item in your own code? |
63
|
|
|
* @param string $encryptionType |
64
|
|
|
* what kind of encryption cipher do you want to use? |
65
|
|
|
* this must be supported by OpenSSL |
66
|
|
|
* @param string $encryptionKey |
67
|
|
|
* a password that you will share with the person who needs |
68
|
|
|
* to decrypt the string |
69
|
|
|
* @param string $iv |
70
|
|
|
* the initialisation vector to use to encrypt the string |
71
|
|
|
* @return string |
72
|
|
|
* the encrypted copy of $item |
73
|
|
|
*/ |
74
|
|
|
public static function from(string $item, string $fieldOrVarName, string $encryptionType, string $encryptionKey, string $iv) : string |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
// robustness! |
77
|
|
|
RequireValidEncryptionCipher::apply()->to($encryptionType, '$encryptionType'); |
78
|
|
|
RequireValidEncryptionIV::apply($encryptionType)->to($iv, '$iv'); |
79
|
|
|
|
80
|
|
|
// if we get to here, then we are confident that this will always |
81
|
|
|
// succeed! |
82
|
|
|
return openssl_encrypt( |
83
|
|
|
$item, |
84
|
|
|
$encryptionType, |
85
|
|
|
$encryptionKey, |
86
|
|
|
OPENSSL_RAW_DATA, |
87
|
|
|
$iv |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.