| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Trait implementation of the signature data output formats for signature algorithms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace CryptoManana\Core\Traits\MessageEncryption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CryptoManana\Core\Interfaces\MessageEncryption\SignatureDataFormatsInterface as SignatureDataFormatsSpecification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use CryptoManana\Core\StringBuilder as StringBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Trait SignatureDataFormatsTrait - Reusable implementation of `SignatureDataFormatsInterface`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @see \CryptoManana\Core\Interfaces\MessageDigestion\SignatureDataFormatsInterface The abstract specification. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package CryptoManana\Core\Traits\MessageEncryption | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property int $signatureFormat The output signature format property storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @mixin SignatureDataFormatsSpecification | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | trait SignatureDataFormatsTrait | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * Internal method for converting format after signing operations. | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $bytes The bytes for conversion. | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @note The parameter is passed via reference from the main logical method for performance reasons. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 60 |  |     protected function signingFormat(&$bytes) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 60 |  |         switch ($this->signatureFormat) { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 60 |  |             case self::SIGNATURE_OUTPUT_HEX_LOWER: | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 12 |  |                 $bytes = bin2hex($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 12 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 52 |  |             case self::SIGNATURE_OUTPUT_HEX_UPPER: | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 52 |  |                 $bytes = StringBuilder::stringToUpper(bin2hex($bytes)); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 52 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 4 |  |             case self::SIGNATURE_OUTPUT_BASE_64: | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 4 |  |                 $bytes = base64_encode($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 4 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 4 |  |             case self::SIGNATURE_OUTPUT_BASE_64_URL: | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 4 |  |                 $bytes = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], base64_encode($bytes)); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 4 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * Internal method for converting from HEX formatted string after verification operations. | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @param string $bytes The bytes for conversion. | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * @note The parameter is passed via reference from the main logical method for performance reasons. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 38 |  |     protected function verificationFormatHex(&$bytes) | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 38 |  |         if (preg_match('/^[a-f0-9]+$/', $bytes)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 12 |  |             $bytes = hex2bin($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 30 |  |         } elseif (preg_match('/^[A-F0-9]+$/', $bytes)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 26 |  |             $bytes = hex2bin(StringBuilder::stringToLower($bytes)); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * Internal method for converting from Base64 formatted string after verification operations. | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * @param string $bytes The bytes for conversion. | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @note The parameter is passed via reference from the main logical method for performance reasons. | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |     protected function verificationFormatBase64(&$bytes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |         if (preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $bytes) && StringBuilder::stringLength($bytes) % 4 === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |             $bytes = base64_decode($bytes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 4 |  |         } elseif (preg_match('/^[a-zA-Z0-9_-]+$/', $bytes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 4 |  |             $bytes = StringBuilder::stringReplace(['-', '_'], ['+', '/'], $bytes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 4 |  |             $bytes .= str_repeat('=', StringBuilder::stringLength($bytes) % 4); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 | 4 |  |             $bytes = base64_decode($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * Internal method for converting format after verification operations. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $bytes The bytes for conversion. | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @note The parameter is passed via reference from the main logical method for performance reasons. | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 38 |  |     protected function verificationFormat(&$bytes) | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 38 |  |         $isHex = in_array( | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 38 |  |             $this->signatureFormat, | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 38 |  |             [self::SIGNATURE_OUTPUT_HEX_LOWER, self::SIGNATURE_OUTPUT_HEX_UPPER], | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 38 |  |             true | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 38 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 38 |  |         $isBase64 = in_array( | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 38 |  |             $this->signatureFormat, | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 38 |  |             [self::SIGNATURE_OUTPUT_BASE_64, self::SIGNATURE_OUTPUT_BASE_64_URL], | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 38 |  |             true | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 38 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 38 |  |         if ($isHex) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 38 |  |             $this->verificationFormatHex($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 | 4 |  |         } elseif ($isBase64) { | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 4 |  |             $this->verificationFormatBase64($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * Internal method for converting the output format representation via the chosen format. | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * @param string $bytes The bytes for conversion. | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @param bool|int|null $direction Flag for signing direction (sign => `true` or verify => `false`). | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @return string The formatted bytes. | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 64 |  |     protected function changeOutputFormat($bytes, $direction = true) | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 64 |  |         if ($this->signatureFormat === self::SIGNATURE_OUTPUT_RAW) { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 4 |  |             return $bytes; | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 64 |  |         if ($direction == true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 | 60 |  |             $this->signingFormat($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 38 |  |             $this->verificationFormat($bytes); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 | 64 |  |         return $bytes; | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * Setter for the output signature format code property. | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * @param int $signatureFormat The output signature format code. | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      * @return $this The signature algorithm object. | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |      * @throws \Exception Validation errors. | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 143 | 16 |  |     public function setSignatureFormat($signatureFormat) | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 16 |  |         $signatureFormat = filter_var( | 
            
                                                                        
                            
            
                                    
            
            
                | 146 | 16 |  |             $signatureFormat, | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 16 |  |             FILTER_VALIDATE_INT, | 
            
                                                                        
                            
            
                                    
            
            
                | 148 | 16 |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 149 | 16 |  |                 "options" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 16 |  |                     "min_range" => self::SIGNATURE_OUTPUT_RAW, | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 16 |  |                     "max_range" => self::SIGNATURE_OUTPUT_BASE_64_URL, | 
            
                                                                        
                            
            
                                    
            
            
                | 152 | 16 |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 16 |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 154 | 16 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 156 | 16 |  |         if ($signatureFormat === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 4 |  |             throw new \InvalidArgumentException( | 
            
                                                                        
                            
            
                                    
            
            
                | 158 | 4 |  |                 'The output format mode must be an integer between ' . | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 4 |  |                 self::SIGNATURE_OUTPUT_RAW . ' and ' . self::SIGNATURE_OUTPUT_BASE_64_URL . '.' | 
            
                                                                        
                            
            
                                    
            
            
                | 160 | 4 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 163 | 12 |  |         $this->signatureFormat = $signatureFormat; | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 165 | 12 |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * Getter for the output signature format code property. | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * @return int The output signature format code. | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 12 |  |     public function getSignatureFormat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 175 | 12 |  |         return $this->signatureFormat; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 177 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 178 |  |  |  |