#!/usr/bin/perl

use Digest::MD5 qw(md5_hex); 

if (@ARGV != 8) {
    die("usage: user pass realm uri nonce nc cnonce [response]\n");
}

my ($user, $pass, $realm, $uri, $nonce, $nc, $cnonce, $oldresponse) = @ARGV;

my ($method) = ("GET");

sub KD(@)
{
   return md5_hex(join(":", @_));
}

# 3.2.2.2 H(A1)
my $HA1 = KD($user , $realm , $pass);

# 3.2.2.3 H(A2)
my $HA2 = KD($method ,$uri);

my $response = KD($HA1, $nonce, $nc, $cnonce, "auth", $HA2);

print "User='$user' pass='$pass' realm='$realm' nonce='$nonce', count='$nc', cnonce='$cnonce' method='$method' uri='$uri'\n";

if ($response eq $oldresponse) {
    print "OK\n";
} else {
    print "H(A1)=\"${HA1}\"\n";
    print "Proxy-Authorization: digest username=\"$user\", realm=\"$realm\", nonce=\"$nonce\", uri=\"$uri\", qop=auth, nc=$nc, cnonce=\"$cnonce\", response=\"$response\"\n";
}

