| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Trait implementation of the hashing output formats for digestion algorithms. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace CryptoManana\Core\Traits\MessageDigestion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface as DigestionFormatsSpecification; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use CryptoManana\Core\StringBuilder as StringBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Trait DigestionFormatsTrait - Reusable implementation of `DigestionFormatsInterface`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @see \CryptoManana\Core\Interfaces\MessageDigestion\DigestionFormatsInterface The abstract specification. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package CryptoManana\Core\Traits\MessageDigestion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property int $digestFormat The digest output format property storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @mixin DigestionFormatsSpecification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | trait DigestionFormatsTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * Internal method for converting the digest's output format representation via the chosen format. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param string $digest The output digest. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return string The input data with proper salting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 608 |  |     protected function changeOutputFormat($digest) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 608 |  |         switch ($this->digestFormat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 608 |  |             case self::DIGEST_OUTPUT_HEX_LOWER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 270 |  |                 break; // Comes already in lower, can be skipped | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 428 |  |             case self::DIGEST_OUTPUT_HEX_UPPER: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 353 |  |                 $digest = StringBuilder::stringToUpper($digest); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 353 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 165 |  |             case self::DIGEST_OUTPUT_BASE_64: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 105 |  |                 $digest = base64_encode(pack('H*', $digest)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 105 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 165 |  |             case self::DIGEST_OUTPUT_BASE_64_URL: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 60 |  |                 $digest = base64_encode(pack('H*', $digest)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 60 |  |                 $digest = StringBuilder::stringReplace(['+', '/', '='], ['-', '_', ''], $digest); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 60 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             default: // case self::DIGEST_OUTPUT_RAW: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 165 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 608 |  |         return $digest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Setter for the digest format code property. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param int $digestFormat The digest format code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return $this The hash algorithm object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @throws \Exception Validation errors. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 417 |  |     public function setDigestFormat($digestFormat) | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 417 |  |         $digestFormat = filter_var( | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 417 |  |             $digestFormat, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 417 |  |             FILTER_VALIDATE_INT, | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 417 |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 417 |  |                 "options" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 417 |  |                     "min_range" => self::DIGEST_OUTPUT_RAW, | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 417 |  |                     "max_range" => self::DIGEST_OUTPUT_BASE_64_URL, | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 417 |  |                 ], | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 417 |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 417 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 417 |  |         if ($digestFormat === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 62 |  |             throw new \InvalidArgumentException( | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 62 |  |                 'Digest output format mode must be an integer between ' . | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 62 |  |                 self::DIGEST_OUTPUT_RAW . ' and ' . self::DIGEST_OUTPUT_BASE_64_URL . '.' | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 62 |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 355 |  |         $this->digestFormat = $digestFormat; | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 355 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Getter for the digest format code property. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return int The digest format code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 109 |  |     public function getDigestFormat() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 109 |  |         return $this->digestFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  |