eva-engine /vendor/Eva/Stdlib/String/Hash.php

Language PHP Lines 172
MD5 Hash 00ce19b9633a62e0b656cc3a782ba348
Repository https://github.com/zc1415926/eva-engine.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
 * EvaEngine
 *
 * @link      https://github.com/AlloVince/eva-engine
 * @copyright Copyright (c) 2012 AlloVince (http://avnpc.com/)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Eva_Api.php
 * @author    AlloVince
 */

 namespace Eva\Stdlib\String;

 /**
 * Unique Hash
 * From : http://blog.kevburnsjr.com/php-unique-hash
 * 
 * @category  Eva
 * @package   Eva_Stdlib
 */
 class Hash
 {
     public static function guid()
     {
         if (function_exists('com_create_guid') === true)
         {
             return trim(com_create_guid(), '{}');
         }
         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));
     }


     public static function shortHashArray($input) {
         $base32 =  array(
             "a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ,  
             "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" ,  
             "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" ,  
             "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" ,  
             "6" , "7" , "8" , "9" , "A" , "B" , "C" , "D" ,  
             "E" , "F" , "G" , "H" , "I" , "J" , "K" , "L" ,  
             "M" , "N" , "O" , "P" , "Q" , "R" , "S" , "T" ,  
             "U" , "V" , "W" , "X" , "Y" , "Z"  
         );  


         $hex = md5($input);
         $hexLen = strlen($hex);
         $subHexLen = $hexLen / 8;
         $output = array();

         for ($i = 0; $i < $subHexLen; $i++) {
             $subHex = substr ($hex, $i * 8, 8);
             $int = 0x3FFFFFFF & (1 * ('0x'.$subHex));
             $out = '';

             for ($j = 0; $j < 6; $j++) {
                 $val = 0x0000003D & $int;
                 $out .= $base32[$val];
                 $int = $int >> 5;
             }

             $output[] = $out;
         }

         return $output;
     }


     public static function uniqueHash($returnArray = false)
     {
         $guid = self::guid();
         $guid = str_replace('-', '', $guid);

         $hashArray = self::shortHashArray($guid);
         return false === $returnArray ? $hashArray[rand(0, 3)] : $hashArray;
     }

     /**
     * Translates a number to a short alhanumeric version
     *
     * Translated any number up to 9007199254740992
     * to a shorter version in letters e.g.:
     * 9007199254740989 --> PpQXn7COf
     * @author	Kevin van Zonneveld <kevin@vanzonneveld.net>
     * @author	Simon Franz
     * @author	Deadfish
     * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net)
     * @license   http://www.opensource.org/licenses/bsd-license.php New BSD Licence
     * @version   SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $
     * @link	  http://kevin.vanzonneveld.net/
     *
     * @param mixed   $in	  String or long input to translate
     * @param boolean $toNum  Reverses translation when true
     * @param mixed   $padUp  Number or boolean padds the result up to a specified length
     * @param string  $passKey Supplying a password makes it harder to calculate the original ID
     *
     * @return mixed string or long
     */
     public static function shortHash($in = null, $toNum = false, $padUp = false, $passKey = null)
     {
         if(!$in){
             $in = mt_rand();
             $padUp = 6;
         }

         $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         if ($passKey !== null) {
             // Although this function's purpose is to just make the
             // ID short - and not so much secure,
             // with this patch by Simon Franz (http://blog.snaky.org/)
             // you can optionally supply a password to make it harder
             // to calculate the corresponding numeric ID

             for ($n = 0; $n<strlen($index); $n++) {
                 $i[] = substr( $index,$n ,1);
             }

             $passhash = hash('sha256',$passKey);
             $passhash = (strlen($passhash) < strlen($index)) ? hash('sha512',$passKey) : $passhash;

             for ($n=0; $n < strlen($index); $n++) {
                 $p[] =  substr($passhash, $n ,1);
             }

             array_multisort($p,  SORT_DESC, $i);
             $index = implode($i);
         }

         $base  = strlen($index);

         if ($toNum) {
             // Digital number  <<--  alphabet letter code
             $in  = strrev($in);
             $out = 0;
             $len = strlen($in) - 1;
             for ($t = 0; $t <= $len; $t++) {
                 $bcpow = bcpow($base, $len - $t);
                 $out   = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
             }

             if (is_numeric($padUp)) {
                 $padUp--;
                 if ($padUp > 0) {
                     $out -= pow($base, $padUp);
                 }
             }
             $out = sprintf('%F', $out);
             $out = substr($out, 0, strpos($out, '.'));
         } else {
             // Digital number  -->>  alphabet letter code
             if (is_numeric($padUp)) {
                 $padUp--;
                 if ($padUp > 0) {
                     $in += pow($base, $padUp);
                 }
             }

             $out = "";
             for ($t = floor(log($in, $base)); $t >= 0; $t--) {
                 $bcp = bcpow($base, $t);
                 $a   = floor($in / $bcp) % $base;
                 $out = $out . substr($index, $a, 1);
                 $in  = $in - ($a * $bcp);
             }
             $out = strrev($out); // reverse
         }

         return $out;
     }

 }
Back to Top