@@ -11,21 +11,21 @@ class CryptoHelper
11
11
/**
12
12
* Extract the message integrity check (MIC) from the digital signature.
13
13
*
14
- * @param MimePart|string $payload
15
- * @param string $algo Default is SHA256
16
- * @param bool $includeHeaders
14
+ * @param MimePart|string $payload
15
+ * @param string $algo Default is SHA256
16
+ * @param bool $includeHeaders
17
17
*
18
18
* @return string
19
19
*/
20
20
public static function calculateMIC ($ payload , $ algo = 'sha256 ' , $ includeHeaders = true )
21
21
{
22
22
$ digestAlgorithm = str_replace ('- ' , '' , strtolower ($ algo ));
23
23
24
- if (! in_array ($ digestAlgorithm , hash_algos (), true )) {
24
+ if (!in_array ($ digestAlgorithm , hash_algos (), true )) {
25
25
throw new \InvalidArgumentException (sprintf ('(MIC) Invalid hash algorithm `%s`. ' , $ digestAlgorithm ));
26
26
}
27
27
28
- if (! ($ payload instanceof MimePart)) {
28
+ if (!($ payload instanceof MimePart)) {
29
29
$ payload = MimePart::fromString ($ payload );
30
30
}
31
31
@@ -37,17 +37,17 @@ public static function calculateMIC($payload, $algo = 'sha256', $includeHeaders
37
37
)
38
38
);
39
39
40
- return $ digest. ', ' . $ algo ;
40
+ return $ digest . ', ' . $ algo ;
41
41
}
42
42
43
43
/**
44
44
* Sign data which contains mime headers.
45
45
*
46
- * @param string|MimePart $data
47
- * @param string|resource $cert
48
- * @param string|resource $privateKey
49
- * @param array $headers
50
- * @param array $micAlgo
46
+ * @param string|MimePart $data
47
+ * @param string|resource $cert
48
+ * @param string|resource $privateKey
49
+ * @param array $headers
50
+ * @param array $micAlgo
51
51
*
52
52
* @return MimePart
53
53
*/
@@ -56,15 +56,15 @@ public static function sign($data, $cert, $privateKey = null, $headers = [], $mi
56
56
$ data = self ::getTempFilename ((string ) $ data );
57
57
$ temp = self ::getTempFilename ();
58
58
59
- if (! openssl_pkcs7_sign ($ data , $ temp , $ cert , $ privateKey , $ headers )) {
59
+ if (!openssl_pkcs7_sign ($ data , $ temp , $ cert , $ privateKey , $ headers )) {
60
60
throw new \RuntimeException (sprintf ('Failed to sign S/Mime message. Error: "%s". ' , openssl_error_string ()));
61
61
}
62
62
63
63
$ payload = MimePart::fromString (file_get_contents ($ temp ), false );
64
64
65
65
if ($ micAlgo ) {
66
66
$ contentType = $ payload ->getHeaderLine ('content-type ' );
67
- $ contentType = preg_replace ('/micalg=(.+);/i ' , 'micalg=" ' . $ micAlgo. '"; ' , $ contentType );
67
+ $ contentType = preg_replace ('/micalg=(.+);/i ' , 'micalg=" ' . $ micAlgo . '"; ' , $ contentType );
68
68
/** @var MimePart $payload */
69
69
$ payload = $ payload ->withHeader ('Content-Type ' , $ contentType );
70
70
}
@@ -86,9 +86,9 @@ public static function sign($data, $cert, $privateKey = null, $headers = [], $mi
86
86
}
87
87
88
88
/**
89
- * @param string|MimePart $data
90
- * @param array|null $caInfo Information about the trusted CA certificates to use in the verification process
91
- * @param array $rootCerts
89
+ * @param string|MimePart $data
90
+ * @param array|null $caInfo Information about the trusted CA certificates to use in the verification process
91
+ * @param array $rootCerts
92
92
*
93
93
* @return bool
94
94
*/
@@ -98,7 +98,7 @@ public static function verify($data, $caInfo = null, $rootCerts = null)
98
98
$ data = self ::getTempFilename ((string ) $ data );
99
99
}
100
100
101
- if (! empty ($ caInfo )) {
101
+ if (!empty ($ caInfo )) {
102
102
foreach ((array ) $ caInfo as $ cert ) {
103
103
$ rootCerts [] = self ::getTempFilename ($ cert );
104
104
}
@@ -118,9 +118,9 @@ public static function verify($data, $caInfo = null, $rootCerts = null)
118
118
}
119
119
120
120
/**
121
- * @param string|MimePart $data
122
- * @param string|array $cert
123
- * @param int|string $cipher
121
+ * @param string|MimePart $data
122
+ * @param string|array $cert
123
+ * @param int|string $cipher
124
124
*
125
125
* @return MimePart
126
126
*/
@@ -133,24 +133,23 @@ public static function encrypt($data, $cert, $cipher = OPENSSL_CIPHER_AES_128_CB
133
133
if (is_string ($ cipher )) {
134
134
$ cipher = strtoupper ($ cipher );
135
135
$ cipher = \str_replace ('- ' , '_ ' , $ cipher );
136
- if (defined ('OPENSSL_CIPHER_ ' . $ cipher )) {
137
- $ cipher = constant ('OPENSSL_CIPHER_ ' . $ cipher );
136
+ if (defined ('OPENSSL_CIPHER_ ' . $ cipher )) {
137
+ $ cipher = constant ('OPENSSL_CIPHER_ ' . $ cipher );
138
138
}
139
139
}
140
140
141
141
$ temp = self ::getTempFilename ();
142
- if (! openssl_pkcs7_encrypt ($ data , $ temp , (array ) $ cert , [], PKCS7_BINARY , $ cipher )) {
143
- throw new \RuntimeException (sprintf ('Failed to encrypt S/Mime message. Error: "%s". ' ,
144
- openssl_error_string ()));
142
+ if (!openssl_pkcs7_encrypt ($ data , $ temp , (array ) $ cert , [], PKCS7_BINARY , $ cipher )) {
143
+ throw new \RuntimeException (sprintf ('Failed to encrypt S/Mime message. Error: "%s". ' , openssl_error_string ()));
145
144
}
146
145
147
146
return MimePart::fromString (file_get_contents ($ temp ), false );
148
147
}
149
148
150
149
/**
151
- * @param string|MimePart $data
152
- * @param mixed $cert
153
- * @param mixed $key
150
+ * @param string|MimePart $data
151
+ * @param mixed $cert
152
+ * @param mixed $key
154
153
*
155
154
* @return MimePart
156
155
*/
@@ -161,9 +160,8 @@ public static function decrypt($data, $cert, $key = null)
161
160
}
162
161
163
162
$ temp = self ::getTempFilename ();
164
- if (! openssl_pkcs7_decrypt ($ data , $ temp , $ cert , $ key )) {
165
- throw new \RuntimeException (sprintf ('Failed to decrypt S/Mime message. Error: "%s". ' ,
166
- openssl_error_string ()));
163
+ if (!openssl_pkcs7_decrypt ($ data , $ temp , $ cert , $ key )) {
164
+ throw new \RuntimeException (sprintf ('Failed to decrypt S/Mime message. Error: "%s". ' , openssl_error_string ()));
167
165
}
168
166
169
167
return MimePart::fromString (file_get_contents ($ temp ));
@@ -172,8 +170,8 @@ public static function decrypt($data, $cert, $key = null)
172
170
/**
173
171
* Compress data.
174
172
*
175
- * @param string|MimePart $data
176
- * @param string $encoding
173
+ * @param string|MimePart $data
174
+ * @param string $encoding
177
175
*
178
176
* @return MimePart
179
177
*/
@@ -190,23 +188,23 @@ public static function compress($data, $encoding = null)
190
188
}
191
189
192
190
$ headers = [
193
- 'Content-Type ' => MimePart::TYPE_PKCS7_MIME . '; name="smime.p7z"; smime-type= ' . MimePart::SMIME_TYPE_COMPRESSED ,
194
- 'Content-Description ' => 'S/MIME Compressed Message ' ,
195
- 'Content-Disposition ' => 'attachment; filename="smime.p7z" ' ,
191
+ 'Content-Type ' => MimePart::TYPE_PKCS7_MIME . '; name="smime.p7z"; smime-type= ' . MimePart::SMIME_TYPE_COMPRESSED ,
192
+ 'Content-Description ' => 'S/MIME Compressed Message ' ,
193
+ 'Content-Disposition ' => 'attachment; filename="smime.p7z" ' ,
196
194
'Content-Transfer-Encoding ' => $ encoding ,
197
195
];
198
196
199
197
$ content = ASN1Helper::encode (
200
198
[
201
199
'contentType ' => ASN1Helper::COMPRESSED_DATA_OID ,
202
- 'content ' => [
203
- 'version ' => 0 ,
200
+ 'content ' => [
201
+ 'version ' => 0 ,
204
202
'compression ' => [
205
203
'algorithm ' => ASN1Helper::ALG_ZLIB_OID ,
206
204
],
207
205
'payload ' => [
208
206
'contentType ' => ASN1Helper::DATA_OID ,
209
- 'content ' => base64_encode (gzcompress ($ content )),
207
+ 'content ' => base64_encode (gzcompress ($ content )),
210
208
],
211
209
],
212
210
],
@@ -226,7 +224,7 @@ public static function compress($data, $encoding = null)
226
224
/**
227
225
* Decompress data.
228
226
*
229
- * @param string|MimePart $data
227
+ * @param string|MimePart $data
230
228
*
231
229
* @return MimePart
232
230
*/
@@ -258,13 +256,13 @@ public static function decompress($data)
258
256
/**
259
257
* Create a temporary file into temporary directory.
260
258
*
261
- * @param string $content
259
+ * @param string $content
262
260
*
263
261
* @return string The temporary file generated
264
262
*/
265
263
public static function getTempFilename ($ content = null )
266
264
{
267
- $ dir = sys_get_temp_dir ();
265
+ $ dir = sys_get_temp_dir ();
268
266
$ filename = tempnam ($ dir , 'phpas2_ ' );
269
267
if ($ content ) {
270
268
file_put_contents ($ filename , $ content );
0 commit comments