php使用hash_hmac生成token

分类首页日期3年前访问3191评论0

hash_hmac

(PHP 5> = 5.1.2,PHP 7,PECL哈希> = 1.1)

hash_hmac — 使用HMAC方法生成键控哈希值

描述

hash_hmac (字符串 $algo ,字符串 $data ,字符串 $key [, bool $raw_output=FALSE ]):字符串

参量

algo

所选哈希算法的名称(即“ md5”,“ sha256”,“ haval160,4”等。)有关受支持算法的列表,请参见hash_hmac_algos()

data

要散列的消息。

key

用于生成消息摘要的HMAC变体的共享密钥。

raw_output

设置为时TRUE,输出原始二进制数据。 FALSE输出小写的十六进制。

返回值

返回一个字符串,其中包含已计算的消息摘要,以小写的十六raw_output进制表示,除非设置为true,否则将返回消息摘要的原始二进制表示形式。返回FALSEalgo是未知的,或者是不加密散列函数。

变更日志

描述
7.2.0禁止使用非加密哈希函数(adler32,crc32,crc32b,fnv132,fnv1a32,fnv164,fnv1a64,joaat)。

例子

Example#1 hash_hmac()示例

<?php
echo hash_hmac('ripemd160''The quick brown fox jumped over the lazy dog.''secret');
?>

上面的示例将输出:

b8e7ae12510bdfb1812e463a7f086122cf37e4f7

//fastadmin的token加密方式

$config = \think\Config::get('token');

/**
     * 获取全球唯一标识
     * @return string
     */

$token = return sprintf(

            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(00xffff),
            mt_rand(00xffff),
            mt_rand(00xffff),
            mt_rand(00x0fff| 0x4000,
            mt_rand(00x3fff| 0x8000,
            mt_rand(00xffff),
            mt_rand(00xffff),
            mt_rand(00xffff)

        );

return hash_hmac($config['hashalgo'], $token, $config['key']);