snej@0: //
snej@0: //  MYDigest.h
snej@0: //  MYCrypto
snej@0: //
snej@0: //  Created by Jens Alfke on 1/4/08.
snej@0: //  Copyright 2008 Jens Alfke. All rights reserved.
snej@0: //
snej@0: 
snej@0: #import <Foundation/Foundation.h>
snej@0: 
snej@0: 
snej@0: /** Abstract superclass for cryptographic digests (aka hashes).
snej@2:     Each specific type of digest has its own concrete subclass.
snej@2:     Digests are full-fledged value objects, and can be compared, used as dictionary keys,
snej@2:     copied, and archived. */
snej@0: @interface MYDigest : NSObject <NSCoding, NSCopying>
snej@0: {
snej@1:     @private
snej@0:     void *_rawDigest;
snej@0: }
snej@0: 
snej@1: /** Initializes a MYDigest from an existing raw digest.
snej@1:     MYDigest itself is abstract, so this must be called on a subclass instance. */
snej@0: - (id) initWithRawDigest: (const void*)rawDigest length: (size_t)length;
snej@0: 
snej@1: /** Wraps an existing digest, stored in an NSData object, in a MYDigest object. */
snej@0: + (MYDigest*) digestFromDigestData: (NSData*)digestData;
snej@1: 
snej@1: /** Wraps an existing digest, expressed as a hex string, in a MYDigest object. */
snej@0: + (MYDigest*) digestFromHexString: (NSString*)hexString;
snej@0: 
snej@1: /** Computes a cryptographic digest of the given data. */
snej@0: + (MYDigest*) digestOfData: (NSData*)data;
snej@1: 
snej@1: /** Computes a cryptographic digest of the given data. */
snej@0: + (MYDigest*) digestOfBytes: (const void*)bytes length: (size_t)length;
snej@0: 
snej@1: /** Returns the digest as an NSData object. */
snej@1: @property (readonly) NSData *asData;
snej@0: 
snej@1: /** Returns the digest as a hex string. */
snej@1: @property (readonly) NSString *hexString;
snej@0: 
snej@2: /** Returns the first 8 digits (32 bits) of the digest's hex string, followed by "...".
snej@1:     This is intended only for use in log messages or object descriptions, since
snej@1:     32 bits isn't nearly enough to provide any useful uniqueness. */
snej@1: @property (readonly) NSString *abbreviatedHexString;
snej@1: 
snej@2: /** The algorithm that created this digest. 
snej@2:     Values are defined in the CSSM_ALGORITHMS enum in cssmtype.h.
snej@2:     (Abstract method.) */
snej@2: @property (readonly) uint32_t algorithm;
snej@1: 
snej@1: /** The length (in bytes, not bits!) of this digest. */
snej@0: @property (readonly) size_t length;
snej@1: 
snej@1: /** A pointer to the raw bytes of digest data. */
snej@0: @property (readonly) const void* bytes;
snej@0: 
snej@1: /** The algorithm used by this subclass. (Abstract method.) */
snej@2: + (uint32_t) algorithm;
snej@1: 
snej@1: /** The length of digests created by this subclass. (Abstract method.) */
snej@0: + (size_t) length;
snej@0: 
snej@1: /** Primitive digest generation method. (Abstract.) */
snej@0: + (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length;
snej@0: 
snej@0: @end
snej@0: 
snej@0: 
snej@2: // A simple C struct containing a 160-bit SHA-1 digest. Used by the MYSHA1Digest class.
snej@0: typedef struct {
snej@0:     UInt8 bytes[20];
snej@0: } RawSHA1Digest;
snej@0:     
snej@0: /** A 160-bit SHA-1 digest encapsulated in an object. */
snej@0: @interface MYSHA1Digest : MYDigest
snej@2: { }
snej@0: 
snej@2: /** Initialize a MYSHA1Digest object given an existing raw SHA-1 digest. */
snej@0: - (MYSHA1Digest*) initWithRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
snej@2: 
snej@2: /** Create a MYSHA1Digest object given an existing raw SHA-1 digest. */
snej@0: + (MYSHA1Digest*) digestFromRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
snej@0: 
snej@3: /** The SHA-1 digest as a C struct */
snej@0: @property (readonly) const RawSHA1Digest* rawSHA1Digest;
snej@0: 
snej@0: @end
snej@0: 
snej@0: 
snej@2: // A simple C struct containing a 256-bit SHA-256 digest.
snej@0: typedef struct {
snej@0:     UInt8 bytes[32];
snej@0: } RawSHA256Digest;
snej@0: 
snej@2: /** A 256-bit SHA-256 digest encapsulated in an object. Used by the MYSHA256Digest class. */
snej@0: @interface MYSHA256Digest : MYDigest
snej@2: { }
snej@0: 
snej@2: /** Initialize a MYSHA256Digest object given an existing raw SHA-1 digest. */
snej@0: - (MYSHA256Digest*) initWithRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
snej@2: 
snej@2: /** Create a MYSHA256Digest object given an existing raw SHA-1 digest. */
snej@0: + (MYSHA256Digest*) digestFromRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
snej@0: 
snej@3: /** The SHA-256 digest as a C struct */
snej@0: @property (readonly) const RawSHA256Digest* rawSHA256Digest;
snej@0: 
snej@0: @end
snej@0: 
snej@0: 
snej@2: /** Convenience methods for computing digests of NSData objects. */
snej@0: @interface NSData (MYDigest)
snej@2: 
snej@2: /** The SHA-1 digest of the receiver's data. */
snej@0: @property (readonly) MYSHA1Digest* my_SHA1Digest;
snej@2: 
snej@2: /** The SHA-256 digest of the receiver's data. */
snej@0: @property (readonly) MYSHA256Digest* my_SHA256Digest;
snej@2: 
snej@0: @end