来看代码了!
无聊的时候瞎写的,欢迎大虾们指正
Common\NDV.php
/**
* NDV (Not complete Digital Visa)
* 非完整数字签证
*
* +----------------------------------------------------+
* * NDV(基础函数库文件),属于KSEF(Kee Simply Efficient Framework)项目扩展库函数
* @author Kee <chinboy2012@gmail.com>
* @date 2012年12月10日
* @version Alpha 1.0
* +----------------------------------------------------+
*/
class NDV{
private $Str_list = array();
private $SafeStr = null;
private $BinCode = null;
private $Md5Code = null;
private $Sh1Code = null;
private $SafeCode = null;
public function NDVa($str, $mode = 'NDV'){
$this-> NDV_list($str);
$this-> NDV_Safe_Add();
$this-> NDV_4in1();
if($mode == 'NDV'){
//输出52位混合编码,默认选项
return $this-> SafeCode;
}elseif($mode == 'BIN'){
//输出20位二进制校验码
return $this-> BinCode;
}elseif($mode == 'MD5'){
//输出32为MD5校验码
return $this-> Md5Code;
}elseif($mode == 'SH1'){
//输出32位Sh1校验码
return $this-> Sh1Code;
}
}
private function NDV_list($str) {
$Str_Len = strlen($str); //计算字符串长度
$Str_Arr = array(); //拆解字符串数组
$Str_list = array(); //混序列表
// $mod3 = $Str_Len%3; //模3值
$Len3 = intval($Str_Len / 3); //除3取整
$list = 0;
//进行一次循环,拆解字符串,混序排列
for ($i = 0, $x = 0; $i < $Str_Len; $x++) {
$Str_Arr[$x] = md5(md5(substr($str, $i, 3)) . md5($str)); //拆解字符串,取字符串摘要获取64位混合编码
//混序排列
$this->BinCode .= $Str_Arr[$x] % 2;
if ($Str_Arr[$x] % 2 == 1) {
//如果模2值为1,则序列下沉
$Str_list[$Len3] = $Str_Arr[$x];
$Len3--;
} elseif ($Str_Arr[$x] % 2 == 0) {
//如果模2值为0,则序列上浮
$Str_list[$list] = $Str_Arr[$x];
$list++;
}
$i+=3;
}
$this->Str_list = $Str_list;
// return $this; //返回数据
}
private function NDV_Safe_Add() {
$safenum = null;
if(count($this-> Str_list) >= 16){
//如果混合序列大于或等于16组,则进行数据合并,提取512位安全编码
for($i=0; $i<16; $i++){
$this-> SafeStr .= (String)($this-> Str_list[$i]);
}
}else {
$safenum = 16-count($this-> Str_list);
for($i=count($this-> Str_list); $i<16; $i++){
//不足16组,验证数据补充
@$this-> Str_list[$i] = md5($this->BinCode.md5($this-> Str_list[$i-1]));
$this-> BinCode .= $this-> Str_list[$i]%2;
}
for($i=0; $i<16; $i++){
//提取512位混合编码
@$this-> SafeStr .= (String)($this-> Str_list[$i]);
}
}
$this-> Md5Code = md5($this-> SafeStr);
$this-> Sh1Code = substr(sha1($this-> SafeStr), 0, 32);
return $this;
}
private function NDV_4in1(){
//四选一
$se = array();
$se[0] = substr($this-> SafeStr, 0, 128);
$this-> BinCode .= $se[0]%2;
$se[1] = substr($this-> SafeStr, 128, 128);
$this-> BinCode .= $se[1]%2;
$se[2] = substr($this-> SafeStr, 256, 128);
$this-> BinCode .= $se[2]%2;
$se[3] = substr($this-> SafeStr, 384, 128);
$this-> BinCode .= $se[3]%2;
$senum = $this-> SafeStr%4;
$SafeCode = md5($se[$senum]);
if($SafeCode%2 == 0){
$SafeCode = substr($SafeCode, 0, 16);
}elseif($SafeCode%2 == 1){
$SafeCode = substr($SafeCode, 16, 16);
}
$SafeCode .= $this-> BinCode;
$this-> SafeCode = $SafeCode;
return $this;
}
}
function NDV($str , $mode = 'NDV'){
$ndv = new NDV();
return $ndv-> NDVa($str,$mode);
}
$string = '111111111111';
var_dump(NDV($string));
var_dump(NDV($string,'BIN'));
var_dump(NDV($string,'MD5'));
var_dump(NDV($string,'SH1'));