PEEL Shopping
Open source ecommerce : PEEL Shopping
SHA256.php
Go to the documentation of this file.
1 <?php
2 // This file should be in UTF8 without BOM - Accents examples: éèê
3 /*
4  * Transparent SHA-256 Implementation for PHP 4 and PHP 5
5  *
6  * Author: Perry McGee (pmcgee@nanolink.ca)
7  * Website: http://www.nanolink.ca/pub/sha256
8  *
9  * Copyright (C) 2006,2007,2008,2009 Nanolink Solutions
10  *
11  * Created: Feb 11, 2006
12  *
13  * This library is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU Lesser General Public
15  * License as published by the Free Software Foundation; either
16  * version 2.1 of the License, or (at your option) any later version.
17  *
18  * This library is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  * Lesser General Public License for more details.
22 
23  * You should have received a copy of the GNU Lesser General Public
24  * License along with this library; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26  * or see <http://www.gnu.org/licenses/>.
27  *
28  * Include:
29  *
30  * require_once("[path/]sha256.inc.php");
31  *
32  * Usage Options:
33  *
34  * 1) $shaStr = hash('sha256', $string_to_hash);
35  *
36  * 2) $shaStr = sha256($string_to_hash[, bool ignore_php5_hash = false]);
37  *
38  * 3) $obj = new nanoSha2([bool $upper_case_output = false]);
39  * $shaStr = $obj->hash($string_to_hash[, bool $ignore_php5_hash = false]);
40  *
41  * Reference: http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html
42  *
43  * 2007-12-13: Cleaned up for initial public release
44  * 2008-05-10: Moved all helper functions into a class. API access unchanged.
45  * 2009-06-23: Created abstraction of hash() routine
46  * 2009-07-23: Added detection of 32 vs 64bit platform, and patches.
47  * Ability to define "_NANO_SHA2_UPPER" to yeild upper case hashes.
48  * 2009-08-01: Added ability to attempt to use mhash() prior to running pure
49  * php code.
50  *
51  * NOTE: Some sporadic versions of PHP do not handle integer overflows the
52  * same as the majority of builds. If you get hash results of:
53  * 7fffffff7fffffff7fffffff7fffffff7fffffff7fffffff7fffffff7fffffff
54  *
55  * If you do not have permissions to change PHP versions (if you did
56  * you'd probably upgrade to PHP 5 anyway) it is advised you install a
57  * module that will allow you to use their hashing routines, examples are:
58  * - mhash module : http://ca3.php.net/mhash
59  * - Suhosin : http://www.hardened-php.net/suhosin/
60  *
61  * If you install the Suhosin module, this script will transparently
62  * use their routine and define the PHP routine as _nano_sha256().
63  *
64  * If the mhash module is present, and $ignore_php5_hash = false the
65  * script will attempt to use the output from mhash prior to running
66  * the PHP code.
67  */
68 if (!class_exists('nanoSha2')) {
69  class nanoSha2 {
70  // php 4 - 5 compatable class properties
71  var $toUpper;
72  var $platform;
73  // Php 4 - 6 compatable constructor
74  function nanoSha2($toUpper = false)
75  {
76  // Determine if the caller wants upper case or not.
77  $this->toUpper = is_bool($toUpper)
78  ? $toUpper
79  : ((defined('_NANO_SHA2_UPPER')) ? true : false);
80  // Deteremine if the system is 32 or 64 bit.
81  $tmpInt = (int)4294967295;
82  $this->platform = ($tmpInt > 0) ? 64 : 32;
83  }
84  // Do the SHA-256 Padding routine (make input a multiple of 512 bits)
85  function char_pad($str)
86  {
87  $tmpStr = $str;
88 
89  $l = strlen($tmpStr) * 8; // # of bits from input string
90 
91  $tmpStr .= "\x80"; // append the "1" bit followed by 7 0's
92 
93  $k = (512 - (($l + 8 + 64) % 512)) / 8; // # of 0 bytes to append
94  $k += 4; // PHP Strings will never exceed (2^31)-1, 1st 32bits of
95  // the 64-bit value representing $l can be all 0's
96  for ($x = 0; $x < $k; $x++) {
97  $tmpStr .= "\0";
98  }
99  // append the 32-bits representing # of bits from input string ($l)
100  $tmpStr .= chr((($l >> 24) &0xFF));
101  $tmpStr .= chr((($l >> 16) &0xFF));
102  $tmpStr .= chr((($l >> 8) &0xFF));
103  $tmpStr .= chr(($l &0xFF));
104 
105  return $tmpStr;
106  }
107  // Here are the bitwise and functions as defined in FIPS180-2 Standard
108  function addmod2n($x, $y, $n = 4294967296) // Z = (X + Y) mod 2^32
109  {
110  $mask = 0x80000000;
111 
112  if ($x < 0) {
113  $x &= 0x7FFFFFFF;
114  $x = (float)$x + $mask;
115  }
116 
117  if ($y < 0) {
118  $y &= 0x7FFFFFFF;
119  $y = (float)$y + $mask;
120  }
121 
122  $r = $x + $y;
123 
124  if ($r >= $n) {
125  while ($r >= $n) {
126  $r -= $n;
127  }
128  }
129 
130  return (int)$r;
131  }
132  // Logical bitwise right shift (PHP default is arithmetic shift)
133  function SHR($x, $n) // x >> n
134  {
135  if ($n >= 32) { // impose some limits to keep it 32-bit
136  return (int)0;
137  }
138 
139  if ($n <= 0) {
140  return (int)$x;
141  }
142 
143  $mask = 0x40000000;
144 
145  if ($x < 0) {
146  $x &= 0x7FFFFFFF;
147  $mask = $mask >> ($n-1);
148  return ($x >> $n) | $mask;
149  }
150 
151  return (int)$x >> (int)$n;
152  }
153 
154  function ROTR($x, $n)
155  {
156  return (int)(($this->SHR($x, $n) | ($x << (32 - $n)) &0xFFFFFFFF));
157  }
158  function Ch($x, $y, $z)
159  {
160  return ($x &$y) ^ ((~$x) &$z);
161  }
162  function Maj($x, $y, $z)
163  {
164  return ($x &$y) ^ ($x &$z) ^ ($y &$z);
165  }
166  function Sigma0($x)
167  {
168  return (int) ($this->ROTR($x, 2) ^ $this->ROTR($x, 13) ^ $this->ROTR($x, 22));
169  }
170  function Sigma1($x)
171  {
172  return (int) ($this->ROTR($x, 6) ^ $this->ROTR($x, 11) ^ $this->ROTR($x, 25));
173  }
174  function sigma_0($x)
175  {
176  return (int) ($this->ROTR($x, 7) ^ $this->ROTR($x, 18) ^ $this->SHR($x, 3));
177  }
178  function sigma_1($x)
179  {
180  return (int) ($this->ROTR($x, 17) ^ $this->ROTR($x, 19) ^ $this->SHR($x, 10));
181  }
182 
183  /*
184  * Custom functions to provide PHP support
185  */
186  // split a byte-string into integer array values
187  function int_split($input)
188  {
189  $l = strlen($input);
190 
191  if ($l <= 0) {
192  return (int)0;
193  }
194 
195  if (($l % 4) != 0) { // invalid input
196  return false;
197  }
198 
199  for ($i = 0; $i < $l; $i += 4) {
200  $int_build = (ord($input[$i]) << 24);
201  $int_build += (ord($input[$i + 1]) << 16);
202  $int_build += (ord($input[$i + 2]) << 8);
203  $int_build += (ord($input[$i + 3]));
204 
205  $result[] = $int_build;
206  }
207 
208  return $result;
209  }
210 
218  function hash($str, $ig_func = false)
219  {
220  unset($binStr); // binary representation of input string
221  unset($hexStr); // 256-bit message digest in readable hex format
222 
223  // check for php's internal sha256 function, ignore if ig_func==true
224  if ($ig_func == false) {
225  if (function_exists('hash') && version_compare(PHP_VERSION, '5.1.2', '>=')) {
226  return hash("sha256", $str, false);
227  } elseif (function_exists('mhash') && defined('MHASH_SHA256')) {
228  return base64_encode(bin2hex(mhash(MHASH_SHA256, $str)));
229  }
230  }
231 
232  /*
233  * SHA-256 Constants
234  * Sequence of sixty-four constant 32-bit words representing the
235  * first thirty-two bits of the fractional parts of the cube roots
236  * of the first sixtyfour prime numbers.
237  */
238  $K = array((int)0x428a2f98, (int)0x71374491, (int)0xb5c0fbcf,
239  (int)0xe9b5dba5, (int)0x3956c25b, (int)0x59f111f1,
240  (int)0x923f82a4, (int)0xab1c5ed5, (int)0xd807aa98,
241  (int)0x12835b01, (int)0x243185be, (int)0x550c7dc3,
242  (int)0x72be5d74, (int)0x80deb1fe, (int)0x9bdc06a7,
243  (int)0xc19bf174, (int)0xe49b69c1, (int)0xefbe4786,
244  (int)0x0fc19dc6, (int)0x240ca1cc, (int)0x2de92c6f,
245  (int)0x4a7484aa, (int)0x5cb0a9dc, (int)0x76f988da,
246  (int)0x983e5152, (int)0xa831c66d, (int)0xb00327c8,
247  (int)0xbf597fc7, (int)0xc6e00bf3, (int)0xd5a79147,
248  (int)0x06ca6351, (int)0x14292967, (int)0x27b70a85,
249  (int)0x2e1b2138, (int)0x4d2c6dfc, (int)0x53380d13,
250  (int)0x650a7354, (int)0x766a0abb, (int)0x81c2c92e,
251  (int)0x92722c85, (int)0xa2bfe8a1, (int)0xa81a664b,
252  (int)0xc24b8b70, (int)0xc76c51a3, (int)0xd192e819,
253  (int)0xd6990624, (int)0xf40e3585, (int)0x106aa070,
254  (int)0x19a4c116, (int)0x1e376c08, (int)0x2748774c,
255  (int)0x34b0bcb5, (int)0x391c0cb3, (int)0x4ed8aa4a,
256  (int)0x5b9cca4f, (int)0x682e6ff3, (int)0x748f82ee,
257  (int)0x78a5636f, (int)0x84c87814, (int)0x8cc70208,
258  (int)0x90befffa, (int)0xa4506ceb, (int)0xbef9a3f7,
259  (int)0xc67178f2);
260  // Pre-processing: Padding the string
261  $binStr = $this->char_pad($str);
262  // Parsing the Padded Message (Break into N 512-bit blocks)
263  $M = str_split($binStr, 64);
264  // Set the initial hash values
265  $h[0] = (int)0x6a09e667;
266  $h[1] = (int)0xbb67ae85;
267  $h[2] = (int)0x3c6ef372;
268  $h[3] = (int)0xa54ff53a;
269  $h[4] = (int)0x510e527f;
270  $h[5] = (int)0x9b05688c;
271  $h[6] = (int)0x1f83d9ab;
272  $h[7] = (int)0x5be0cd19;
273  // loop through message blocks and compute hash. ( For i=1 to N : )
274  $N = count($M);
275  for ($i = 0; $i < $N; $i++) {
276  // Break input block into 16 32bit words (message schedule prep)
277  $MI = $this->int_split($M[$i]);
278  // Initialize working variables
279  $_a = (int)$h[0];
280  $_b = (int)$h[1];
281  $_c = (int)$h[2];
282  $_d = (int)$h[3];
283  $_e = (int)$h[4];
284  $_f = (int)$h[5];
285  $_g = (int)$h[6];
286  $_h = (int)$h[7];
287  unset($_s0);
288  unset($_s1);
289  unset($_T1);
290  unset($_T2);
291  $W = array();
292  // Compute the hash and update
293  for ($t = 0; $t < 16; $t++) {
294  // Prepare the first 16 message schedule values as we loop
295  $W[$t] = $MI[$t];
296  // Compute hash
297  $_T1 = $this->addmod2n($this->addmod2n($this->addmod2n($this->addmod2n($_h, $this->Sigma1($_e)), $this->Ch($_e, $_f, $_g)), $K[$t]), $W[$t]);
298  $_T2 = $this->addmod2n($this->Sigma0($_a), $this->Maj($_a, $_b, $_c));
299  // Update working variables
300  $_h = $_g;
301  $_g = $_f;
302  $_f = $_e;
303  $_e = $this->addmod2n($_d, $_T1);
304  $_d = $_c;
305  $_c = $_b;
306  $_b = $_a;
307  $_a = $this->addmod2n($_T1, $_T2);
308  }
309 
310  for (; $t < 64; $t++) {
311  // Continue building the message schedule as we loop
312  $_s0 = $W[($t + 1)&0x0F];
313  $_s0 = $this->sigma_0($_s0);
314  $_s1 = $W[($t + 14)&0x0F];
315  $_s1 = $this->sigma_1($_s1);
316 
317  $W[$t&0xF] = $this->addmod2n($this->addmod2n($this->addmod2n($W[$t&0xF], $_s0), $_s1), $W[($t + 9)&0x0F]);
318  // Compute hash
319  $_T1 = $this->addmod2n($this->addmod2n($this->addmod2n($this->addmod2n($_h, $this->Sigma1($_e)), $this->Ch($_e, $_f, $_g)), $K[$t]), $W[$t&0xF]);
320  $_T2 = $this->addmod2n($this->Sigma0($_a), $this->Maj($_a, $_b, $_c));
321  // Update working variables
322  $_h = $_g;
323  $_g = $_f;
324  $_f = $_e;
325  $_e = $this->addmod2n($_d, $_T1);
326  $_d = $_c;
327  $_c = $_b;
328  $_b = $_a;
329  $_a = $this->addmod2n($_T1, $_T2);
330  }
331 
332  $h[0] = $this->addmod2n($h[0], $_a);
333  $h[1] = $this->addmod2n($h[1], $_b);
334  $h[2] = $this->addmod2n($h[2], $_c);
335  $h[3] = $this->addmod2n($h[3], $_d);
336  $h[4] = $this->addmod2n($h[4], $_e);
337  $h[5] = $this->addmod2n($h[5], $_f);
338  $h[6] = $this->addmod2n($h[6], $_g);
339  $h[7] = $this->addmod2n($h[7], $_h);
340  }
341  // Convert the 32-bit words into human readable hexadecimal format.
342  $hexStr = sprintf("%08x%08x%08x%08x%08x%08x%08x%08x", $h[0], $h[1], $h[2], $h[3], $h[4], $h[5], $h[6], $h[7]);
343 
344  return ($this->toUpper) ? strtoupper($hexStr) : $hexStr;
345  }
346  }
347 }
348 
349 if (!function_exists('str_split')) {
354  function str_split($string, $split_length = 1)
355  {
356  $sign = ($split_length < 0) ? -1 : 1;
357  $strlen = strlen($string);
358  $split_length = abs($split_length);
359 
360  if (($split_length == 0) || ($strlen == 0)) {
361  $result = false;
362  } elseif ($split_length >= $strlen) {
363  $result[] = $string;
364  } else {
365  $length = $split_length;
366 
367  for ($i = 0; $i < $strlen; $i++) {
368  $i = (($sign < 0) ? $i + $length : $i);
369  $result[] = substr($string, $sign * $i, $length);
370  $i--;
371  $i = (($sign < 0) ? $i : $i + $length);
372 
373  $length = (($i + $split_length) > $strlen)
374  ? ($strlen - ($i + 1))
375  : $split_length;
376  }
377  }
378 
379  return $result;
380  }
381 }
382 
394 // 2009-07-23: Added check for function as the Suhosin plugin adds this routine.
395 if (!function_exists('sha256')) {
403  function sha256($str, $ig_func = false)
404  {
405  $obj = new nanoSha2((defined('_NANO_SHA2_UPPER')) ? true : false);
406  return $obj->hash($str, $ig_func);
407  }
408 } else {
416  function _nano_sha256($str, $ig_func = false)
417  {
418  $obj = new nanoSha2((defined('_NANO_SHA2_UPPER')) ? true : false);
419  return $obj->hash($str, $ig_func);
420  }
421 }
422 
423 if (!function_exists('hash')) {
431  function hash($algo, $data)
432  {
433  if (empty($algo) || !is_string($algo) || !is_string($data)) {
434  return false;
435  }
436 
437  if (function_exists($algo)) {
438  return $algo($data);
439  }
440  }
441 }
442 
foreach(array('date1', 'date2', 'type', 'renewals', 'width') as $item) $data
Definition: chart-data.php:29
$result
if(strlen($date2)== '10') if($type== 'users-by-age'&&a_priv('admin_users', true)) elseif($type== 'forums-count'&&a_priv('admin_content', true)) elseif($type== 'forums-categories'&&a_priv('admin_content', true)) elseif($type== 'users-count'&&a_priv('admin_users', true)) elseif($type== 'product-categories'&&a_priv('admin_products', true)) elseif($type== 'users-by-sex'&&a_priv('admin_users', true)) elseif($type== 'users-by-country'&&a_priv('admin_users', true)) elseif($type== 'sales'&&a_priv('admin_sales', true))
Definition: chart-data.php:160
global $l
Definition: afr.php:33

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:30:07 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.