PageRenderTime 1ms CodeModel.GetById 148ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/Eva/Stdlib/String/Hash.php

https://github.com/zc1415926/eva-engine
PHP | 171 lines | 102 code | 25 blank | 44 comment | 15 complexity | 00ce19b9633a62e0b656cc3a782ba348 MD5 | raw file
  1<?php
  2/**
  3 * EvaEngine
  4 *
  5 * @link      https://github.com/AlloVince/eva-engine
  6 * @copyright Copyright (c) 2012 AlloVince (http://avnpc.com/)
  7 * @license   http://framework.zend.com/license/new-bsd New BSD License
  8 * @package   Eva_Api.php
  9 * @author    AlloVince
 10 */
 11
 12 namespace Eva\Stdlib\String;
 13
 14 /**
 15 * Unique Hash
 16 * From : http://blog.kevburnsjr.com/php-unique-hash
 17 * 
 18 * @category  Eva
 19 * @package   Eva_Stdlib
 20 */
 21 class Hash
 22 {
 23     public static function guid()
 24     {
 25         if (function_exists('com_create_guid') === true)
 26         {
 27             return trim(com_create_guid(), '{}');
 28         }
 29         return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
 30     }
 31
 32
 33     public static function shortHashArray($input) {
 34         $base32 =  array(
 35             "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ,  
 36             "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,  
 37             "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ,  
 38             "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" ,  
 39             "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" ,  
 40             "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" ,  
 41             "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" ,  
 42             "U" , "V" , "W" , "X" , "Y" , "Z"  
 43         );  
 44
 45
 46         $hex = md5($input);
 47         $hexLen = strlen($hex);
 48         $subHexLen = $hexLen / 8;
 49         $output = array();
 50
 51         for ($i = 0; $i < $subHexLen; $i++) {
 52             $subHex = substr ($hex, $i * 8, 8);
 53             $int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
 54             $out = '';
 55
 56             for ($j = 0; $j < 6; $j++) {
 57                 $val = 0x0000003D & $int;
 58                 $out .= $base32[$val];
 59                 $int = $int >> 5;
 60             }
 61
 62             $output[] = $out;
 63         }
 64
 65         return $output;
 66     }
 67
 68
 69     public static function uniqueHash($returnArray = false)
 70     {
 71         $guid = self::guid();
 72         $guid = str_replace('-', '', $guid);
 73
 74         $hashArray = self::shortHashArray($guid);
 75         return false === $returnArray ? $hashArray[rand(0, 3)] : $hashArray;
 76     }
 77
 78     /**
 79     * Translates a number to a short alhanumeric version
 80     *
 81     * Translated any number up to 9007199254740992
 82     * to a shorter version in letters e.g.:
 83     * 9007199254740989 --> PpQXn7COf
 84     * @author	Kevin van Zonneveld <kevin@vanzonneveld.net>
 85     * @author	Simon Franz
 86     * @author	Deadfish
 87     * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 88     * @license   http://www.opensource.org/licenses/bsd-license.php New BSD Licence
 89     * @version   SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
 90     * @link	  http://kevin.vanzonneveld.net/
 91     *
 92     * @param mixed   $in	  String or long input to translate
 93     * @param boolean $toNum  Reverses translation when true
 94     * @param mixed   $padUp  Number or boolean padds the result up to a specified length
 95     * @param string  $passKey Supplying a password makes it harder to calculate the original ID
 96     *
 97     * @return mixed string or long
 98     */
 99     public static function shortHash($in = null, $toNum = false, $padUp = false, $passKey = null)
100     {
101         if(!$in){
102             $in = mt_rand();
103             $padUp = 6;
104         }
105
106         $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
107         if ($passKey !== null) {
108             // Although this function's purpose is to just make the
109             // ID short - and not so much secure,
110             // with this patch by Simon Franz (http://blog.snaky.org/)
111             // you can optionally supply a password to make it harder
112             // to calculate the corresponding numeric ID
113
114             for ($n = 0; $n<strlen($index); $n++) {
115                 $i[] = substr( $index,$n ,1);
116             }
117
118             $passhash = hash('sha256',$passKey);
119             $passhash = (strlen($passhash) < strlen($index)) ? hash('sha512',$passKey) : $passhash;
120
121             for ($n=0; $n < strlen($index); $n++) {
122                 $p[] =  substr($passhash, $n ,1);
123             }
124
125             array_multisort($p,  SORT_DESC, $i);
126             $index = implode($i);
127         }
128
129         $base  = strlen($index);
130
131         if ($toNum) {
132             // Digital number  <<--  alphabet letter code
133             $in  = strrev($in);
134             $out = 0;
135             $len = strlen($in) - 1;
136             for ($t = 0; $t <= $len; $t++) {
137                 $bcpow = bcpow($base, $len - $t);
138                 $out   = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
139             }
140
141             if (is_numeric($padUp)) {
142                 $padUp--;
143                 if ($padUp > 0) {
144                     $out -= pow($base, $padUp);
145                 }
146             }
147             $out = sprintf('%F', $out);
148             $out = substr($out, 0, strpos($out, '.'));
149         } else {
150             // Digital number  -->>  alphabet letter code
151             if (is_numeric($padUp)) {
152                 $padUp--;
153                 if ($padUp > 0) {
154                     $in += pow($base, $padUp);
155                 }
156             }
157
158             $out = "";
159             for ($t = floor(log($in, $base)); $t >= 0; $t--) {
160                 $bcp = bcpow($base, $t);
161                 $a   = floor($in / $bcp) % $base;
162                 $out = $out . substr($index, $a, 1);
163                 $in  = $in - ($a * $bcp);
164             }
165             $out = strrev($out); // reverse
166         }
167
168         return $out;
169     }
170
171 }