@@ -54,7 +54,9 @@ class CookieComponent extends Component {
54
54
* a secure connection exists.
55
55
* - `key` - Encryption key.
56
56
* - `httpOnly` - Set to true to make HTTP only cookies. Cookies that are HTTP only
57
- * are not accessible in JavaScript. Default false
57
+ * are not accessible in JavaScript. Default false.
58
+ * - `encryption` - Type of encryption to use. Defaults to 'aes'.
59
+ *
58
60
* @var array
59
61
*/
60
62
protected $ _defaultConfig = [
@@ -64,7 +66,8 @@ class CookieComponent extends Component {
64
66
'domain ' => '' ,
65
67
'secure ' => false ,
66
68
'key ' => null ,
67
- 'httpOnly ' => false
69
+ 'httpOnly ' => false ,
70
+ 'encryption ' => 'aes '
68
71
];
69
72
70
73
/**
@@ -77,15 +80,6 @@ class CookieComponent extends Component {
77
80
*/
78
81
protected $ _values = array ();
79
82
80
- /**
81
- * Type of encryption to use.
82
- *
83
- * Defaults to Security::encrypt(); or AES encryption.
84
- *
85
- * @var string
86
- */
87
- protected $ _type = 'aes ' ;
88
-
89
83
/**
90
84
* Used to reset cookie time if $expire is passed to CookieComponent::write()
91
85
*
@@ -340,23 +334,26 @@ public function destroy() {
340
334
}
341
335
342
336
/**
343
- * Will allow overriding default encryption method. Use this method
344
- * in ex: AppController::beforeFilter() before you have read or
345
- * written any cookies.
337
+ * Get / set encryption type. Use this method in ex: AppController::beforeFilter()
338
+ * before you have read or written any cookies.
346
339
*
347
340
* @param string $type Encryption method
348
- * @return void
341
+ * @return string
349
342
* @throws \Cake\Error\Exception When an unknown type is used.
350
343
*/
351
- public function type ($ type = 'aes ' ) {
344
+ public function encryption ($ type = null ) {
345
+ if ($ type === null ) {
346
+ return $ this ->_config ['encryption ' ];
347
+ }
348
+
352
349
$ availableTypes = [
353
350
'rijndael ' ,
354
351
'aes '
355
352
];
356
353
if (!in_array ($ type , $ availableTypes )) {
357
354
throw new Error \Exception ('You must use rijndael, or aes for cookie encryption type ' );
358
355
}
359
- $ this ->_type = $ type ;
356
+ $ this ->config ( ' encryption ' , $ type) ;
360
357
}
361
358
362
359
/**
@@ -447,10 +444,10 @@ protected function _encrypt($value) {
447
444
return $ value ;
448
445
}
449
446
$ prefix = "Q2FrZQ==. " ;
450
- if ($ this ->_type === 'rijndael ' ) {
447
+ if ($ this ->_config [ ' encryption ' ] === 'rijndael ' ) {
451
448
$ cipher = Security::rijndael ($ value , $ this ->_config ['key ' ], 'encrypt ' );
452
449
}
453
- if ($ this ->_type === 'aes ' ) {
450
+ if ($ this ->_config [ ' encryption ' ] === 'aes ' ) {
454
451
$ cipher = Security::encrypt ($ value , $ this ->_config ['key ' ]);
455
452
}
456
453
return $ prefix . base64_encode ($ cipher );
@@ -464,7 +461,6 @@ protected function _encrypt($value) {
464
461
*/
465
462
protected function _decrypt ($ values ) {
466
463
$ decrypted = array ();
467
- $ type = $ this ->_type ;
468
464
469
465
foreach ((array )$ values as $ name => $ value ) {
470
466
if (is_array ($ value )) {
@@ -491,10 +487,10 @@ protected function _decode($value) {
491
487
return $ this ->_explode ($ value );
492
488
}
493
489
$ value = base64_decode (substr ($ value , strlen ($ prefix )));
494
- if ($ this ->_type === 'rijndael ' ) {
490
+ if ($ this ->_config [ ' encryption ' ] === 'rijndael ' ) {
495
491
$ plain = Security::rijndael ($ value , $ this ->_config ['key ' ], 'decrypt ' );
496
492
}
497
- if ($ this ->_type === 'aes ' ) {
493
+ if ($ this ->_config [ ' encryption ' ] === 'aes ' ) {
498
494
$ plain = Security::decrypt ($ value , $ this ->_config ['key ' ]);
499
495
}
500
496
return $ this ->_explode ($ plain );
0 commit comments