Source

JBSCrypto / JBSCrypto / JBSCrypto.h

//  The MIT License (MIT)
//  Copyright (c) 2012 James Sumners
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to
//  deal in the Software without restriction, including without limitation the
//  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
//  sell copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
//  IN THE SOFTWARE.

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>
#import <Security/SecRandom.h>

@interface JBSCrypto : NSObject
#pragma mark - Decryption methods
/**
 Decrypts an NSData object that has been encrypted with the
 encryptData:withKey: method. The same key must be used for decrypting as was
 used for encrypting.
 
 @param theData The data object to decrypt.
 @param key The key to use for decrypting.
 
 @return The decrypted data, or nil on error.
 */
+ (NSData *)decryptData:(NSData *)theData withKey:(NSString *)key;

#pragma mark - Encryption methods
/**
 Encrypts an NSData object via the AES 128 algorithm given an AES 256 key.
 The key is expected to be an NSASCIIStringEncoding based string (i.e. one
 generated via one of the key generation methods of this class).
 
 @param theData The data to encrypt.
 @param key The AES 256 key to use for encryption.
 
 @return An NSData object that represents the encrypted data.
 */
+ (NSData *)encryptData:(NSData *)theData withKey:(NSString *)key;

#pragma mark - Key generating methods
/**
 Generates an SHA256 hash that can be used as an AES 256 key. The result is a
 string With NSASCIIStringEncoding.
 
 This method generates the key from a set of random 1024 bytes.
 
 @return A SHA256 hash.
 */
+ (NSString *)generateKey;

/**
 Generates an SHA256 hash that can be used as an AES 256 key. The result is a
 string with NSASCIIStringEncoding.
 
 @param theData An NSData object to hash.
 
 @return A SHA256 hash.
 */
+ (NSString *)generateKeyFromData:(NSData *)theData;

/**
 Generates an SHA256 hash that can be used as an AES 256 key. The result is a
 string with NSASCIIStringEncoding.
 
 @param theString A source string to hash.
 @param stringEncoding The encoding of the given string.
 
 @return A SHA256 hash.
 */
+ (NSString *)generateKeyFromString:(NSString *)theString 
                       withEncoding:(NSStringEncoding)stringEncoding;
@end