* Some cleanup. Got the test cases to pass again.
* Added some missing copyright notices.
5 // Created by Jens Alfke on 1/4/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.h>
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>
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;
26 /** Wraps an existing digest, stored in an NSData object, in a MYDigest object. */
27 + (MYDigest*) digestFromDigestData: (NSData*)digestData;
29 /** Wraps an existing digest, expressed as a hex string, in a MYDigest object. */
30 + (MYDigest*) digestFromHexString: (NSString*)hexString;
32 /** Computes a cryptographic digest of the given data. */
33 + (MYDigest*) digestOfData: (NSData*)data;
35 /** Computes a cryptographic digest of the given data. */
36 + (MYDigest*) digestOfBytes: (const void*)bytes length: (size_t)length;
38 /** Returns the digest as an NSData object. */
39 @property (readonly) NSData *asData;
41 /** Returns the digest as a hex string. */
42 @property (readonly) NSString *hexString;
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;
49 /** The algorithm that created this digest.
50 Values are defined in the CSSM_ALGORITHMS enum in cssmtype.h.
52 @property (readonly) uint32_t algorithm;
54 /** The length (in bytes, not bits!) of this digest. */
55 @property (readonly) size_t length;
57 /** A pointer to the raw bytes of digest data. */
58 @property (readonly) const void* bytes;
60 /** The algorithm used by this subclass. (Abstract method.) */
61 + (uint32_t) algorithm;
63 /** The length of digests created by this subclass. (Abstract method.) */
66 /** Primitive digest generation method. (Abstract.) */
67 + (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length;
72 // A simple C struct containing a 160-bit SHA-1 digest. Used by the MYSHA1Digest class.
77 /** A 160-bit SHA-1 digest encapsulated in an object. */
78 @interface MYSHA1Digest : MYDigest
81 /** Initialize a MYSHA1Digest object given an existing raw SHA-1 digest. */
82 - (MYSHA1Digest*) initWithRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
84 /** Create a MYSHA1Digest object given an existing raw SHA-1 digest. */
85 + (MYSHA1Digest*) digestFromRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
87 /** The SHA-1 digest as a C struct */
88 @property (readonly) const RawSHA1Digest* rawSHA1Digest;
93 // A simple C struct containing a 256-bit SHA-256 digest.
98 /** A 256-bit SHA-256 digest encapsulated in an object. Used by the MYSHA256Digest class. */
99 @interface MYSHA256Digest : MYDigest
102 /** Initialize a MYSHA256Digest object given an existing raw SHA-1 digest. */
103 - (MYSHA256Digest*) initWithRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
105 /** Create a MYSHA256Digest object given an existing raw SHA-1 digest. */
106 + (MYSHA256Digest*) digestFromRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
108 /** The SHA-256 digest as a C struct */
109 @property (readonly) const RawSHA256Digest* rawSHA256Digest;
114 /** Convenience methods for computing digests of NSData objects. */
115 @interface NSData (MYDigest)
117 /** The SHA-1 digest of the receiver's data. */
118 @property (readonly) MYSHA1Digest* my_SHA1Digest;
120 /** The SHA-256 digest of the receiver's data. */
121 @property (readonly) MYSHA256Digest* my_SHA256Digest;