Skip to content

Instantly share code, notes, and snippets.

@nerdsRob
Created April 3, 2013 14:10
Show Gist options
  • Select an option

  • Save nerdsRob/5301542 to your computer and use it in GitHub Desktop.

Select an option

Save nerdsRob/5301542 to your computer and use it in GitHub Desktop.
//
// See full source code :
// https://github.com/FLCLjp/VerifyFingerPrint
//
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {
SecTrustRef trustRef = [[challenge protectionSpace] serverTrust];
CFIndex count = SecTrustGetCertificateCount(trustRef);
for (CFIndex i = 0; i < count; i++) {
SecCertificateRef certRef = SecTrustGetCertificateAtIndex(trustRef, i);
CFDataRef certData = SecCertificateCopyData(certRef);
NSData *sha1 = ((__bridge NSData *)certData).sha1;
CFRelease(certData);
// compare by sha1 strings.
NSString *TWITTER_COM_CER_SHA1 = @"<593f874d e35c49a1 a4f78a14 3c79e58f 032cf992>";
if ([[sha1 description] isEqualToString:TWITTER_COM_CER_SHA1]) {
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:trustRef] forAuthenticationChallenge:challenge];
return;
}
}
}
[[challenge sender] cancelAuthenticationChallenge: challenge];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment