|
- <?php
- header("Content-type: text/html; charset=gb2312");
- /*
- 数据添加签名 (尾部添加自定义哈希函数校验码防非法篡改)
- 参数1:要添加签名的数据
- 参数2:签名密码 (允许留空) 将拼接数据尾部一起加密,请勿通过网络传输,不正确的密码将无法通过验证。
- 参数3:自定义散列函数名 (留空默认为md5)
- 回调的函数名必须真实有效,需要提供两个参数:
- 参数1:规定要计算的字符串。
- 参数2:规定十六进制或二进制输出格式(默认提交TRUE):
- TRUE - 原始 16 字符二进制格式
- FALSE - 默认。32 字符十六进制数
- 本程序只对数据尾部添加hash签名,确保数据存储、传输是正确的,本函数不加密数据。
- 签名后不能改变数据长度以及任何1字节,否则将无效。
- */
- function hash_sign_encode($data,$password=null,$hash_name = "md5"){
- return $data.$hash_name($data.$password,1);
- }
- /*
- 数据验证并解除签名(无效签名返回空)
- 参数见 hash_sign_encode() 函数
- */
- function hash_sign_decode($data,$password=null,$hash_name = "md5"){
- $hl = strlen($hash_name(null,1));
- $hash = substr($data,-$hl);
- $data= substr($data,0,-$hl);
- if($hash_name($data.$password,1) != $hash){
- return;
- }
- return $data;
- }
- $a = hash_sign_encode("Hello World!Hello World!Hello World!Hello World!Hello World! 中文测试","123456","sha1");
- echo $a."<br>";
- echo hash_sign_decode($a,"123456","sha1");
复制代码 |
|