现在位置: 首页  >  编程技术  >  PHP
海康SDK通信,生成登录验证时的摘要信息
0 167
<?php

// Digest=MD5(MD5(A1):<nonce>:<nc>:<cnonce>:<qop>:MD5(A2))

// A1=<user>:<realm>:<password>
// A2=<request-method>:<uri-directive-value>

// Digest qop="auth", realm="DS-GWAS0101(6233)", nonce="4e4449354e4451794f5451364e6a4a6c4e47517a4e6a673d", stale="FALSE"

// Digest username="admin", realm="DS-GWAS0101(6233)", nonce="4e4449354e4451794f5451364e6a4a6c4e47517a4e6a673d", uri="/ISAPI/Security/userCheck?format=json&timeStamp=1659163271665", response="5524a7f41c8938862f982801cbde6044", qop=auth, nc=00000001, cnonce="d24caff90e7b4910"



$sign=makeDigest();
echo $sign;


function makeDigest($method='',$uri='',$headerDigest=''){
	// 测试数据
	$method='GET';
	$uri='/ISAPI/Security/userCheck?format=json';
	$headerDigest='Digest qop="auth", realm="DS-GWAS0101(6233)", nonce="4d44466b4d5441785a4445364e6a4a6c4e4759345a6d453d", stale="TRUE"';


	function getRandStr($length){
		$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$randstr = '';
		for ($i=0;$i<$length;$i++) {$num=rand(0,strlen($str)-1);$randstr .= $str[$num];}
		return $randstr;
	}

	$pattern='/(\w+)="?([\w\d\-\(\)\.\*\/\+]+)"?/';
	preg_match_all($pattern, $headerDigest, $arr);
	$header_arr=[];
	if (count($arr)) {
		foreach ($arr[0] as $k => $v) {
			$key=$arr[1][$k];
			$value=$arr[2][$k];
			$header_arr[$key]=$value;
		}
	}

	$signParams=[
		'cnonce'=>getRandStr(16),
		'nc'=>rand(10000000,99999999),
	];

	$signArr=[
		'user'=>'admin',
		'realm'=>$header_arr['realm'],
		'password'=>'Lysoo889900',
		'request_method'=>$method,
		'uri_directive_value'=>$uri,
		'nonce'=>$header_arr['nonce'],
		'nc'=>$signParams['nc'],
		'cnonce'=>$signParams['cnonce'],
		'qop'=>$header_arr['qop'],
	];

	$A1="{$signArr['user']}:{$signArr['realm']}:{$signArr['password']}";
	$A2="{$signArr['request_method']}:{$signArr['uri_directive_value']}";
	$str=md5($A1).":{$signArr['nonce']}:{$signArr['nc']}:{$signArr['cnonce']}:{$signArr['qop']}:".md5($A2);
	$response=md5($str);

	$Digest='Digest username="'.$signArr['user'].'", realm="'.$signArr['realm'].'", nonce="'.$signArr['nonce'].'", uri="'.$signArr['uri_directive_value'].'", response="'.$response.'", qop='.$signArr['qop'].', nc='.$signArr['nc'].', cnonce="'.$signArr['cnonce'].'"';

	return $Digest;
}


上一篇:国密sm2加密解密
 评论
 站内搜索