仅供自己参考,留个印象
首先是自己的MD5
-(NSString *) MD5: (NSString *)string
{
unsigned char digest[CC_MD5_DIGEST_LENGTH];
CC_MD5_CTX md5;
CC_MD5_Init(&md5);
CC_MD5_Update(&md5, [string UTF8String], [string length]);
CC_MD5_Final(digest, &md5);
NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
digest[0], digest[1],
digest[2], digest[3],
digest[4], digest[5],
digest[6], digest[7],
digest[8], digest[9],
digest[10], digest[11],
digest[12], digest[13],
digest[14], digest[15]];
return s;
}
然后是根据Digest算法组拼response
-(void)verifyContact:(NSHTTPURLResponse *)res
{
NSString * Authenticate=[[res allHeaderFields] objectForKey:@"Www-Authenticate"];
NSMutableArray *array=[NSMutableArray arrayWithArray:[Authenticate componentsSeparatedByString:@","]];
NSString *nonce;
NSString *opaque;
NSString *qop;
NSString *realm;
for (NSString *string in array) {
NSRange range1=[string rangeOfString:@"="];
NSRange range2=[string rangeOfString:@"realm"];
NSRange range3=[string rangeOfString:@"nonce"];
NSRange range4=[string rangeOfString:@"opaque"];
NSRange range5=[string rangeOfString:@"qop"];
string =[string substringFromIndex:range1.location+2];
int length=[string length];
string=[string substringToIndex:length-1];
if(range2.location!=NSNotFound)
{
realm=string;
}
else if(range3.location!=NSNotFound){
nonce=string;
}
else if (range4.location!=NSNotFound) {
opaque=string;
}
else if (range5.location!=NSNotFound) {
qop=string;
}
}
/*
Accept:text/html,application/xhtml+xml,application/xml;q=0.9;q=0.8
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Authorization:Digest username="xxx", realm="Active Phone Book", nonce="1339576936611:9c638a3ecb91b0c70d14afaa1419a555", uri="/Contacts", response="0d4fab572c5f25fb7edb20101250175f", opaque="2378F7FC02D9DB685F0B1257C542CE75", qop=auth, nc=00000001, cnonce="8cf4b89abdc36228"
Connection:keep-alive
Cookie:JSESSIONID=29F20F29603A285EF668A762012B2FDE
Host:xxxxx
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5
*/
NSString *username = [app.passwordItem objectForKey:kSecAttrDescription];
NSString *password = [app.passwordItem objectForKey:kSecAttrComment];
NSString *A1=[NSString stringWithFormat:@"%@:%@:%@",username,realm,password];
NSString *HA1=[self MD5:A1];
NSString *A2=@"POST:/Contacts";
NSString *HA2=[self MD5:A2];
NSString *nc=@"00000001";
NSString *cnonce=[self MD5:[NSString stringWithFormat:@"%ld",random()]];
NSString *responseToMD5=[NSString stringWithFormat:@"%@:%@:%@:%@:%@:%@",HA1,nonce,nc,cnonce,qop,HA2];
NSString *response_Digest=[self MD5:responseToMD5];
NSString *Authorization =[NSString stringWithFormat:@" Digest username=\"%@\", realm=\"%@\", nonce=\"%@\", uri=\"%@\", response=\"%@\", opaque=\"%@\", qop=auth, nc=%@, cnonce=\"%@\"",username,realm,nonce,@"/Contacts",response_Digest,opaque,nc,cnonce];
NSLog(@"what is my Authorization:%@",Authorization);
}