Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Crypt.php
Go to the documentation of this file.
1 <?php
29 {
30 
31  const TYPE_OPENSSL = 'openssl';
32  const TYPE_HASH = 'hash';
33  const TYPE_MHASH = 'mhash';
34 
35  protected static $_type = null;
36 
40  protected static $_supportedAlgosOpenssl = array(
41  'md2',
42  'md4',
43  'mdc2',
44  'rmd160',
45  'sha',
46  'sha1',
47  'sha224',
48  'sha256',
49  'sha384',
50  'sha512'
51  );
52 
56  protected static $_supportedAlgosMhash = array(
57  'adler32',
58  'crc32',
59  'crc32b',
60  'gost',
61  'haval128',
62  'haval160',
63  'haval192',
64  'haval256',
65  'md4',
66  'md5',
67  'ripemd160',
68  'sha1',
69  'sha256',
70  'tiger',
71  'tiger128',
72  'tiger160'
73  );
74 
81  public static function hash($algorithm, $data, $binaryOutput = false)
82  {
83  $algorithm = strtolower($algorithm);
84  if (function_exists($algorithm)) {
85  return $algorithm($data, $binaryOutput);
86  }
87  self::_detectHashSupport($algorithm);
88  $supportedMethod = '_digest' . ucfirst(self::$_type);
89  $result = self::$supportedMethod($algorithm, $data, $binaryOutput);
90  return $result;
91  }
92 
97  protected static function _detectHashSupport($algorithm)
98  {
99  if (function_exists('hash')) {
100  self::$_type = self::TYPE_HASH;
101  if (in_array($algorithm, hash_algos())) {
102  return;
103  }
104  }
105  if (function_exists('mhash')) {
106  self::$_type = self::TYPE_MHASH;
107  if (in_array($algorithm, self::$_supportedAlgosMhash)) {
108  return;
109  }
110  }
111  if (function_exists('openssl_digest')) {
112  if ($algorithm == 'ripemd160') {
113  $algorithm = 'rmd160';
114  }
115  self::$_type = self::TYPE_OPENSSL;
116  if (in_array($algorithm, self::$_supportedAlgosOpenssl)) {
117  return;
118  }
119  }
123  #require_once 'Zend/Crypt/Exception.php';
124  throw new Zend_Crypt_Exception('\'' . $algorithm . '\' is not supported by any available extension or native function');
125  }
126 
133  protected static function _digestHash($algorithm, $data, $binaryOutput)
134  {
135  return hash($algorithm, $data, $binaryOutput);
136  }
137 
144  protected static function _digestMhash($algorithm, $data, $binaryOutput)
145  {
146  $constant = constant('MHASH_' . strtoupper($algorithm));
147  $binary = mhash($constant, $data);
148  if ($binaryOutput) {
149  return $binary;
150  }
151  return bin2hex($binary);
152  }
153 
160  protected static function _digestOpenssl($algorithm, $data, $binaryOutput)
161  {
162  if ($algorithm == 'ripemd160') {
163  $algorithm = 'rmd160';
164  }
165  return openssl_digest($data, $algorithm, $binaryOutput);
166  }
167 
168 }
static $_type
Definition: Crypt.php:35
static hash($algorithm, $data, $binaryOutput=false)
Definition: Crypt.php:81
static _detectHashSupport($algorithm)
Definition: Crypt.php:97
const TYPE_HASH
Definition: Crypt.php:32
const TYPE_MHASH
Definition: Crypt.php:33
static $_supportedAlgosOpenssl
Definition: Crypt.php:40
static $_supportedAlgosMhash
Definition: Crypt.php:56
const TYPE_OPENSSL
Definition: Crypt.php:31