1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MYCryptor.h Sat Apr 04 20:42:03 2009 -0700
1.3 @@ -0,0 +1,86 @@
1.4 +//
1.5 +// Cryptor.h
1.6 +// MYCrypto
1.7 +//
1.8 +// Created by Jens Alfke on 3/21/09.
1.9 +// Copyright 2009 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import <Foundation/Foundation.h>
1.13 +#import <CommonCrypto/CommonCryptor.h>
1.14 +
1.15 +
1.16 +/** Symmetric encryption: a Cocoa wrapper for CommonCrypto/commonCryptor.h */
1.17 +@interface MYCryptor : NSObject
1.18 +{
1.19 + NSData *_key;
1.20 + CCOperation _operation;
1.21 + CCAlgorithm _algorithm;
1.22 + CCOptions _options;
1.23 + CCCryptorRef _cryptor;
1.24 + NSError *_error;
1.25 + NSOutputStream *_outputStream;
1.26 + NSMutableData *_output;
1.27 + size_t _outputExtraBytes;
1.28 +}
1.29 +
1.30 +/** CommonCryptor.h defines key size and size-range constants, like kCCKeySizeAES128 */
1.31 ++ (NSData*) randomKeyOfLength: (size_t)length;
1.32 +
1.33 ++ (NSData*) keyOfLength: (size_t)lengthInBits fromPassphrase: (NSString*)passphrase;
1.34 +
1.35 +/** Creates a MYCryptor configured to encrypt data. */
1.36 +- (id) initEncryptorWithKey: (NSData*)key
1.37 + algorithm: (CCAlgorithm)algorithm;
1.38 +
1.39 +/** Creates a MYCryptor configured to decrypt data. */
1.40 +- (id) initDecryptorWithKey: (NSData*)key
1.41 + algorithm: (CCAlgorithm)algorithm;
1.42 +
1.43 +/** Setting this property tells the cryptor to send its output to the stream,
1.44 + instead of accumulating itself in the outputData property. */
1.45 +@property (retain) NSOutputStream *outputStream;
1.46 +
1.47 +/** The encryption/decryption key; same as the 'key' parameter to the initializer. */
1.48 +@property (readonly) NSData *key;
1.49 +
1.50 +/** The cipher to use; initial value is the 'algorithm' parameter to the initializer.
1.51 + You can change this before the first call to -addData:, but not after. */
1.52 +@property CCAlgorithm algorithm;
1.53 +
1.54 +/** Block-mode cipher options; you can set flags to enable PKCS7 padding or ECB mode
1.55 + (default is CBC.)
1.56 + You can change this before the first call to -addData:, but not after. */
1.57 +@property CCOptions options;
1.58 +
1.59 +/** The error state, if any, of this cryptor.
1.60 + After -addData: or -finish: returns NO, check this property. */
1.61 +@property (readonly, retain) NSError *error;
1.62 +
1.63 +/** Adds input data.
1.64 + @return YES if the operation succeeded, NO if it failed. */
1.65 +- (BOOL) addData: (NSData*)data;
1.66 +
1.67 +/** Finishes up the encryption/decryption and flushes the remaining bytes of output.
1.68 + After this is called, you cannot add any more bytes of data.
1.69 + @return YES if the operation succeeded, NO if it failed. */
1.70 +- (BOOL) finish;
1.71 +
1.72 +/** The output of the cryptor. Accessing this property implicitly calls -finish, so don't
1.73 + do it until you've added all of the input. (And don't add any more input afterwards.)
1.74 + This property will be nil if the outputStream property has been set. */
1.75 +@property (readonly) NSData *outputData;
1.76 +
1.77 +@end
1.78 +
1.79 +
1.80 +
1.81 +/** NSError domain for MYCryptor operations. Error code is interpreted as a CCCryptorStatus,
1.82 + with additional error code(s) defined below. */
1.83 +extern NSString* const CryptorErrorDomain;
1.84 +
1.85 +enum {
1.86 + /** Indicates that the outputStream couldn't write all the bytes given to it (this is legal
1.87 + behavior for an NSOutputStream, but MYCryptor can't handle this yet.) */
1.88 + kMYCryptorErrorOutputStreamChoked = -777000
1.89 +};