MYCryptor.h
author snej@snej.local
Sat Apr 04 20:42:03 2009 -0700 (2009-04-04)
changeset 0 0a6527af039b
child 1 60e4cbbb5128
permissions -rw-r--r--
Initial checkin. Passes tests on Mac and in iPhone simulator.
     1 //
     2 //  Cryptor.h
     3 //  MYCrypto
     4 //
     5 //  Created by Jens Alfke on 3/21/09.
     6 //  Copyright 2009 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import <CommonCrypto/CommonCryptor.h>
    11 
    12 
    13 /** Symmetric encryption: a Cocoa wrapper for CommonCrypto/commonCryptor.h */
    14 @interface MYCryptor : NSObject
    15 {
    16     NSData *_key;
    17     CCOperation _operation;
    18     CCAlgorithm _algorithm;
    19     CCOptions _options;
    20     CCCryptorRef _cryptor;
    21     NSError *_error;
    22     NSOutputStream *_outputStream;
    23     NSMutableData *_output;
    24     size_t _outputExtraBytes;
    25 }
    26 
    27 /** CommonCryptor.h defines key size and size-range constants, like kCCKeySizeAES128 */
    28 + (NSData*) randomKeyOfLength: (size_t)length;
    29 
    30 + (NSData*) keyOfLength: (size_t)lengthInBits fromPassphrase: (NSString*)passphrase;
    31 
    32 /** Creates a MYCryptor configured to encrypt data. */
    33 - (id) initEncryptorWithKey: (NSData*)key
    34                   algorithm: (CCAlgorithm)algorithm;
    35 
    36 /** Creates a MYCryptor configured to decrypt data. */
    37 - (id) initDecryptorWithKey: (NSData*)key
    38                   algorithm: (CCAlgorithm)algorithm;
    39 
    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;
    43 
    44 /** The encryption/decryption key; same as the 'key' parameter to the initializer. */
    45 @property (readonly) NSData *key;
    46 
    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;
    50 
    51 /** Block-mode cipher options; you can set flags to enable PKCS7 padding or ECB mode
    52     (default is CBC.)
    53     You can change this before the first call to -addData:, but not after. */
    54 @property CCOptions options;
    55 
    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;
    59 
    60 /** Adds input data.
    61     @return  YES if the operation succeeded, NO if it failed. */
    62 - (BOOL) addData: (NSData*)data;
    63 
    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. */
    67 - (BOOL) finish;
    68 
    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;
    73 
    74 @end
    75 
    76 
    77 
    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;
    81 
    82 enum {
    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
    86 };