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.)
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. */
jens@23
    27
+ (id) digestFromDigestData: (NSData*)digestData;
snej@1
    28
snej@1
    29
/** Wraps an existing digest, expressed as a hex string, in a MYDigest object. */
jens@23
    30
+ (id) digestFromHexString: (NSString*)hexString;
snej@0
    31
snej@1
    32
/** Computes a cryptographic digest of the given data. */
jens@23
    33
+ (id) digestOfData: (NSData*)data;
snej@1
    34
snej@1
    35
/** Computes a cryptographic digest of the given data. */
jens@23
    36
+ (id) 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
jens@26
    66
/** Byte-by-byte lexical comparison of digest data. */
jens@26
    67
- (NSComparisonResult) compare: (MYDigest*)other;
jens@26
    68
snej@1
    69
/** Primitive digest generation method. (Abstract.) */
snej@0
    70
+ (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length;
snej@0
    71
snej@0
    72
@end
snej@0
    73
snej@0
    74
snej@2
    75
// A simple C struct containing a 160-bit SHA-1 digest. Used by the MYSHA1Digest class.
snej@0
    76
typedef struct {
snej@0
    77
    UInt8 bytes[20];
snej@0
    78
} RawSHA1Digest;
snej@0
    79
    
snej@0
    80
/** A 160-bit SHA-1 digest encapsulated in an object. */
snej@0
    81
@interface MYSHA1Digest : MYDigest
snej@2
    82
{ }
snej@0
    83
snej@2
    84
/** Initialize a MYSHA1Digest object given an existing raw SHA-1 digest. */
snej@0
    85
- (MYSHA1Digest*) initWithRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
snej@2
    86
snej@2
    87
/** Create a MYSHA1Digest object given an existing raw SHA-1 digest. */
snej@0
    88
+ (MYSHA1Digest*) digestFromRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
snej@0
    89
snej@3
    90
/** The SHA-1 digest as a C struct */
snej@0
    91
@property (readonly) const RawSHA1Digest* rawSHA1Digest;
snej@0
    92
snej@0
    93
@end
snej@0
    94
snej@0
    95
snej@2
    96
// A simple C struct containing a 256-bit SHA-256 digest.
snej@0
    97
typedef struct {
snej@0
    98
    UInt8 bytes[32];
snej@0
    99
} RawSHA256Digest;
snej@0
   100
snej@2
   101
/** A 256-bit SHA-256 digest encapsulated in an object. Used by the MYSHA256Digest class. */
snej@0
   102
@interface MYSHA256Digest : MYDigest
snej@2
   103
{ }
snej@0
   104
snej@2
   105
/** Initialize a MYSHA256Digest object given an existing raw SHA-1 digest. */
snej@0
   106
- (MYSHA256Digest*) initWithRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
snej@2
   107
snej@2
   108
/** Create a MYSHA256Digest object given an existing raw SHA-1 digest. */
snej@0
   109
+ (MYSHA256Digest*) digestFromRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
snej@0
   110
snej@3
   111
/** The SHA-256 digest as a C struct */
snej@0
   112
@property (readonly) const RawSHA256Digest* rawSHA256Digest;
snej@0
   113
snej@0
   114
@end
snej@0
   115
snej@0
   116
snej@2
   117
/** Convenience methods for computing digests of NSData objects. */
snej@0
   118
@interface NSData (MYDigest)
snej@2
   119
snej@2
   120
/** The SHA-1 digest of the receiver's data. */
snej@0
   121
@property (readonly) MYSHA1Digest* my_SHA1Digest;
snej@2
   122
snej@2
   123
/** The SHA-256 digest of the receiver's data. */
snej@0
   124
@property (readonly) MYSHA256Digest* my_SHA256Digest;
snej@2
   125
snej@0
   126
@end