Initial checkin. Passes tests on Mac and in iPhone simulator.
5 // Created by Jens Alfke on 3/21/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import <Foundation/Foundation.h>
10 #import <CommonCrypto/CommonCryptor.h>
13 /** Symmetric encryption: a Cocoa wrapper for CommonCrypto/commonCryptor.h */
14 @interface MYCryptor : NSObject
17 CCOperation _operation;
18 CCAlgorithm _algorithm;
20 CCCryptorRef _cryptor;
22 NSOutputStream *_outputStream;
23 NSMutableData *_output;
24 size_t _outputExtraBytes;
27 /** CommonCryptor.h defines key size and size-range constants, like kCCKeySizeAES128 */
28 + (NSData*) randomKeyOfLength: (size_t)length;
30 + (NSData*) keyOfLength: (size_t)lengthInBits fromPassphrase: (NSString*)passphrase;
32 /** Creates a MYCryptor configured to encrypt data. */
33 - (id) initEncryptorWithKey: (NSData*)key
34 algorithm: (CCAlgorithm)algorithm;
36 /** Creates a MYCryptor configured to decrypt data. */
37 - (id) initDecryptorWithKey: (NSData*)key
38 algorithm: (CCAlgorithm)algorithm;
40 /** Setting this property tells the cryptor to send its output to the stream,
41 instead of accumulating itself in the outputData property. */
42 @property (retain) NSOutputStream *outputStream;
44 /** The encryption/decryption key; same as the 'key' parameter to the initializer. */
45 @property (readonly) NSData *key;
47 /** The cipher to use; initial value is the 'algorithm' parameter to the initializer.
48 You can change this before the first call to -addData:, but not after. */
49 @property CCAlgorithm algorithm;
51 /** Block-mode cipher options; you can set flags to enable PKCS7 padding or ECB mode
53 You can change this before the first call to -addData:, but not after. */
54 @property CCOptions options;
56 /** The error state, if any, of this cryptor.
57 After -addData: or -finish: returns NO, check this property. */
58 @property (readonly, retain) NSError *error;
61 @return YES if the operation succeeded, NO if it failed. */
62 - (BOOL) addData: (NSData*)data;
64 /** Finishes up the encryption/decryption and flushes the remaining bytes of output.
65 After this is called, you cannot add any more bytes of data.
66 @return YES if the operation succeeded, NO if it failed. */
69 /** The output of the cryptor. Accessing this property implicitly calls -finish, so don't
70 do it until you've added all of the input. (And don't add any more input afterwards.)
71 This property will be nil if the outputStream property has been set. */
72 @property (readonly) NSData *outputData;
78 /** NSError domain for MYCryptor operations. Error code is interpreted as a CCCryptorStatus,
79 with additional error code(s) defined below. */
80 extern NSString* const CryptorErrorDomain;
83 /** Indicates that the outputStream couldn't write all the bytes given to it (this is legal
84 behavior for an NSOutputStream, but MYCryptor can't handle this yet.) */
85 kMYCryptorErrorOutputStreamChoked = -777000