en

designoir

Base64-Hashes

Base64.php

<?php

/**
 * class Base64
 * Copyright (C) 2006  Dao Gottwald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *   Herltestraße 12
 *   D-01307, Germany
 *
 * @version  1.0
 */

class Base64 {
    protected static $tab_fromBin = array (
        '000000' => '0', '000001' => '1', '000010' => '2', '000011' => '3',
        '000100' => '4', '000101' => '5', '000110' => '6', '000111' => '7',
        '001000' => '8', '001001' => '9', '001010' => 'a', '001011' => 'b',
        '001100' => 'c', '001101' => 'd', '001110' => 'e', '001111' => 'f',
        '010000' => 'g', '010001' => 'h', '010010' => 'i', '010011' => 'j',
        '010100' => 'k', '010101' => 'l', '010110' => 'm', '010111' => 'n',
        '011000' => 'o', '011001' => 'p', '011010' => 'q', '011011' => 'r',
        '011100' => 's', '011101' => 't', '011110' => 'u', '011111' => 'v',
        '100000' => 'w', '100001' => 'x', '100010' => 'y', '100011' => 'z',
        '100100' => 'A', '100101' => 'B', '100110' => 'C', '100111' => 'D',
        '101000' => 'E', '101001' => 'F', '101010' => 'G', '101011' => 'H',
        '101100' => 'I', '101101' => 'J', '101110' => 'K', '101111' => 'L',
        '110000' => 'M', '110001' => 'N', '110010' => 'O', '110011' => 'P',
        '110100' => 'Q', '110101' => 'R', '110110' => 'S', '110111' => 'T',
        '111000' => 'U', '111001' => 'V', '111010' => 'W', '111011' => 'X',
        '111100' => 'Y', '111101' => 'Z', '111110' => '-', '111111' => '_'
    );
    
    public static function md5 ($s) {
        return self::fromHex (md5 ($s));
    }
    
    public static function sha1 ($s) {
        return self::fromHex (sha1 ($s));
    }
    
    public static function fromHex ($s) {
        return self::fromBin (self::_hexBin ($s));
    }
    
    protected static function _hexBin ($s) {
        $s = str_split ($s, 1);
        foreach ($s as &$val) {
            $val = str_pad (base_convert ($val, 16, 2), 4, '0', STR_PAD_LEFT);
        }
        $s = implode ('', $s);
        return $s;
    }
    
    protected static function fromBin ($s) {
        if ($pad = strlen ($s) % 6) {
            $s = str_repeat ('0', 6 - $pad) . $s;
        }
        return strtr ($s, self::$tab_fromBin);
    }
}

?>

Beispiel #1: test.php

<?php

class Base64Reverse extends Base64 {
    public static function toHex ($s) {
        return self::bin2hex (self::toBin ($s));
    }
    private static function bin2hex ($bin){
        $hex = '';
        for ($i = strlen ($bin) - 4; $i >= 0; $i -= 4) {
            $hex .= dechex (bindec (substr ($bin, $i, 4)));
        }
        return strrev ($hex);
    }
    private static function toBin ($s) {
        $tab = self::$tab_fromBin;
        $tab = array_flip ($tab);
        return strtr ($s, $tab);
    }
}

$id = uniqid();

echo    sha1($id), "\n",
    base64_encode (pack('H*',sha1($id))), "\n",
    Base64::sha1($id), "\n",
    Base64Reverse::toHex(Base64::sha1($id));

?>

test.php ausführen

Kommentare

  1. bLeEd meinte am 24. August ’06, 08:48 Uhr ():
    Hallo dazio,

    Ich verstehe nicht ganz wozu das gut sein soll da es bereits base64_en/decode() gibt. Aber wer es braucht.

    lg, bLeEd
  2. Dao meinte am 24. August ’06, 09:57 Uhr ():
    bLeEd, du kannst dir ganz leicht verdeutlichen, was der Unterschied ist:
    $id = uniqid();
    echo Base64::sha1($id), "\n",
      base64_encode(sha1($id));
    base64_encode arbeitet nicht mit hexadezimalen Werten und macht md5-/sha1-Strings deshalb *länger* statt *kürzer*.

    Wie im Beispiel kann man zwar |base64_encode(pack('H*',sha1($id)))| benutzen, was meiner Methode allerdings unterlegen ist. :P
  3. </Life> meinte am 10. September ’08, 00:03 Uhr ():
    Tjo. Durch $raw_output damit dann obsolet. :)

HTML wird nicht interpretiert.

Geändert am 31. März ’06 Dão G., 2005–2010
aggressiv akt andromeda bar beine blue efeu frontal fugaetu industriell komet land noir rost rot sonnenblume splash split winter wolke zeit