This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
MYDigest.h
128 lines (90 loc) · 4.25 KB
/
MYDigest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// MYDigest.h
// MYCrypto
//
// Created by Jens Alfke on 1/4/08.
// Copyright 2008 Jens Alfke. All rights reserved.
//
#import <Foundation/Foundation.h>
/** Abstract superclass for cryptographic digests (aka hashes).
Each specific type of digest has its own concrete subclass.
Digests are full-fledged value objects, and can be compared, used as dictionary keys,
copied, and archived. */
@interface MYDigest : NSObject <NSCoding, NSCopying>
{
@private
void *_rawDigest;
}
/** Initializes a MYDigest from an existing raw digest.
MYDigest itself is abstract, so this must be called on a subclass instance. */
- (id) initWithRawDigest: (const void*)rawDigest length: (size_t)length;
/** Wraps an existing digest, stored in an NSData object, in a MYDigest object. */
+ (id) digestFromDigestData: (NSData*)digestData;
/** Wraps an existing digest, expressed as a hex string, in a MYDigest object.
If the string is invalid (wrong length, or not hexadecimal), returns nil. */
+ (id) digestFromHexString: (NSString*)hexString;
/** Computes a cryptographic digest of the given data. */
+ (id) digestOfData: (NSData*)data;
/** Computes a cryptographic digest of the given data. */
+ (id) digestOfBytes: (const void*)bytes length: (size_t)length;
/** Returns the digest as an NSData object. */
@property (weak, readonly) NSData *asData;
/** Returns the digest as a hex string. */
@property (weak, readonly) NSString *hexString;
/** Returns the first 8 digits (32 bits) of the digest's hex string, followed by "...".
This is intended only for use in log messages or object descriptions, since
32 bits isn't nearly enough to provide any useful uniqueness. */
@property (weak, readonly) NSString *abbreviatedHexString;
/** The algorithm that created this digest. There is no universal way to name these,
so on Mac OS values are interpreted as CSSM_ALGORITHMS; on iOS, as CCHmacAlgorithm.
(Abstract method.) */
@property (readonly) uint32_t algorithm;
/** The length (in bytes, not bits!) of this digest. */
@property (readonly) size_t length;
/** A pointer to the raw bytes of digest data. */
@property (readonly) const void* bytes;
/** The algorithm used by this subclass. (Abstract method.)
For interpreting the results, see the comment on the instance method of the same name. */
+ (uint32_t) algorithm;
/** The length of digests created by this subclass. (Abstract method.) */
+ (size_t) length;
/** Byte-by-byte lexical comparison of digest data. */
- (NSComparisonResult) compare: (MYDigest*)other;
/** Primitive digest generation method. (Abstract.) */
+ (void) computeDigest: (void*)dstDigest ofBytes: (const void*)bytes length: (size_t)length;
@end
// A simple C struct containing a 160-bit SHA-1 digest. Used by the MYSHA1Digest class.
typedef struct {
UInt8 bytes[20];
} RawSHA1Digest;
/** A 160-bit SHA-1 digest encapsulated in an object. */
@interface MYSHA1Digest : MYDigest
{ }
/** Initialize a MYSHA1Digest object given an existing raw SHA-1 digest. */
- (MYSHA1Digest*) initWithRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
/** Create a MYSHA1Digest object given an existing raw SHA-1 digest. */
+ (MYSHA1Digest*) digestFromRawSHA1Digest: (const RawSHA1Digest*)rawDigest;
/** The SHA-1 digest as a C struct */
@property (readonly) const RawSHA1Digest* rawSHA1Digest;
@end
// A simple C struct containing a 256-bit SHA-256 digest.
typedef struct {
UInt8 bytes[32];
} RawSHA256Digest;
/** A 256-bit SHA-256 digest encapsulated in an object. Used by the MYSHA256Digest class. */
@interface MYSHA256Digest : MYDigest
{ }
/** Initialize a MYSHA256Digest object given an existing raw SHA-1 digest. */
- (MYSHA256Digest*) initWithRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
/** Create a MYSHA256Digest object given an existing raw SHA-1 digest. */
+ (MYSHA256Digest*) digestFromRawSHA256Digest: (const RawSHA256Digest*)rawDigest;
/** The SHA-256 digest as a C struct */
@property (readonly) const RawSHA256Digest* rawSHA256Digest;
@end
/** Convenience methods for computing digests of NSData objects. */
@interface NSData (MYDigest)
/** The SHA-1 digest of the receiver's data. */
@property (readonly) MYSHA1Digest* my_SHA1Digest;
/** The SHA-256 digest of the receiver's data. */
@property (readonly) MYSHA256Digest* my_SHA256Digest;
@end