Code cleanup, more header comments.
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 simple Cocoa wrapper for CommonCrypto/commonCryptor.h.
14 Provides a streaming interface for encrypting/decrypting data.
15 This class will probably be merged into or integrated with MYSymmetricKey. */
16 @interface MYCryptor : NSObject
20 CCOperation _operation;
21 CCAlgorithm _algorithm;
23 CCCryptorRef _cryptor;
25 NSOutputStream *_outputStream;
26 NSMutableData *_output;
27 size_t _outputExtraBytes;
30 /** Returns a block of cryptographically-random data, suitable for use as a symmetric key.
31 (CommonCryptor.h defines constants for key sizes and size-ranges, like kCCKeySizeAES128.) */
32 + (NSData*) randomKeyOfLength: (size_t)length;
34 /** Converts a passphrase into a block of data of the given size, suitable for use as a symmetric key. */
35 + (NSData*) keyOfLength: (size_t)lengthInBits fromPassphrase: (NSString*)passphrase;
37 /** Creates a MYCryptor configured to encrypt data. */
38 - (id) initEncryptorWithKey: (NSData*)key
39 algorithm: (CCAlgorithm)algorithm;
41 /** Creates a MYCryptor configured to decrypt data. */
42 - (id) initDecryptorWithKey: (NSData*)key
43 algorithm: (CCAlgorithm)algorithm;
45 /** The encryption/decryption key; same as the 'key' parameter to the initializer. */
46 @property (readonly) NSData *key;
48 /** The cipher to use; initial value is the 'algorithm' parameter to the initializer.
49 You can change this <i>before</i> the first call to -addData:, but not after. */
50 @property CCAlgorithm algorithm;
52 /** Block-mode cipher options; you can set flags to enable PKCS7 padding or ECB mode
54 You can change this <i>before</i> the first call to -addData:, but not after. */
55 @property CCOptions options;
57 /** Setting this property tells the cryptor to send its output to the stream,
58 instead of accumulating it in the outputData property.
59 You can change this <i>before</i> the first call to -addData:, but not after. */
60 @property (retain) NSOutputStream *outputStream;
62 /** The error state, if any, of this cryptor.
63 After -addData: or -finish: returns NO, check this property. */
64 @property (readonly, retain) NSError *error;
67 @return YES if the operation succeeded, NO if it failed. */
68 - (BOOL) addData: (NSData*)data;
70 /** Finishes up the encryption/decryption and flushes the remaining bytes of output.
71 After this is called, you cannot add any more bytes of data.
72 @return YES if the operation succeeded, NO if it failed. */
75 /** The output of the cryptor. Accessing this property implicitly calls -finish, so don't
76 do it until you've added all of the input. (And don't add any more input afterwards.)
77 This property will be nil if the outputStream property has been set. */
78 @property (readonly) NSData *outputData;
84 /** NSError domain for MYCryptor operations. Error code is interpreted as a CCCryptorStatus,
85 with additional error code(s) defined below. */
86 extern NSString* const CryptorErrorDomain;
89 /** Indicates that the outputStream couldn't write all the bytes given to it (this is legal
90 behavior for an NSOutputStream, but MYCryptor can't handle this yet.) */
91 kMYCryptorErrorOutputStreamChoked = -777000