MYDigest.h
author snej@snej-mbp.mtv.corp.google.com
Wed Apr 08 16:30:52 2009 -0700 (2009-04-08)
changeset 3 1dfe820d7ebe
parent 2 8982b8fada63
child 23 39fec79de6e8
permissions -rw-r--r--
* Replaced MYKeyPair with MYPrivateKey.
* Changed config files.
     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 + (MYDigest*) digestFromDigestData: (NSData*)digestData;
    28 
    29 /** Wraps an existing digest, expressed as a hex string, in a MYDigest object. */
    30 + (MYDigest*) digestFromHexString: (NSString*)hexString;
    31 
    32 /** Computes a cryptographic digest of the given data. */
    33 + (MYDigest*) digestOfData: (NSData*)data;
    34 
    35 /** Computes a cryptographic digest of the given data. */
    36 + (MYDigest*) 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 /** Primitive digest generation method. (Abstract.) */
    67 + (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length;
    68 
    69 @end
    70 
    71 
    72 // A simple C struct containing a 160-bit SHA-1 digest. Used by the MYSHA1Digest class.
    73 typedef struct {
    74     UInt8 bytes[20];
    75 } RawSHA1Digest;
    76     
    77 /** A 160-bit SHA-1 digest encapsulated in an object. */
    78 @interface MYSHA1Digest : MYDigest
    79 { }
    80 
    81 /** Initialize a MYSHA1Digest object given an existing raw SHA-1 digest. */
    82 - (MYSHA1Digest*) initWithRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
    83 
    84 /** Create a MYSHA1Digest object given an existing raw SHA-1 digest. */
    85 + (MYSHA1Digest*) digestFromRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
    86 
    87 /** The SHA-1 digest as a C struct */
    88 @property (readonly) const RawSHA1Digest* rawSHA1Digest;
    89 
    90 @end
    91 
    92 
    93 // A simple C struct containing a 256-bit SHA-256 digest.
    94 typedef struct {
    95     UInt8 bytes[32];
    96 } RawSHA256Digest;
    97 
    98 /** A 256-bit SHA-256 digest encapsulated in an object. Used by the MYSHA256Digest class. */
    99 @interface MYSHA256Digest : MYDigest
   100 { }
   101 
   102 /** Initialize a MYSHA256Digest object given an existing raw SHA-1 digest. */
   103 - (MYSHA256Digest*) initWithRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
   104 
   105 /** Create a MYSHA256Digest object given an existing raw SHA-1 digest. */
   106 + (MYSHA256Digest*) digestFromRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
   107 
   108 /** The SHA-256 digest as a C struct */
   109 @property (readonly) const RawSHA256Digest* rawSHA256Digest;
   110 
   111 @end
   112 
   113 
   114 /** Convenience methods for computing digests of NSData objects. */
   115 @interface NSData (MYDigest)
   116 
   117 /** The SHA-1 digest of the receiver's data. */
   118 @property (readonly) MYSHA1Digest* my_SHA1Digest;
   119 
   120 /** The SHA-256 digest of the receiver's data. */
   121 @property (readonly) MYSHA256Digest* my_SHA256Digest;
   122 
   123 @end