NSData+CommonCrypto.h 5.21 KB
/*
 *  NSData+CommonCrypto.h
 *  AQToolkit
 *
 *  Created by Jim Dovey on 31/8/2008.
 *
 *  Copyright (c) 2008-2009, Jim Dovey
 *  All rights reserved.
 *  
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  
 *  Neither the name of this project's author nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#import <Foundation/NSData.h>
#import <Foundation/NSError.h>
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonHMAC.h>

extern NSString * const kBBACommonCryptoErrorDomain;

@interface NSError (BBACommonCryptoErrorDomain)
+ (NSError *) errorWithCCCryptorStatusEx: (CCCryptorStatus) status;
@end

@interface NSData (BBACommonDigest)

- (NSData *) MD2SumEx;
- (NSData *) MD4SumEx;
- (NSData *) MD5SumEx;

- (NSData *) SHA1HashEx;
- (NSData *) SHA224HashEx;
- (NSData *) SHA256HashEx;
- (NSData *) SHA384HashEx;
- (NSData *) SHA512HashEx;

@end

@interface NSData (BBACommonCryptor)

- (NSData *) AES256EncryptedDataUsingKeyEx: (NSString *) key IV:(NSString *)iv;
- (NSData *) decryptedAES256DataUsingKeyEx: (NSString *) key IV:(NSString *) iv;

- (NSData *) DESEncryptedDataUsingKeyEx: (id) key error: (NSError **) error;
- (NSData *) decryptedDESDataUsingKeyEx: (id) key error: (NSError **) error;

- (NSData *) CASTEncryptedDataUsingKeyEx: (id) key error: (NSError **) error;
- (NSData *) decryptedCASTDataUsingKeyEx: (id) key error: (NSError **) error;

@end

@interface NSData (BBALowLevelCommonCryptor)

- (NSData *) dataEncryptedUsingAlgorithmEx: (CCAlgorithm) algorithm
                                     key: (id) key		// data or string
                                   error: (CCCryptorStatus *) error;
- (NSData *) dataEncryptedUsingAlgorithmEx: (CCAlgorithm) algorithm
                                     key: (id) key		// data or string
                                 options: (CCOptions) options
                                   error: (CCCryptorStatus *) error;
- (NSData *) dataEncryptedUsingAlgorithmEx: (CCAlgorithm) algorithm
                                     key: (id) key		// data or string
                    initializationVector: (id) iv		// data or string
                                 options: (CCOptions) options
                                   error: (CCCryptorStatus *) error;

- (NSData *) decryptedDataUsingAlgorithmEx: (CCAlgorithm) algorithm
                                     key: (id) key		// data or string
                                   error: (CCCryptorStatus *) error;
- (NSData *) decryptedDataUsingAlgorithmEx: (CCAlgorithm) algorithm
                                     key: (id) key		// data or string
                                 options: (CCOptions) options
                                   error: (CCCryptorStatus *) error;
- (NSData *) decryptedDataUsingAlgorithmEx: (CCAlgorithm) algorithm
                                     key: (id) key		// data or string
                    initializationVector: (id) iv		// data or string
                                 options: (CCOptions) options
                                   error: (CCCryptorStatus *) error;

/**
 * @brief AES加密或者解密
 * @param mode 模式 kCCMode ECB, CBC, CFB, CTR, OFB, RC4, CFB8
 * @param operation 操作
 * @param algorithm AES DES, 3DES, CAST, RC4, RC2, Blowfish
 * @param padding cc NoPadding, PKCS7Padding
 * @param key key
 * @param keyLength kCCKeySizeAES 128, 192, 256
 * @param iv iv
 * @param error 可能发生的错误
 */
- (NSData *)cryptWithMode:(CCMode)mode
                operation:(CCOperation)operation
                algorithm:(CCAlgorithm)algorithm
                  padding:(CCPadding)padding
                      key:(NSData *)key
                keyLength:(size_t)keyLength
                       iv:(NSData *)iv
                    error:(NSError **)error;

@end

@interface NSData (BBACommonHMAC)

- (NSData *) HMACWithAlgorithmEx: (CCHmacAlgorithm) algorithm;
- (NSData *) HMACWithAlgorithmEx: (CCHmacAlgorithm) algorithm key: (id) key;

@end