php加密算法hmac[存档]

md5被破解已经很早的事了,所以这个比md5好一点加密算法,还是值得存下来的。
<?php
/**
 * @param String $data 预加密数据
 * @param String $key  密钥
 * @return String
 */
function hmac($data, $key){
    if (function_exists('hash_hmac')) {
        return hash_hmac('md5', $data, $key);
    }

    $key = (strlen($key) > 64) ? pack('H32', 'md5') : str_pad($key, 64, chr(0));
    $ipad = substr($key,0, 64) ^ str_repeat(chr(0×36), 64);
    $opad = substr($key,0, 64) ^ str_repeat(chr(0x5C), 64);
    return md5($opad.pack('H32', md5($ipad.$data)));
}

//———————————————
// 使用例子
function getKey () {
    $tmp = array_merge(range(0, 9), range('A', 'Z'));
    $key = '';
    for ($i = 0; $i < 16; $i++) {
        $key .= $tmp[mt_rand(0, 35)];
    }
    return md5($key);
}

$password = '123456';
$key = getKey(); // 密钥也可以是验证码md5后
$hmac = hmac($password, $key);

// 把私有密钥和密码分别存如数据库
print $key . "\n";
print $hmac;
?>
 

版权所有© HzlzH | 本文采用 BY-NC-SA 进行授权
转载需注明 转自: 《php加密算法hmac[存档]

相关文章

{ Leave a Reply ? }

  1. wontao China Google Chrome Windows

    google搜索[php 加密],你是第三!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)