Skip to content

Instantly share code, notes, and snippets.

@papertigers
Forked from bahamas10/main.m
Created March 11, 2014 17:59
Show Gist options
  • Save papertigers/9491366 to your computer and use it in GitHub Desktop.
Save papertigers/9491366 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSData (MD5)
- (NSString *)MD5String;
@end
#import <CommonCrypto/CommonDigest.h>
@implementation NSData (MD5)
- (NSString *)MD5String {
unsigned char result[16];
CC_MD5(self.bytes, self.length, result);
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
// get the public key
NSString *pubkey = [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:[@"~/.ssh/id_rsa.pub" stringByExpandingTildeInPath]] encoding:NSASCIIStringEncoding];
NSLog(@"pubkey = %@", pubkey);
// extract the middle portion
NSString *dataEncoded = [pubkey componentsSeparatedByString:@" "][1];
// base64 decode it
NSData *dataDecoded = [[NSData alloc] initWithBase64EncodedString:dataEncoded options:0];
// md5 it
NSString *md5 = [dataDecoded MD5String];
// add colons
NSMutableString *_fp = [NSMutableString new];
for (int i = 0; i < md5.length; i++) {
[_fp appendString:[md5 substringWithRange:NSMakeRange(i, 1)]];
if (i % 2 && i < md5.length - 1)
[_fp appendString:@":"];
}
// make everything lowercase
NSString *fp = [_fp lowercaseString];
NSLog(@"fingerprint = %@", fp);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment