22
33namespace Omnipay \Redsys \Message ;
44
5- use Omnipay \Common \Message \AbstractResponse as BaseAbstractResponse ;
65use Omnipay \Common \Exception \RuntimeException ;
76
87/**
9- * Abstract Response
8+ * Security
109 *
11- * This abstract class extends the base Omnipay AbstractResponse in order
12- * to provide some common encoding and decoding functions.
10+ * This class provides common encoding, decoding and signing functions.
11+ * While all of this code could be called statically, it is left as a
12+ * regular class in order to faciliate unit testing. If alternate
13+ * encryption methods are provided later, the VERSION const can be
14+ * switched to a constructor option (and validated against a whitelist).
1315 */
14- abstract class AbstractResponse extends BaseAbstractResponse
16+ class Security
1517{
18+ /** @var string */
19+ const VERSION = 'HMAC_SHA256_V1 ' ;
20+
1621 /**
1722 * Encode merchant parameters
1823 *
1924 * @param array $data The parameters to encode
2025 *
2126 * @return string Encoded data
2227 */
23- protected function encodeMerchantParameters ($ data )
28+ public function encodeMerchantParameters ($ data )
2429 {
2530 return base64_encode (json_encode ($ data ));
2631 }
@@ -32,28 +37,27 @@ protected function encodeMerchantParameters($data)
3237 *
3338 * @return array Decoded data
3439 */
35- protected function decodeMerchantParameters ($ data )
40+ public function decodeMerchantParameters ($ data )
3641 {
3742 return (array )json_decode (base64_decode (strtr ($ data , '-_ ' , '+/ ' )));
3843 }
3944
4045 /**
4146 * Encrypt message with given key and default IV
4247 *
43- * @todo function_exists() vs extension_loaded()?
44- *
4548 * @param string $message The message to encrypt
46- * @param string $key The key used to encrypt the message
49+ * @param string $key The base64-encoded key used to encrypt the message
4750 *
4851 * @return string Encrypted message
4952 *
5053 * @throws RuntimeException
5154 */
5255 protected function encryptMessage ($ message , $ key )
5356 {
57+ $ key = base64_decode ($ key );
5458 $ iv = implode (array_map ('chr ' , array (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )));
5559
56- if (function_exists ( ' mcrypt_encrypt ' )) {
60+ if ($ this -> hasValidEncryptionMethod ( )) {
5761 $ ciphertext = mcrypt_encrypt (MCRYPT_3DES , $ key , $ message , MCRYPT_MODE_CBC , $ iv );
5862 } else {
5963 throw new RuntimeException ('No valid encryption extension installed ' );
@@ -62,24 +66,43 @@ protected function encryptMessage($message, $key)
6266 return $ ciphertext ;
6367 }
6468
69+ /**
70+ * Check if the system has a valid encryption method available
71+ *
72+ * @return bool
73+ */
74+ public function hasValidEncryptionMethod ()
75+ {
76+ return extension_loaded ('mcrypt ' ) && function_exists ('mcrypt_encrypt ' );
77+ }
78+
6579 /**
6680 * Create signature hash used to verify messages
6781 *
6882 * @todo Add if-check on algorithm to match against signature version as new param?
6983 *
7084 * @param string $message The message to encrypt
7185 * @param string $salt Unique salt used to generate the ciphertext
72- * @param string $key The key used to encrypt the message
86+ * @param string $key The base64-encoded key used to encrypt the message
7387 *
7488 * @return string Generated signature
7589 */
76- protected function createSignature ($ message , $ salt , $ key )
90+ public function createSignature ($ message , $ salt , $ key )
7791 {
7892 $ ciphertext = $ this ->encryptMessage ($ salt , $ key );
7993 return base64_encode (hash_hmac ('sha256 ' , $ message , $ ciphertext , true ));
8094 }
8195
82- protected function createReturnSignature ($ message , $ salt , $ key )
96+ /**
97+ * Create signature hash used to verify messages back for Redirect gateway
98+ *
99+ * @param string $message The message to encrypt
100+ * @param string $salt Unique salt used to generate the ciphertext
101+ * @param string $key The base64-encoded key used to encrypt the message
102+ *
103+ * @return string Generated signature
104+ */
105+ public function createReturnSignature ($ message , $ salt , $ key )
83106 {
84107 return strtr ($ this ->createSignature ($ message , $ salt , $ key ), '+/ ' , '-_ ' );
85108 }
0 commit comments