一个简单的用于检验digest认证的脚本

最近做了不少需要联调的项目,常遇到接口digest认证问题,对接时常有digest请求不对的情况。为了防止扯皮,写了一个脚本用来校验digest和试错。记录一下。

HA1=MD5(A1)=MD5(username:realm:password)

HA2=MD5(A2)=MD5(method:uri)

response=MD5(HA1:nonce:nc:cnonce:qop:HA2)

#! /bin/bash

username=username""
password="password"
realm="realm"
method="GET" #may be POST
uri="/uri/path"
nonce="fQcWUysgr3pyTEjX7cEkyA==" #根据实际情况填
nc=00000001 #根据实际情况填
cnonce="ICAgICAgICAgICAgICAgICAgICAgICAgMTAzOTY1ODg=" #根据实际情况填
qop=auth #根据实际情况填

loglevel=info #please use "debug" to check detail logs

if [ "$1" ];then
    nonce=$1
    echo nonce is: $nonce
fi

if [ "$2" ];then
    cnonce=$2
    echo cnonce is: $cnonce
fi


HA1=$(echo -n $username:$realm:$password|openssl md5|cut -d" " -f2)

HD=$nonce:$nc:$cnonce:$qop

HA2=$(echo -n $method:$uri|openssl md5|cut -d" " -f2)

response=$(echo -n $HA1:$HD:$HA2|openssl md5|cut -d" " -f2)
if [ $loglevel == "debug" ];then
    echo $username:$realm:$password
    echo "HA1 is:" $HA1
    echo HD is: $HD
    echo $method:$uri
    echo "HA2 is:" $HA2
    echo $HA1:$HD:$HA2
fi




posted @ 2023-01-06 17:12  Kevin4X  阅读(139)  评论(0编辑  收藏  举报