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