Last active
November 16, 2016 23:48
-
-
Save facugon/1ea974c785a3db9ea37ec209d1ecde1f to your computer and use it in GitHub Desktop.
digest headers generator algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$wwwauth = $argv[1] ; | |
class DigestHeadersGenerator { | |
public function H($param) | |
{ | |
return md5($param); | |
} | |
public function KD($a,$b) | |
{ | |
return $this->H("$a:$b"); | |
} | |
public function parseHttpDigest($digest) | |
{ | |
$data = array(); | |
$parts = explode(", ", $digest); | |
foreach ($parts as $element) { | |
$bits = explode("=", $element); | |
$data[$bits[0]] = str_replace('"','', $bits[1]); | |
} | |
return $data; | |
} | |
public function headers($wwwauth, $user, $pass, $httpmethod, $uri) | |
{ | |
list($dummy_digest, $value) = split(' ', $wwwauth, 2); | |
$x = $this->parseHttpDigest($value); | |
$realm = $x['realm']; | |
$A1 = $this->H("{$user}:{$realm}:{$pass}"); | |
//echo " A1 " . $A1 . "\n" ; | |
$A2 = $this->H("{$httpmethod}:{$uri}"); | |
//echo " A2 " . $A2 . "\n" ; | |
if ($x['qop'] == 'auth') { | |
$cnonce = time(); | |
$ncvalue = 1; | |
$ncbin = (string) decbin( $ncvalue ); | |
$ncbin = substr("00000000",0,8 - strlen($ncbin)) . $ncbin; | |
$noncebit = "{$x['nonce']}:{$ncbin}:{$cnonce}:auth:{$A2}"; | |
$respdig = $this->KD($A1, $noncebit); | |
} else { | |
# FIX: handle error here | |
} | |
//echo " Response " . $respdig . "\n"; | |
$base = 'Digest username="'.$user.'", realm="'; | |
$base .= $x['realm'].'", nonce="'.$x['nonce'].'", opaque="'.$x['opaque'].'",'; | |
$base .= ' uri="'.$uri.'", cnonce="'.$cnonce; | |
$base .= '", nc="'.$ncbin.'", response="'.$respdig.'", qop="auth"'; | |
return $base; | |
} | |
} | |
function httpDigestGet($wwwauth) | |
{ | |
$gen = new DigestHeadersGenerator(); | |
$username = 'facundo.siquot@gmail.com'; | |
$password = md5('El Gran Mongo'); | |
$method = 'GET'; | |
$uri = 'http://ccmebackend.development.com/user-rest'; | |
//echo " md5 passwd " . $password . "\n"; | |
$authorization = $gen->headers($wwwauth,$username,$password,$method,$uri); | |
$headers = array(); | |
$headers[] = "Authorization: {$authorization}"; | |
$headers[] = "Accept: application/json"; | |
$headers[] = "Content-Type: application/json" ; | |
$ch = curl_init(); | |
curl_setopt($ch,CURLOPT_URL,$uri); | |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); | |
curl_setopt($ch,CURLOPT_USERAGENT,'CCME Test Digest Client'); | |
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); | |
curl_setopt($ch,CURLOPT_HEADER,true); | |
curl_setopt($ch,CURLINFO_HEADER_OUT,true); | |
$output=curl_exec($ch); | |
$info = curl_getinfo($ch); | |
print_r($info['request_header']); | |
curl_close($ch); | |
return $output; | |
} | |
$response = httpDigestGet($wwwauth); | |
print_r($response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment