MYDigest.h
author Jens Alfke <jens@mooseyard.com>
Tue Jul 21 10:13:08 2009 -0700 (2009-07-21)
changeset 27 d0aadddb9c64
parent 23 39fec79de6e8
permissions -rw-r--r--
MYCertificate now checks validity of self-signed certs loaded from the keychain (because the Security framework doesn't validate self-signed certs.)
     1 //
     2 //  MYDigest.h
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 1/4/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 
    12 /** Abstract superclass for cryptographic digests (aka hashes).
    13     Each specific type of digest has its own concrete subclass.
    14     Digests are full-fledged value objects, and can be compared, used as dictionary keys,
    15     copied, and archived. */
    16 @interface MYDigest : NSObject <NSCoding, NSCopying>
    17 {
    18     @private
    19     void *_rawDigest;
    20 }
    21 
    22 /** Initializes a MYDigest from an existing raw digest.
    23     MYDigest itself is abstract, so this must be called on a subclass instance. */
    24 - (id) initWithRawDigest: (const void*)rawDigest length: (size_t)length;
    25 
    26 /** Wraps an existing digest, stored in an NSData object, in a MYDigest object. */
    27 + (id) digestFromDigestData: (NSData*)digestData;
    28 
    29 /** Wraps an existing digest, expressed as a hex string, in a MYDigest object. */
    30 + (id) digestFromHexString: (NSString*)hexString;
    31 
    32 /** Computes a cryptographic digest of the given data. */
    33 + (id) digestOfData: (NSData*)data;
    34 
    35 /** Computes a cryptographic digest of the given data. */
    36 + (id) digestOfBytes: (const void*)bytes length: (size_t)length;
    37 
    38 /** Returns the digest as an NSData object. */
    39 @property (readonly) NSData *asData;
    40 
    41 /** Returns the digest as a hex string. */
    42 @property (readonly) NSString *hexString;
    43 
    44 /** Returns the first 8 digits (32 bits) of the digest's hex string, followed by "...".
    45     This is intended only for use in log messages or object descriptions, since
    46     32 bits isn't nearly enough to provide any useful uniqueness. */
    47 @property (readonly) NSString *abbreviatedHexString;
    48 
    49 /** The algorithm that created this digest. 
    50     Values are defined in the CSSM_ALGORITHMS enum in cssmtype.h.
    51     (Abstract method.) */
    52 @property (readonly) uint32_t algorithm;
    53 
    54 /** The length (in bytes, not bits!) of this digest. */
    55 @property (readonly) size_t length;
    56 
    57 /** A pointer to the raw bytes of digest data. */
    58 @property (readonly) const void* bytes;
    59 
    60 /** The algorithm used by this subclass. (Abstract method.) */
    61 + (uint32_t) algorithm;
    62 
    63 /** The length of digests created by this subclass. (Abstract method.) */
    64 + (size_t) length;
    65 
    66 /** Byte-by-byte lexical comparison of digest data. */
    67 - (NSComparisonResult) compare: (MYDigest*)other;
    68 
    69 /** Primitive digest generation method. (Abstract.) */
    70 + (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length;
    71 
    72 @end
    73 
    74 
    75 // A simple C struct containing a 160-bit SHA-1 digest. Used by the MYSHA1Digest class.
    76 typedef struct {
    77     UInt8 bytes[20];
    78 } RawSHA1Digest;
    79     
    80 /** A 160-bit SHA-1 digest encapsulated in an object. */
    81 @interface MYSHA1Digest : MYDigest
    82 { }
    83 
    84 /** Initialize a MYSHA1Digest object given an existing raw SHA-1 digest. */
    85 - (MYSHA1Digest*) initWithRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
    86 
    87 /** Create a MYSHA1Digest object given an existing raw SHA-1 digest. */
    88 + (MYSHA1Digest*) digestFromRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
    89 
    90 /** The SHA-1 digest as a C struct */
    91 @property (readonly) const RawSHA1Digest* rawSHA1Digest;
    92 
    93 @end
    94 
    95 
    96 // A simple C struct containing a 256-bit SHA-256 digest.
    97 typedef struct {
    98     UInt8 bytes[32];
    99 } RawSHA256Digest;
   100 
   101 /** A 256-bit SHA-256 digest encapsulated in an object. Used by the MYSHA256Digest class. */
   102 @interface MYSHA256Digest : MYDigest
   103 { }
   104 
   105 /** Initialize a MYSHA256Digest object given an existing raw SHA-1 digest. */
   106 - (MYSHA256Digest*) initWithRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
   107 
   108 /** Create a MYSHA256Digest object given an existing raw SHA-1 digest. */
   109 + (MYSHA256Digest*) digestFromRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
   110 
   111 /** The SHA-256 digest as a C struct */
   112 @property (readonly) const RawSHA256Digest* rawSHA256Digest;
   113 
   114 @end
   115 
   116 
   117 /** Convenience methods for computing digests of NSData objects. */
   118 @interface NSData (MYDigest)
   119 
   120 /** The SHA-1 digest of the receiver's data. */
   121 @property (readonly) MYSHA1Digest* my_SHA1Digest;
   122 
   123 /** The SHA-256 digest of the receiver's data. */
   124 @property (readonly) MYSHA256Digest* my_SHA256Digest;
   125 
   126 @end