MYDigest.m
changeset 0 0a6527af039b
child 2 8982b8fada63
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/MYDigest.m	Sat Apr 04 20:42:03 2009 -0700
     1.3 @@ -0,0 +1,278 @@
     1.4 +//
     1.5 +//  MYDigest.m
     1.6 +//  MYCrypto
     1.7 +//
     1.8 +//  Created by Jens Alfke on 1/4/08.
     1.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "MYDigest.h"
    1.13 +#import <CommonCrypto/CommonDigest.h>
    1.14 +#import "MYCrypto_Private.h"
    1.15 +
    1.16 +
    1.17 +#if USE_IPHONE_API
    1.18 +enum {
    1.19 +    CSSM_ALGID_SHA1 = 8,
    1.20 +    CSSM_ALGID_SHA256 = 0x80000000 + 14
    1.21 +};
    1.22 +#endif
    1.23 +
    1.24 +
    1.25 +@implementation MYDigest
    1.26 +
    1.27 ++ (uint32_t) algorithm {
    1.28 +    AssertAbstractMethod();
    1.29 +}
    1.30 +
    1.31 ++ (size_t) length {
    1.32 +    AssertAbstractMethod();
    1.33 +}
    1.34 +
    1.35 ++ (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length {
    1.36 +    AssertAbstractMethod();
    1.37 +}
    1.38 +
    1.39 +
    1.40 +- (id) initWithRawDigest: (const void*)rawDigest length: (size_t)length {
    1.41 +    Assert([self class] != [MYDigest class], @"MYDigest is an abstract class");
    1.42 +    Assert(rawDigest!=NULL);
    1.43 +    AssertEq(length,[[self class] length]);
    1.44 +    self = [super init];
    1.45 +    if (self) {
    1.46 +        _rawDigest = malloc(length);
    1.47 +        Assert(_rawDigest);
    1.48 +        memcpy(_rawDigest,rawDigest,length);
    1.49 +    }
    1.50 +    return self;
    1.51 +}
    1.52 +
    1.53 +- (void) dealloc
    1.54 +{
    1.55 +    if(_rawDigest) free(_rawDigest);
    1.56 +    [super dealloc];
    1.57 +}
    1.58 +
    1.59 +
    1.60 +- (id) copyWithZone: (NSZone*)zone
    1.61 +{
    1.62 +    return [self retain];
    1.63 +}
    1.64 +
    1.65 +- (id)initWithCoder:(NSCoder *)decoder
    1.66 +{
    1.67 +    NSUInteger length;
    1.68 +    const void *bytes = [decoder decodeBytesForKey: @"digest" returnedLength: &length];
    1.69 +    return [self initWithRawDigest: bytes length: length];
    1.70 +}
    1.71 +
    1.72 +
    1.73 +- (void)encodeWithCoder:(NSCoder *)coder
    1.74 +{
    1.75 +    [coder encodeBytes: self.bytes length: self.length forKey: @"digest"];
    1.76 +}
    1.77 +
    1.78 +
    1.79 ++ (MYDigest*) digestFromDigestData: (NSData*)digestData {
    1.80 +    return [[[self alloc] initWithRawDigest: digestData.bytes length: digestData.length] autorelease];
    1.81 +}
    1.82 +
    1.83 ++ (MYDigest*) digestFromHexString: (NSString*)hexString
    1.84 +{
    1.85 +    const char *cStr = [hexString UTF8String];
    1.86 +    const size_t length = [self length];
    1.87 +    if( !cStr || strlen(cStr)!=2*length )
    1.88 +        return nil;
    1.89 +    uint8_t digest[length];
    1.90 +    for( int i=0; i<length; i++ ) {
    1.91 +        if( sscanf(cStr, "%2hhx", &digest[i]) != 1 )
    1.92 +            return nil;
    1.93 +        cStr += 2;
    1.94 +    }
    1.95 +    return [[[self alloc] initWithRawDigest: &digest length: length] autorelease];
    1.96 +}
    1.97 +
    1.98 ++ (MYDigest*) digestOfData: (NSData*)data {
    1.99 +    return [self digestOfBytes: data.bytes length: data.length];
   1.100 +}
   1.101 ++ (MYDigest*) digestOfBytes: (const void*)bytes length: (size_t)length {
   1.102 +    const size_t digestLength = [self length];
   1.103 +    uint8_t digest[digestLength];
   1.104 +    [self computeDigest: digest ofBytes: bytes length: length];
   1.105 +    return [[[self alloc] initWithRawDigest: &digest length: digestLength] autorelease];
   1.106 +}
   1.107 +
   1.108 +- (uint32_t) algorithm {
   1.109 +    return [[self class] algorithm];
   1.110 +}
   1.111 +
   1.112 +- (size_t) length {
   1.113 +    return [[self class] length];
   1.114 +}
   1.115 +
   1.116 +- (const void*) bytes {
   1.117 +    return _rawDigest;
   1.118 +}
   1.119 +
   1.120 +
   1.121 +- (BOOL) isEqual: (id)digest
   1.122 +{
   1.123 +    return [digest isKindOfClass: [MYDigest class]]
   1.124 +        && [digest algorithm] == self.algorithm
   1.125 +        && memcmp(self.bytes, [digest bytes], self.length)==0;
   1.126 +}
   1.127 +
   1.128 +- (NSUInteger) hash
   1.129 +{
   1.130 +    return *(NSUInteger*)self.bytes;
   1.131 +    //? This makes the hashcode endian-dependent. Does that matter?
   1.132 +}
   1.133 +
   1.134 +- (NSComparisonResult) compare: (MYDigest*)other
   1.135 +{
   1.136 +    size_t size=self.length, otherSize=other.length;
   1.137 +    NSComparisonResult cmp = memcmp(self.bytes, other.bytes, MIN(size,otherSize));
   1.138 +    return cmp ? cmp : ((int)size - (int)otherSize);
   1.139 +}
   1.140 +
   1.141 +
   1.142 +- (NSData*) asData
   1.143 +{
   1.144 +    return [NSData dataWithBytes: self.bytes length: self.length];
   1.145 +}
   1.146 +
   1.147 +- (NSString*) description
   1.148 +{
   1.149 +    return [NSString stringWithFormat: @"%@[%@]", [self class], [self abbreviatedHexString]];
   1.150 +}
   1.151 +
   1.152 +- (NSString*) hexString
   1.153 +{
   1.154 +    const uint8_t *bytes = self.bytes;
   1.155 +    size_t length = self.length;
   1.156 +    char out[2*length+1];
   1.157 +    char *dst = &out[0];
   1.158 +    for( int i=0; i<length; i+=1 )
   1.159 +        dst += sprintf(dst,"%02X", bytes[i]);
   1.160 +    return [[[NSString alloc] initWithBytes: out length: 2*length encoding: NSASCIIStringEncoding]
   1.161 +            autorelease];
   1.162 +}
   1.163 +
   1.164 +- (NSString*) abbreviatedHexString
   1.165 +{
   1.166 +    const uint8_t *bytes = self.bytes;
   1.167 +    return [NSString stringWithFormat: @"%02hhX%02hhX%02hhX%02hhX...",
   1.168 +            bytes[0],bytes[1],bytes[2],bytes[3]];
   1.169 +}
   1.170 +
   1.171 +
   1.172 +@end
   1.173 +
   1.174 +
   1.175 +
   1.176 +@implementation MYSHA1Digest
   1.177 +
   1.178 ++ (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length {
   1.179 +    NSParameterAssert(bytes!=NULL);
   1.180 +    NSParameterAssert(length>0);
   1.181 +    CC_SHA1(bytes,length, dstDigest);
   1.182 +}
   1.183 +
   1.184 ++ (uint32_t) algorithm            {return CSSM_ALGID_SHA1;}
   1.185 ++ (size_t) length               {return sizeof(RawSHA1Digest);}
   1.186 +
   1.187 +- (MYSHA1Digest*) initWithRawSHA1Digest: (const RawSHA1Digest*)rawDigest {
   1.188 +    return [super initWithRawDigest: rawDigest length: sizeof(*rawDigest)];
   1.189 +}
   1.190 +
   1.191 ++ (MYSHA1Digest*) digestFromRawSHA1Digest: (const RawSHA1Digest*)rawDigest {
   1.192 +    return [[[self alloc] initWithRawSHA1Digest: rawDigest] autorelease];
   1.193 +}
   1.194 +
   1.195 +- (const RawSHA1Digest*) rawSHA1Digest {
   1.196 +    return self.bytes;
   1.197 +}
   1.198 +
   1.199 +
   1.200 +@end
   1.201 +
   1.202 +
   1.203 +
   1.204 +@implementation MYSHA256Digest
   1.205 +
   1.206 ++ (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length {
   1.207 +    NSParameterAssert(bytes!=NULL);
   1.208 +    NSParameterAssert(length>0);
   1.209 +    CC_SHA256(bytes,length, dstDigest);
   1.210 +}
   1.211 +
   1.212 ++ (uint32_t) algorithm            {return CSSM_ALGID_SHA256;}
   1.213 ++ (size_t) length               {return sizeof(RawSHA256Digest);}
   1.214 +
   1.215 +- (MYSHA256Digest*) initWithRawSHA256Digest: (const RawSHA256Digest*)rawDigest {
   1.216 +    return [super initWithRawDigest: rawDigest length: sizeof(*rawDigest)];
   1.217 +}
   1.218 +
   1.219 ++ (MYSHA256Digest*) digestFromRawSHA256Digest: (const RawSHA256Digest*)rawDigest {
   1.220 +    return [[[self alloc] initWithRawSHA256Digest: rawDigest] autorelease];
   1.221 +}
   1.222 +
   1.223 +- (const RawSHA256Digest*) rawSHA256Digest {
   1.224 +    return self.bytes;
   1.225 +}
   1.226 +
   1.227 +
   1.228 +@end
   1.229 +
   1.230 +
   1.231 +
   1.232 +@implementation NSData (MYDigest)
   1.233 +
   1.234 +- (MYSHA1Digest*) my_SHA1Digest
   1.235 +{
   1.236 +    return (MYSHA1Digest*) [MYSHA1Digest digestOfData: self];
   1.237 +}
   1.238 +
   1.239 +- (MYSHA256Digest*) my_SHA256Digest
   1.240 +{
   1.241 +    return (MYSHA256Digest*) [MYSHA256Digest digestOfData: self];
   1.242 +}
   1.243 +
   1.244 +@end
   1.245 +
   1.246 +
   1.247 +
   1.248 +#import "Test.h"
   1.249 +
   1.250 +
   1.251 +static void testDigestOf( NSData *src, NSString *expectedSHA1Hex, NSString *expectedSHA256Hex )
   1.252 +{
   1.253 +    MYSHA1Digest *d1 = [src my_SHA1Digest];
   1.254 +    NSString *hex = d1.hexString;
   1.255 +    Log(@"Digesting %u bytes to %@",src.length,hex);
   1.256 +    if( expectedSHA1Hex )
   1.257 +        CAssertEqual(hex,expectedSHA1Hex);
   1.258 +    MYSHA1Digest *d2 = (MYSHA1Digest*) [MYSHA1Digest digestFromHexString: hex];
   1.259 +    CAssertEqual(d1,d2);
   1.260 +    CAssertEqual(d2.hexString,hex);
   1.261 +
   1.262 +    MYSHA256Digest *d256 = [src my_SHA256Digest];
   1.263 +    hex = d256.hexString;
   1.264 +    Log(@"Digesting %u bytes to %@",src.length,hex);
   1.265 +    if( expectedSHA256Hex )
   1.266 +        CAssertEqual(hex,expectedSHA256Hex);
   1.267 +    MYSHA256Digest *d256_2 = (MYSHA256Digest*) [MYSHA256Digest digestFromHexString: hex];
   1.268 +    CAssertEqual(d256,d256_2);
   1.269 +    CAssertEqual(d256_2.hexString,hex);
   1.270 +}
   1.271 +
   1.272 +
   1.273 +TestCase(MYDigest) {
   1.274 +    testDigestOf([@"Pack my box with five dozen liquor jugs, you ugly potatoe pie!" 
   1.275 +                          dataUsingEncoding: NSUTF8StringEncoding],
   1.276 +                 @"4F254781ED6C0103BE056DD8418EFBAC0C2EBE3C",
   1.277 +                 @"08AA4BCDDF7654D7AB5CDD25395A4DD8F3BEB5C79FE567D10C1A21B9134F48FD");
   1.278 +    testDigestOf([NSData dataWithContentsOfFile: @"/Library/Desktop Pictures/Nature/Zen Garden.jpg"],
   1.279 +                 @"62A17839B3B86D3543EB2E34D2718A0FE044FA31",
   1.280 +                 @"FBD25FA6CEE794049973DE3BDF752345617FCA81018C8FC65350BCDD901142DB");
   1.281 +}