From 19ce9a3c7688c3ba024a0865816f6c93926a2096 Mon Sep 17 00:00:00 2001 From: Kelp Date: Fri, 25 Jan 2013 14:20:40 +0800 Subject: [PATCH] clear up code. only for ARC project. --- CocoaSecurity/CocoaSecurity.h | 122 +++++---- CocoaSecurity/CocoaSecurity.m | 320 ++++++++++++++---------- CocoaSecurityTests/CocoaSecurityTests.m | 124 +++++---- README.md | 68 +++-- 4 files changed, 353 insertions(+), 281 deletions(-) diff --git a/CocoaSecurity/CocoaSecurity.h b/CocoaSecurity/CocoaSecurity.h index 3df0847..0a47b80 100644 --- a/CocoaSecurity/CocoaSecurity.h +++ b/CocoaSecurity/CocoaSecurity.h @@ -1,15 +1,11 @@ /* - CocoaSecurity 1.0.1 + CocoaSecurity 1.1 Created by Kelp on 12/5/12. Copyright (c) 2012 Kelp http://kelp.phate.org/ MIT License CocoaSecurity is core. It provides AES encrypt, AES decrypt, Hash(MD5, HmacMD5, SHA1~SHA512, HmacSHA1~HmacSHA512) messages. - - 1.0.1 2012-05-15 - update ARC version - */ #import @@ -18,83 +14,107 @@ #import #import -#pragma mark - CocoaSecurityResult +#pragma mark - CocoaSecurityResult @interface CocoaSecurityResult : NSObject -#if __has_feature(objc_arc) -@property (strong) NSData *data; -@property (strong, readonly) NSString *utf8String; -@property (strong, readonly) NSString *hex; -@property (strong, readonly) NSString *hexLower; -@property (strong, readonly) NSString *base64; -#else -@property (retain) NSData *data; -@property (retain, readonly) NSString *utf8String; -@property (retain, readonly) NSString *hex; -@property (retain, readonly) NSString *hexLower; -@property (retain, readonly) NSString *base64; -#endif +@property (strong, nonatomic, readonly) NSData *data; +@property (strong, nonatomic, readonly) NSString *utf8String; +@property (strong, nonatomic, readonly) NSString *hex; +@property (strong, nonatomic, readonly) NSString *hexLower; +@property (strong, nonatomic, readonly) NSString *base64; + +- (id)initWithBytes:(unsigned char[])initData length:(NSUInteger)length; -- (id)initWithBytes: (unsigned char[])initData length: (uint) length; @end #pragma mark - CocoaSecurity @interface CocoaSecurity : NSObject -#pragma mark - Init -+ (id)sharedInstance; - #pragma mark - AES Encrypt ++ (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSString *)key; ++ (CocoaSecurityResult *)aesEncrypt:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv; ++ (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSData *)key iv:(NSData *)iv; ++ (CocoaSecurityResult *)aesEncryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv; - (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSString *)key; - (CocoaSecurityResult *)aesEncrypt:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv; - (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSData *)key iv:(NSData *)iv; - (CocoaSecurityResult *)aesEncryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv; #pragma mark AES Decrypt ++ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSString *)key; ++ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv; ++ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSData *)key iv:(NSData *)iv; ++ (CocoaSecurityResult *)aesDecryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv; - (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSString *)key; - (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv; - (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSData *)key iv:(NSData *)iv; - (CocoaSecurityResult *)aesDecryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv; #pragma mark - MD5 -- (CocoaSecurityResult *)md5: (NSString *)hashString; -- (CocoaSecurityResult *)md5WithData: (NSData *)hashData; ++ (CocoaSecurityResult *)md5:(NSString *)hashString; ++ (CocoaSecurityResult *)md5WithData:(NSData *)hashData; +- (CocoaSecurityResult *)md5:(NSString *)hashString; +- (CocoaSecurityResult *)md5WithData:(NSData *)hashData; #pragma mark HMAC-MD5 -- (CocoaSecurityResult *)hmacMd5: (NSString *)hashString hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacMd5WithData: (NSData *)hashData hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacMd5:(NSString *)hashString hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacMd5WithData:(NSData *)hashData hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacMd5:(NSString *)hashString hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacMd5WithData:(NSData *)hashData hmacKey:(NSString *)key; #pragma mark - SHA -- (CocoaSecurityResult *)sha1: (NSString *)hashString; -- (CocoaSecurityResult *)sha1WithData: (NSData *)hashData; -- (CocoaSecurityResult *)sha224: (NSString *)hashString; -- (CocoaSecurityResult *)sha224WithData: (NSData *)hashData; -- (CocoaSecurityResult *)sha256: (NSString *)hashString; -- (CocoaSecurityResult *)sha256WithData: (NSData *)hashData; -- (CocoaSecurityResult *)sha384: (NSString *)hashString; -- (CocoaSecurityResult *)sha384WithData: (NSData *)hashData; -- (CocoaSecurityResult *)sha512: (NSString *)hashString; -- (CocoaSecurityResult *)sha512WithData: (NSData *)hashData; ++ (CocoaSecurityResult *)sha1:(NSString *)hashString; ++ (CocoaSecurityResult *)sha1WithData:(NSData *)hashData; ++ (CocoaSecurityResult *)sha224:(NSString *)hashString; ++ (CocoaSecurityResult *)sha224WithData:(NSData *)hashData; ++ (CocoaSecurityResult *)sha256:(NSString *)hashString; ++ (CocoaSecurityResult *)sha256WithData:(NSData *)hashData; ++ (CocoaSecurityResult *)sha384:(NSString *)hashString; ++ (CocoaSecurityResult *)sha384WithData:(NSData *)hashData; ++ (CocoaSecurityResult *)sha512:(NSString *)hashString; ++ (CocoaSecurityResult *)sha512WithData:(NSData *)hashData; +- (CocoaSecurityResult *)sha1:(NSString *)hashString; +- (CocoaSecurityResult *)sha1WithData:(NSData *)hashData; +- (CocoaSecurityResult *)sha224:(NSString *)hashString; +- (CocoaSecurityResult *)sha224WithData:(NSData *)hashData; +- (CocoaSecurityResult *)sha256:(NSString *)hashString; +- (CocoaSecurityResult *)sha256WithData:(NSData *)hashData; +- (CocoaSecurityResult *)sha384:(NSString *)hashString; +- (CocoaSecurityResult *)sha384WithData:(NSData *)hashData; +- (CocoaSecurityResult *)sha512:(NSString *)hashString; +- (CocoaSecurityResult *)sha512WithData:(NSData *)hashData; #pragma mark HMAC-SHA -- (CocoaSecurityResult *)hmacSha1: (NSString *)hashString hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha1WithData: (NSData *)hashData hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha224: (NSString *)hashString hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha224WithData: (NSData *)hashData hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha256: (NSString *)hashString hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha256WithData: (NSData *)hashData hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha384: (NSString *)hashString hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha384WithData: (NSData *)hashData hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha512: (NSString *)hashString hmacKey:(NSString *)key; -- (CocoaSecurityResult *)hmacSha512WithData: (NSData *)hashData hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha1:(NSString *)hashString hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha1WithData:(NSData *)hashData hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha224:(NSString *)hashString hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha224WithData:(NSData *)hashData hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha256:(NSString *)hashString hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha256WithData:(NSData *)hashData hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha384:(NSString *)hashString hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha384WithData:(NSData *)hashData hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha512:(NSString *)hashString hmacKey:(NSString *)key; ++ (CocoaSecurityResult *)hmacSha512WithData:(NSData *)hashData hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha1:(NSString *)hashString hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha1WithData:(NSData *)hashData hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha224:(NSString *)hashString hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha224WithData:(NSData *)hashData hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha256:(NSString *)hashString hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha256WithData:(NSData *)hashData hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha384:(NSString *)hashString hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha384WithData:(NSData *)hashData hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha512:(NSString *)hashString hmacKey:(NSString *)key; +- (CocoaSecurityResult *)hmacSha512WithData:(NSData *)hashData hmacKey:(NSString *)key; @end #pragma mark - CocoaSecurityEncoder @interface CocoaSecurityEncoder : NSObject -- (NSString *)base64: (NSData *)data; -- (NSString *)hex: (NSData *)data useLower: (bool)isOutputLower; +- (NSString *)base64:(NSData *)data; +- (NSString *)hex:(NSData *)data useLower:(bool)isOutputLower; @end + + #pragma mark - CocoaSecurityDecoder @interface CocoaSecurityDecoder : NSObject -- (NSData *)base64: (NSString *)data; -- (NSData *)hex: (NSString *)data; +- (NSData *)base64:(NSString *)data; +- (NSData *)hex:(NSString *)data; @end diff --git a/CocoaSecurity/CocoaSecurity.m b/CocoaSecurity/CocoaSecurity.m index 5fd6f46..286663b 100644 --- a/CocoaSecurity/CocoaSecurity.m +++ b/CocoaSecurity/CocoaSecurity.m @@ -8,22 +8,18 @@ #import "CocoaSecurity.h" + #pragma mark - CocoaSecurity @implementation CocoaSecurity -static CocoaSecurity *_instance = nil; - -#pragma mark - Init -+ (id)sharedInstance -{ - if (_instance == nil) { - _instance = [self new]; - } - return _instance; -} #pragma mark - AES Encrypt // default AES Encrypt, key -> SHA384(key).sub(0, 32), iv -> SHA384(key).sub(32, 16) ++ (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesEncrypt:data key:key]; +} - (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSString *)key { CocoaSecurityResult * sha = [self sha384:key]; @@ -33,14 +29,26 @@ static CocoaSecurity *_instance = nil; return [self aesEncrypt:data key:aesKey iv:aesIv]; } #pragma mark AES Encrypt 128, 192, 256 ++ (CocoaSecurityResult *)aesEncrypt:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesEncrypt:data hexKey:key hexIv:iv]; +} ++ (CocoaSecurityResult *)aesEncrypt:(NSString *)data key:(NSData *)key iv:(NSData *)iv +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesEncrypt:data key:key iv:iv]; +} ++ (CocoaSecurityResult *)aesEncryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesEncryptWithData:data key:key iv:iv]; +} - (CocoaSecurityResult *)aesEncrypt:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv { - CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init]; + CocoaSecurityDecoder *decoder = [CocoaSecurityDecoder new]; NSData *aesKey = [decoder hex:key]; NSData *aesIv = [decoder hex:iv]; -#if !__has_feature(objc_arc) - [decoder release]; -#endif return [self aesEncrypt:data key:aesKey iv:aesIv]; } @@ -80,11 +88,7 @@ static CocoaSecurity *_instance = nil; bufferSize, &encryptedSize); if (cryptStatus == kCCSuccess) { -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:buffer length:encryptedSize]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:buffer length:encryptedSize] autorelease]; -#endif free(buffer); return result; @@ -99,6 +103,11 @@ static CocoaSecurity *_instance = nil; } #pragma mark - AES Decrypt // default AES Decrypt, key -> SHA384(key).sub(0, 32), iv -> SHA384(key).sub(32, 16) ++ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesDecryptWithBase64:data key:key]; +} - (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSString *)key { CocoaSecurityResult * sha = [self sha384:key]; @@ -108,25 +117,32 @@ static CocoaSecurity *_instance = nil; return [self aesDecryptWithBase64:data key:aesKey iv:aesIv]; } #pragma mark AES Decrypt 128, 192, 256 ++ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesDecryptWithBase64:data hexKey:key hexIv:iv]; +} ++ (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSData *)key iv:(NSData *)iv +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesDecryptWithBase64:data key:key iv:iv]; +} ++ (CocoaSecurityResult *)aesDecryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs aesDecryptWithData:data key:key iv:iv]; +} - (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data hexKey:(NSString *)key hexIv:(NSString *)iv { - CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init]; + CocoaSecurityDecoder *decoder = [CocoaSecurityDecoder new]; NSData *aesKey = [decoder hex:key]; NSData *aesIv = [decoder hex:iv]; -#if !__has_feature(objc_arc) - [decoder release]; -#endif return [self aesDecryptWithBase64:data key:aesKey iv:aesIv]; } - (CocoaSecurityResult *)aesDecryptWithBase64:(NSString *)data key:(NSData *)key iv:(NSData *)iv { -#if __has_feature(objc_arc) - CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init]; -#else - CocoaSecurityDecoder *decoder = [[[CocoaSecurityDecoder alloc] init] autorelease]; -#endif - + CocoaSecurityDecoder *decoder = [CocoaSecurityDecoder new]; return [self aesDecryptWithData:[decoder base64:data] key:key iv:iv]; } - (CocoaSecurityResult *)aesDecryptWithData:(NSData *)data key:(NSData *)key iv:(NSData *)iv @@ -161,11 +177,7 @@ static CocoaSecurity *_instance = nil; bufferSize, &encryptedSize); if (cryptStatus == kCCSuccess) { -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:buffer length:encryptedSize]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:buffer length:encryptedSize] autorelease]; -#endif free(buffer); return result; @@ -179,7 +191,18 @@ static CocoaSecurity *_instance = nil; } } + #pragma mark - MD5 ++ (CocoaSecurityResult *)md5:(NSString *)hashString +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs md5:hashString]; +} ++ (CocoaSecurityResult *)md5WithData:(NSData *)hashData +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs md5WithData:hashData]; +} - (CocoaSecurityResult *)md5:(NSString *)hashString { return [self md5WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]]; @@ -190,16 +213,22 @@ static CocoaSecurity *_instance = nil; digest = malloc(CC_MD5_DIGEST_LENGTH); CC_MD5([hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH] autorelease]; -#endif free(digest); return result; } #pragma mark - HMAC-MD5 ++ (CocoaSecurityResult *)hmacMd5:(NSString *)hashString hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacMd5:hashString hmacKey:key]; +} ++ (CocoaSecurityResult *)hmacMd5WithData:(NSData *)hashData hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacMd5WithData:hashData hmacKey:key]; +} - (CocoaSecurityResult *)hmacMd5:(NSString *)hashString hmacKey:(NSString *)key { return [self hmacMd5WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key]; @@ -211,18 +240,25 @@ static CocoaSecurity *_instance = nil; const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding]; CCHmac(kCCHmacAlgMD5, cKey, strlen(cKey), [hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_MD5_DIGEST_LENGTH] autorelease]; -#endif free(digest); cKey = nil; return result; } + #pragma mark - SHA1 ++ (CocoaSecurityResult *)sha1:(NSString *)hashString +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha1:hashString]; +} ++ (CocoaSecurityResult *)sha1WithData:(NSData *)hashData +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha1WithData:hashData]; +} - (CocoaSecurityResult *)sha1:(NSString *)hashString { return [self sha1WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]]; @@ -233,16 +269,22 @@ static CocoaSecurity *_instance = nil; digest = malloc(CC_SHA1_DIGEST_LENGTH); CC_SHA1([hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH] autorelease]; -#endif free(digest); return result; } #pragma mark SHA224 ++ (CocoaSecurityResult *)sha224:(NSString *)hashString +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha224:hashString]; +} ++ (CocoaSecurityResult *)sha224WithData:(NSData *)hashData +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha224WithData:hashData]; +} - (CocoaSecurityResult *)sha224:(NSString *)hashString { return [self sha224WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]]; @@ -253,16 +295,22 @@ static CocoaSecurity *_instance = nil; digest = malloc(CC_SHA224_DIGEST_LENGTH); CC_SHA224([hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA224_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA224_DIGEST_LENGTH] autorelease]; -#endif free(digest); return result; } #pragma mark SHA256 ++ (CocoaSecurityResult *)sha256:(NSString *)hashString +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha256:hashString]; +} ++ (CocoaSecurityResult *)sha256WithData:(NSData *)hashData +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha256WithData:hashData]; +} - (CocoaSecurityResult *)sha256:(NSString *)hashString { return [self sha256WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]]; @@ -273,16 +321,22 @@ static CocoaSecurity *_instance = nil; digest = malloc(CC_SHA256_DIGEST_LENGTH); CC_SHA256([hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH] autorelease]; -#endif free(digest); return result; } #pragma mark SHA384 ++ (CocoaSecurityResult *)sha384:(NSString *)hashString +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha384:hashString]; +} ++ (CocoaSecurityResult *)sha384WithData:(NSData *)hashData +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha384WithData:hashData]; +} - (CocoaSecurityResult *)sha384:(NSString *)hashString { return [self sha384WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]]; @@ -293,16 +347,22 @@ static CocoaSecurity *_instance = nil; digest = malloc(CC_SHA384_DIGEST_LENGTH); CC_SHA384([hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA384_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA384_DIGEST_LENGTH] autorelease]; -#endif free(digest); return result; } #pragma mark SHA512 ++ (CocoaSecurityResult *)sha512:(NSString *)hashString +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha512:hashString]; +} ++ (CocoaSecurityResult *)sha512WithData:(NSData *)hashData +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs sha512WithData:hashData]; +} - (CocoaSecurityResult *)sha512:(NSString *)hashString { return [self sha512WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]]; @@ -313,17 +373,24 @@ static CocoaSecurity *_instance = nil; digest = malloc(CC_SHA512_DIGEST_LENGTH); CC_SHA512([hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA512_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA512_DIGEST_LENGTH] autorelease]; -#endif free(digest); return result; } + #pragma mark - HMAC-SHA1 ++ (CocoaSecurityResult *)hmacSha1:(NSString *)hashString hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha1:hashString hmacKey:key]; +} ++ (CocoaSecurityResult *)hmacSha1WithData:(NSData *)hashData hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha1WithData:hashData hmacKey:key]; +} - (CocoaSecurityResult *)hmacSha1:(NSString *)hashString hmacKey:(NSString *)key { return [self hmacSha1WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key]; @@ -335,17 +402,23 @@ static CocoaSecurity *_instance = nil; const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding]; CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), [hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH] autorelease]; -#endif free(digest); cKey = nil; return result; } #pragma mark HMAC-SHA224 ++ (CocoaSecurityResult *)hmacSha224:(NSString *)hashString hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha224:hashString hmacKey:key]; +} ++ (CocoaSecurityResult *)hmacSha224WithData:(NSData *)hashData hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha224WithData:hashData hmacKey:key]; +} - (CocoaSecurityResult *)hmacSha224:(NSString *)hashString hmacKey:(NSString *)key { return [self hmacSha224WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key]; @@ -357,17 +430,23 @@ static CocoaSecurity *_instance = nil; const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding]; CCHmac(kCCHmacAlgSHA224, cKey, strlen(cKey), [hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA224_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA224_DIGEST_LENGTH] autorelease]; -#endif free(digest); cKey = nil; return result; } #pragma mark HMAC-SHA256 ++ (CocoaSecurityResult *)hmacSha256:(NSString *)hashString hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha256:hashString hmacKey:key]; +} ++ (CocoaSecurityResult *)hmacSha256WithData:(NSData *)hashData hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha256WithData:hashData hmacKey:key]; +} - (CocoaSecurityResult *)hmacSha256:(NSString *)hashString hmacKey:(NSString *)key { return [self hmacSha256WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key]; @@ -379,17 +458,23 @@ static CocoaSecurity *_instance = nil; const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding]; CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), [hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA256_DIGEST_LENGTH] autorelease]; -#endif free(digest); cKey = nil; return result; } #pragma mark HMAC-SHA384 ++ (CocoaSecurityResult *)hmacSha384:(NSString *)hashString hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha384:hashString hmacKey:key]; +} ++ (CocoaSecurityResult *)hmacSha384WithData:(NSData *)hashData hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha384WithData:hashData hmacKey:key]; +} - (CocoaSecurityResult *)hmacSha384:(NSString *)hashString hmacKey:(NSString *)key { return [self hmacSha384WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key]; @@ -401,17 +486,23 @@ static CocoaSecurity *_instance = nil; const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding]; CCHmac(kCCHmacAlgSHA384, cKey, strlen(cKey), [hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA384_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA384_DIGEST_LENGTH] autorelease]; -#endif free(digest); cKey = nil; return result; } #pragma mark HMAC-SHA512 ++ (CocoaSecurityResult *)hmacSha512:(NSString *)hashString hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha512:hashString hmacKey:key]; +} ++ (CocoaSecurityResult *)hmacSha512WithData:(NSData *)hashData hmacKey:(NSString *)key +{ + CocoaSecurity *cs = [CocoaSecurity new]; + return [cs hmacSha512WithData:hashData hmacKey:key]; +} - (CocoaSecurityResult *)hmacSha512:(NSString *)hashString hmacKey:(NSString *)key { return [self hmacSha512WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding] hmacKey:key]; @@ -423,43 +514,37 @@ static CocoaSecurity *_instance = nil; const char *cKey = [key cStringUsingEncoding:NSUTF8StringEncoding]; CCHmac(kCCHmacAlgSHA512, cKey, strlen(cKey), [hashData bytes], [hashData length], digest); -#if __has_feature(objc_arc) CocoaSecurityResult *result = [[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA512_DIGEST_LENGTH]; -#else - CocoaSecurityResult *result = [[[CocoaSecurityResult alloc] initWithBytes:digest length:CC_SHA512_DIGEST_LENGTH] autorelease]; -#endif free(digest); cKey = nil; return result; } + @end #pragma mark - CocoaSecurityResult @implementation CocoaSecurityResult -- (id)initWithBytes: (unsigned char[])initData length: (uint) length +@synthesize data = _data; + +#pragma mark - Init +- (id)initWithBytes:(unsigned char[])initData length:(NSUInteger)length { - self.data = [NSData dataWithBytes:initData length:length]; - + self = [super init]; + if (self) { + _data = [NSData dataWithBytes:initData length:length]; + } return self; } -#pragma mark - NSData -@synthesize data; - #pragma mark UTF8 String // convert CocoaSecurityResult to UTF8 string - (NSString *)utf8String { -#if __has_feature(objc_arc) - NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; -#else - NSString *result = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; -#endif - + NSString *result = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding]; return result; } @@ -467,36 +552,21 @@ static CocoaSecurity *_instance = nil; // convert CocoaSecurityResult to HEX string - (NSString *)hex { -#if __has_feature(objc_arc) - CocoaSecurityEncoder *encoder = [[CocoaSecurityEncoder alloc] init]; -#else - CocoaSecurityEncoder *encoder = [[[CocoaSecurityEncoder alloc] init] autorelease]; -#endif - - return [encoder hex:data useLower:false]; + CocoaSecurityEncoder *encoder = [CocoaSecurityEncoder new]; + return [encoder hex:_data useLower:false]; } - (NSString *)hexLower { -#if __has_feature(objc_arc) - CocoaSecurityEncoder *encoder = [[CocoaSecurityEncoder alloc] init]; -#else - CocoaSecurityEncoder *encoder = [[[CocoaSecurityEncoder alloc] init] autorelease]; -#endif - - return [encoder hex:data useLower:true]; + CocoaSecurityEncoder *encoder = [CocoaSecurityEncoder new]; + return [encoder hex:_data useLower:true]; } #pragma mark Base64 // convert CocoaSecurityResult to Base64 string - (NSString *)base64 { -#if __has_feature(objc_arc) - CocoaSecurityEncoder *encoder = [[CocoaSecurityEncoder alloc] init]; -#else - CocoaSecurityEncoder *encoder = [[[CocoaSecurityEncoder alloc] init] autorelease]; -#endif - - return [encoder base64:data]; + CocoaSecurityEncoder *encoder = [CocoaSecurityEncoder new]; + return [encoder base64:_data]; } @end @@ -510,15 +580,11 @@ static CocoaSecurity *_instance = nil; { // base on GTMBase64 NSString *result = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding]; -#if __has_feature(objc_arc) return result; -#else - return [result autorelease]; -#endif } // convert NSData to hex string -- (NSString *)hex: (NSData *)data useLower: (bool)isOutputLower +- (NSString *)hex:(NSData *)data useLower:(bool)isOutputLower { static const char HexEncodeCharsLower[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; static const char HexEncodeChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; @@ -527,17 +593,17 @@ static CocoaSecurity *_instance = nil; resultData = malloc([data length] * 2 +1); // convert imgData(NSData) to char[] unsigned char *sourceData = ((unsigned char *)[data bytes]); - uint length = [data length]; + NSUInteger length = [data length]; if (isOutputLower) { - for (uint index = 0; index < length; index++) { + for (NSUInteger index = 0; index < length; index++) { // set result data resultData[index * 2] = HexEncodeCharsLower[(sourceData[index] >> 4)]; resultData[index * 2 + 1] = HexEncodeCharsLower[(sourceData[index] % 0x10)]; } } else { - for (uint index = 0; index < length; index++) { + for (NSUInteger index = 0; index < length; index++) { // set result data resultData[index * 2] = HexEncodeChars[(sourceData[index] >> 4)]; resultData[index * 2 + 1] = HexEncodeChars[(sourceData[index] % 0x10)]; @@ -557,19 +623,13 @@ static CocoaSecurity *_instance = nil; #pragma mark - CocoaSecurityDecoder @implementation CocoaSecurityDecoder - - (NSData *)base64:(NSString *)data { // base on GTMBase64 NSData *result = [[NSData alloc] initWithData:[GTMBase64 decodeString:data]]; -#if __has_feature(objc_arc) return result; -#else - return [result autorelease]; -#endif } - -- (NSData *)hex: (NSString *)data +- (NSData *)hex:(NSString *)data { static const unsigned char HexDecodeChars[] = { @@ -590,9 +650,9 @@ static CocoaSecurity *_instance = nil; const char *source = [data cStringUsingEncoding:NSUTF8StringEncoding]; // malloc buffer unsigned char *buffer; - uint length = strlen(source) / 2; + NSUInteger length = strlen(source) / 2; buffer = malloc(length); - for (uint index = 0; index < length; index++) { + for (NSUInteger index = 0; index < length; index++) { buffer[index] = (HexDecodeChars[source[index * 2]] << 4) + (HexDecodeChars[source[index * 2 + 1]]); } // init result NSData diff --git a/CocoaSecurityTests/CocoaSecurityTests.m b/CocoaSecurityTests/CocoaSecurityTests.m index afdcb42..3c3f202 100644 --- a/CocoaSecurityTests/CocoaSecurityTests.m +++ b/CocoaSecurityTests/CocoaSecurityTests.m @@ -39,96 +39,90 @@ - (void)testAES { - CocoaSecurity *cs = [CocoaSecurity sharedInstance]; - CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init]; - // AES128 - CocoaSecurityResult *aes128 = [cs aesEncryptWithData:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding] - key:[decoder hex:@"C40C69779E15780ADAE46C45EB451E23"] - iv:[decoder hex:@"CC0A69779E15780ADAE46C45EB451A23"]]; + CocoaSecurityResult *aes128 = [CocoaSecurity aesEncrypt:@"kelp" + hexKey:@"C40C69779E15780ADAE46C45EB451E23" + hexIv:@"CC0A69779E15780ADAE46C45EB451A23"]; STAssertEqualObjects(aes128.base64, @"zT1PS64MnXIUDCUiy13RRg==", nil); - STAssertEqualObjects([cs aesDecryptWithBase64:aes128.base64 - hexKey:@"C40C69779E15780ADAE46C45EB451E23" - hexIv:@"CC0A69779E15780ADAE46C45EB451A23"].utf8String, @"kelp", nil); - + STAssertEqualObjects([CocoaSecurity aesDecryptWithBase64:aes128.base64 + hexKey:@"C40C69779E15780ADAE46C45EB451E23" + hexIv:@"CC0A69779E15780ADAE46C45EB451A23"].utf8String, @"kelp", nil); // AES192 - CocoaSecurityResult *aes192 = [cs aesEncryptWithData:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding] - key:[decoder hex:@"C40C69779E15780ADAE46C45EB451E230000000000000000"] - iv:[decoder hex:@"CC0A69779E15780ADAE46C45EB451A23"]]; + CocoaSecurityResult *aes192 = [CocoaSecurity aesEncrypt:@"kelp" + hexKey:@"C40C69779E15780ADAE46C45EB451E230000000000000000" + hexIv:@"CC0A69779E15780ADAE46C45EB451A23"]; STAssertEqualObjects(aes192.base64, @"zSpp/l/B/Gp+j0vByqcTVg==", nil); - STAssertEqualObjects([cs aesDecryptWithBase64:aes192.base64 - hexKey:@"C40C69779E15780ADAE46C45EB451E230000000000000000" - hexIv:@"CC0A69779E15780ADAE46C45EB451A23"].utf8String, @"kelp", nil); + STAssertEqualObjects([CocoaSecurity aesDecryptWithBase64:aes192.base64 + hexKey:@"C40C69779E15780ADAE46C45EB451E230000000000000000" + hexIv:@"CC0A69779E15780ADAE46C45EB451A23"].utf8String, @"kelp", nil); // AES256 - CocoaSecurityResult *aes256 = [cs aesEncrypt:@"kelp" - hexKey:@"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9" - hexIv:@"CC0A69779E15780ADAE46C45EB451A23"]; + CocoaSecurityResult *aes256 = [CocoaSecurity aesEncrypt:@"kelp" + hexKey:@"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9" + hexIv:@"CC0A69779E15780ADAE46C45EB451A23"]; STAssertEqualObjects(aes256.base64, @"WQYg5qvcGyCBY3IF0hPsoQ==", nil); - STAssertEqualObjects([cs aesDecryptWithBase64:aes256.base64 - hexKey:@"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9" - hexIv:@"CC0A69779E15780ADAE46C45EB451A23"].utf8String, @"kelp", nil); + STAssertEqualObjects([CocoaSecurity aesDecryptWithBase64:aes256.base64 + hexKey:@"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9" + hexIv:@"CC0A69779E15780ADAE46C45EB451A23"].utf8String, @"kelp", nil); - - CocoaSecurityResult *aesDefault = [cs aesEncrypt:@"kelp" key:@"key"]; + // AES default + CocoaSecurityResult *aesDefault = [CocoaSecurity aesEncrypt:@"kelp" key:@"key"]; STAssertEqualObjects(aesDefault.base64, @"ez9uubPneV1d2+rpjnabJw==", nil); - STAssertEqualObjects([cs aesDecryptWithBase64:aesDefault.base64 key:@"key"].utf8String, @"kelp", nil); + STAssertEqualObjects([CocoaSecurity aesDecryptWithBase64:aesDefault.base64 key:@"key"].utf8String, @"kelp", nil); } - (void)testMD5 { - CocoaSecurity *cs = [CocoaSecurity sharedInstance]; - CocoaSecurityResult *md5Result = [cs md5:@"kelp"]; - CocoaSecurityResult *hmacMd5Result = [cs hmacMd5:@"kelp" hmacKey:@"key"]; + CocoaSecurityResult *md5 = [CocoaSecurity md5:@"kelp"]; + CocoaSecurityResult *hmacMd5 = [CocoaSecurity hmacMd5:@"kelp" hmacKey:@"key"]; - STAssertEqualObjects(md5Result.hex, @"C40C69779E15780ADAE46C45EB451E23", nil); - STAssertEqualObjects(md5Result.hexLower, @"c40c69779e15780adae46c45eb451e23", nil); - STAssertEqualObjects(md5Result.base64, @"xAxpd54VeAra5GxF60UeIw==", nil); + STAssertEqualObjects(md5.hex, @"C40C69779E15780ADAE46C45EB451E23", nil); + STAssertEqualObjects(md5.hexLower, @"c40c69779e15780adae46c45eb451e23", nil); + STAssertEqualObjects(md5.base64, @"xAxpd54VeAra5GxF60UeIw==", nil); - STAssertEqualObjects(hmacMd5Result.hex, @"2DFF352719234D5D6A9839FD8F43C8D2", nil); - STAssertEqualObjects(hmacMd5Result.hexLower, @"2dff352719234d5d6a9839fd8f43c8d2", nil); - STAssertEqualObjects(hmacMd5Result.base64, @"Lf81JxkjTV1qmDn9j0PI0g==", nil); + STAssertEqualObjects(hmacMd5.hex, @"2DFF352719234D5D6A9839FD8F43C8D2", nil); + STAssertEqualObjects(hmacMd5.hexLower, @"2dff352719234d5d6a9839fd8f43c8d2", nil); + STAssertEqualObjects(hmacMd5.base64, @"Lf81JxkjTV1qmDn9j0PI0g==", nil); } - (void)testSHA { - CocoaSecurity *cs = [CocoaSecurity sharedInstance]; - CocoaSecurityResult *sha1Result = [cs sha1:@"kelp"]; - CocoaSecurityResult *sha224Result = [cs sha224:@"kelp"]; - CocoaSecurityResult *sha256Result = [cs sha256:@"kelp"]; - CocoaSecurityResult *sha384Result = [cs sha384:@"kelp"]; - CocoaSecurityResult *sha512Result = [cs sha512:@"kelp"]; - CocoaSecurityResult *hmacSha1Result = [cs hmacSha1:@"kelp" hmacKey:@"key"]; - CocoaSecurityResult *hmacSha224Result = [cs hmacSha224:@"kelp" hmacKey:@"key"]; - CocoaSecurityResult *hmacSha256Result = [cs hmacSha256:@"kelp" hmacKey:@"key"]; - CocoaSecurityResult *hmacSha384Result = [cs hmacSha384:@"kelp" hmacKey:@"key"]; - CocoaSecurityResult *hmacSha512Result = [cs hmacSha512:@"kelp" hmacKey:@"key"]; + CocoaSecurityResult *sha1 = [CocoaSecurity sha1:@"kelp"]; + CocoaSecurityResult *sha224 = [CocoaSecurity sha224:@"kelp"]; + CocoaSecurityResult *sha256 = [CocoaSecurity sha256:@"kelp"]; + CocoaSecurityResult *sha384 = [CocoaSecurity sha384:@"kelp"]; + CocoaSecurityResult *sha512 = [CocoaSecurity sha512:@"kelp"]; + CocoaSecurityResult *hmacSha1 = [CocoaSecurity hmacSha1:@"kelp" hmacKey:@"key"]; + CocoaSecurityResult *hmacSha224 = [CocoaSecurity hmacSha224:@"kelp" hmacKey:@"key"]; + CocoaSecurityResult *hmacSha256 = [CocoaSecurity hmacSha256:@"kelp" hmacKey:@"key"]; + CocoaSecurityResult *hmacSha384 = [CocoaSecurity hmacSha384:@"kelp" hmacKey:@"key"]; + CocoaSecurityResult *hmacSha512 = [CocoaSecurity hmacSha512:@"kelp" hmacKey:@"key"]; - STAssertEqualObjects(sha1Result.hexLower, @"70b6a0495fb444a63297c83de187b1730a18e85a", nil); - STAssertEqualObjects(sha1Result.base64, @"cLagSV+0RKYyl8g94YexcwoY6Fo=", nil); - STAssertEqualObjects(hmacSha1Result.hexLower, @"fae888da051e44eb0c57f43935ad82cdbedf482f", nil); - STAssertEqualObjects(hmacSha1Result.base64, @"+uiI2gUeROsMV/Q5Na2Czb7fSC8=", nil); + STAssertEqualObjects(sha1.hexLower, @"70b6a0495fb444a63297c83de187b1730a18e85a", nil); + STAssertEqualObjects(sha1.base64, @"cLagSV+0RKYyl8g94YexcwoY6Fo=", nil); + STAssertEqualObjects(hmacSha1.hexLower, @"fae888da051e44eb0c57f43935ad82cdbedf482f", nil); + STAssertEqualObjects(hmacSha1.base64, @"+uiI2gUeROsMV/Q5Na2Czb7fSC8=", nil); - STAssertEqualObjects(sha224Result.hexLower, @"1e124576cebf14ecdac30b8ca05ff94deb343f54ebb0eab21559dcf1", nil); - STAssertEqualObjects(sha224Result.base64, @"HhJFds6/FOzawwuMoF/5Tes0P1TrsOqyFVnc8Q==", nil); - STAssertEqualObjects(hmacSha224Result.hexLower, @"4777556ee573705fcf6194de22947e09562653a84684c4b015a91e0c", nil); - STAssertEqualObjects(hmacSha224Result.base64, @"R3dVbuVzcF/PYZTeIpR+CVYmU6hGhMSwFakeDA==", nil); + STAssertEqualObjects(sha224.hexLower, @"1e124576cebf14ecdac30b8ca05ff94deb343f54ebb0eab21559dcf1", nil); + STAssertEqualObjects(sha224.base64, @"HhJFds6/FOzawwuMoF/5Tes0P1TrsOqyFVnc8Q==", nil); + STAssertEqualObjects(hmacSha224.hexLower, @"4777556ee573705fcf6194de22947e09562653a84684c4b015a91e0c", nil); + STAssertEqualObjects(hmacSha224.base64, @"R3dVbuVzcF/PYZTeIpR+CVYmU6hGhMSwFakeDA==", nil); - STAssertEqualObjects(sha256Result.hexLower, @"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9", nil); - STAssertEqualObjects(sha256Result.base64, @"KA+LuMQ9Uy84nvDipTISILB4KwZSBdzfy42PAu1RFbk=", nil); - STAssertEqualObjects(hmacSha256Result.hexLower, @"09e6c01ee44e4fc87871d3d8eb5265b67a941e9bf68d1b33851aeeed0114cd33", nil); - STAssertEqualObjects(hmacSha256Result.base64, @"CebAHuROT8h4cdPY61JltnqUHpv2jRszhRru7QEUzTM=", nil); + STAssertEqualObjects(sha256.hexLower, @"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9", nil); + STAssertEqualObjects(sha256.base64, @"KA+LuMQ9Uy84nvDipTISILB4KwZSBdzfy42PAu1RFbk=", nil); + STAssertEqualObjects(hmacSha256.hexLower, @"09e6c01ee44e4fc87871d3d8eb5265b67a941e9bf68d1b33851aeeed0114cd33", nil); + STAssertEqualObjects(hmacSha256.base64, @"CebAHuROT8h4cdPY61JltnqUHpv2jRszhRru7QEUzTM=", nil); - STAssertEqualObjects(sha384Result.hexLower, @"e0801e06e6eea6257018bc0f2aaf1f7ec23385ce2ac9865fe209322262f323e80c81f65e711e30d162af5638ef8b4334", nil); - STAssertEqualObjects(sha384Result.base64, @"4IAeBubupiVwGLwPKq8ffsIzhc4qyYZf4gkyImLzI+gMgfZecR4w0WKvVjjvi0M0", nil); - STAssertEqualObjects(hmacSha384Result.hexLower, @"99f2a12918f5e0c7e21ef4759ecb8dd882c95af32a204ac83928aa413e1d8e9ed312c29c41e2f3c00a78d448df11d15e", nil); - STAssertEqualObjects(hmacSha384Result.base64, @"mfKhKRj14MfiHvR1nsuN2ILJWvMqIErIOSiqQT4djp7TEsKcQeLzwAp41EjfEdFe", nil); + STAssertEqualObjects(sha384.hexLower, @"e0801e06e6eea6257018bc0f2aaf1f7ec23385ce2ac9865fe209322262f323e80c81f65e711e30d162af5638ef8b4334", nil); + STAssertEqualObjects(sha384.base64, @"4IAeBubupiVwGLwPKq8ffsIzhc4qyYZf4gkyImLzI+gMgfZecR4w0WKvVjjvi0M0", nil); + STAssertEqualObjects(hmacSha384.hexLower, @"99f2a12918f5e0c7e21ef4759ecb8dd882c95af32a204ac83928aa413e1d8e9ed312c29c41e2f3c00a78d448df11d15e", nil); + STAssertEqualObjects(hmacSha384.base64, @"mfKhKRj14MfiHvR1nsuN2ILJWvMqIErIOSiqQT4djp7TEsKcQeLzwAp41EjfEdFe", nil); - STAssertEqualObjects(sha512Result.hexLower, @"af8489a9fb6dcb8973515cdda3366c939ebcc8ac8fb0a7c322f1333babe03655222930ad48b4924f1a1f13c23856bc3c2e1b93cb10c74e72362e5457756517ff", nil); - STAssertEqualObjects(sha512Result.base64, @"r4SJqftty4lzUVzdozZsk568yKyPsKfDIvEzO6vgNlUiKTCtSLSSTxofE8I4Vrw8LhuTyxDHTnI2LlRXdWUX/w==", nil); - STAssertEqualObjects(hmacSha512Result.hexLower, @"3807619fdaa2dd77e3dd554a627284406000a5c924db72202af0e6b1832789a94bacc710dc2b7da61fbfd6e1065dfe39085a872538f5b19fde112092c90d893a", nil); - STAssertEqualObjects(hmacSha512Result.base64, @"OAdhn9qi3Xfj3VVKYnKEQGAApckk23IgKvDmsYMnialLrMcQ3Ct9ph+/1uEGXf45CFqHJTj1sZ/eESCSyQ2JOg==", nil); + STAssertEqualObjects(sha512.hexLower, @"af8489a9fb6dcb8973515cdda3366c939ebcc8ac8fb0a7c322f1333babe03655222930ad48b4924f1a1f13c23856bc3c2e1b93cb10c74e72362e5457756517ff", nil); + STAssertEqualObjects(sha512.base64, @"r4SJqftty4lzUVzdozZsk568yKyPsKfDIvEzO6vgNlUiKTCtSLSSTxofE8I4Vrw8LhuTyxDHTnI2LlRXdWUX/w==", nil); + STAssertEqualObjects(hmacSha512.hexLower, @"3807619fdaa2dd77e3dd554a627284406000a5c924db72202af0e6b1832789a94bacc710dc2b7da61fbfd6e1065dfe39085a872538f5b19fde112092c90d893a", nil); + STAssertEqualObjects(hmacSha512.base64, @"OAdhn9qi3Xfj3VVKYnKEQGAApckk23IgKvDmsYMnialLrMcQ3Ct9ph+/1uEGXf45CFqHJTj1sZ/eESCSyQ2JOg==", nil); } @end diff --git a/README.md b/README.md index bb6b630..37820ae 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,48 @@ -#CocoaSecurity 1.0.1 +#CocoaSecurity -Kelp http://kelp.phate.org/
-[MIT License][mit]
+Kelp http://kelp.phate.org/ +[MIT License][mit] [Apache License, Version 2.0][Apache] : GTMBase64 by Google Inc. +[Apache]: http://www.apache.org/licenses/LICENSE-2.0 +[MIT]: http://www.opensource.org/licenses/mit-license.php -CocoaSecurity include 4 classes, **CocoaSecurity**, **CocoaSecurityResult**, **CocoaSecurityEncoder** and **CocoaSecurityDecoder**. +CocoaSecurity include 4 classes, `CocoaSecurity`, `CocoaSecurityResult`, `CocoaSecurityEncoder` and `CocoaSecurityDecoder`. ##CocoaSecurity -CocoaSecurity is core. It provides AES encrypt, AES decrypt, Hash(MD5, HmacMD5, SHA1~SHA512, HmacSHA1~HmacSHA512) messages. -

+CocoaSecurity is core. It provides AES encrypt, AES decrypt, Hash(MD5, HmacMD5, SHA1~SHA512, HmacSHA1~HmacSHA512) messages. + **MD5:** ```objective-c -CocoaSecurity *cs = [[[CocoaSecurity alloc] init] autorelease]; -CocoaSecurityResult *md5Result = [cs md5:@"kelp"]; +CocoaSecurityResult *md5 = [CocoaSecurity md5:@"kelp"]; -// md5Result.hex = 'C40C69779E15780ADAE46C45EB451E23' -// md5Result.hexLower = 'c40c69779e15780adae46c45eb451e23' -// md5Result.base64 = 'xAxpd54VeAra5GxF60UeIw==' +// md5.hex = 'C40C69779E15780ADAE46C45EB451E23' +// md5.hexLower = 'c40c69779e15780adae46c45eb451e23' +// md5.base64 = 'xAxpd54VeAra5GxF60UeIw==' ``` **SHA256:** ```objective-c -CocoaSecurity *cs = [[[CocoaSecurity alloc] init] autorelease]; -CocoaSecurityResult *sha256Result = [cs sha256:@"kelp"]; +CocoaSecurityResult *sha256 = [CocoaSecurity sha256:@"kelp"]; -// sha256Result.hexLower = '280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9' -// sha256Result.base64 = 'KA+LuMQ9Uy84nvDipTISILB4KwZSBdzfy42PAu1RFbk=' +// sha256.hexLower = '280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9' +// sha256.base64 = 'KA+LuMQ9Uy84nvDipTISILB4KwZSBdzfy42PAu1RFbk=' ``` **default AES Encrypt:**
key -> SHA384(key).sub(0, 32)
iv -> SHA384(key).sub(32, 16) ```objective-c -CocoaSecurity *cs = [[[CocoaSecurity alloc] init] autorelease]; -CocoaSecurityResult *aesDefault = [cs aesEncrypt:@"kelp" key:@"key"]; +CocoaSecurityResult *aesDefault = [CocoaSecurity aesEncrypt:@"kelp" key:@"key"]; // aesDefault.base64 = 'ez9uubPneV1d2+rpjnabJw==' ``` **AES256 Encrypt & Decrypt:** ```objective-c -CocoaSecurity *cs = [[[CocoaSecurity alloc] init] autorelease]; - -CocoaSecurityResult *aes256 = [cs aesEncrypt:@"kelp" +CocoaSecurityResult *aes256 = [CocoaSecurity aesEncrypt:@"kelp" hexKey:@"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9" hexIv:@"CC0A69779E15780ADAE46C45EB451A23"]; // aes256.base64 = 'WQYg5qvcGyCBY3IF0hPsoQ==' -CocoaSecurityResult *aes256Decrypt = [cs aesDecryptWithBase64:@"WQYg5qvcGyCBY3IF0hPsoQ==" +CocoaSecurityResult *aes256Decrypt = [CocoaSecurity aesDecryptWithBase64:@"WQYg5qvcGyCBY3IF0hPsoQ==" hexKey:@"280f8bb8c43d532f389ef0e2a5321220b0782b065205dcdfcb8d8f02ed5115b9" hexIv:@"CC0A69779E15780ADAE46C45EB451A23"]; // aes256Decrypt.utf8String = 'kelp' @@ -56,11 +53,11 @@ CocoaSecurityResult *aes256Decrypt = [cs aesDecryptWithBase64:@"WQYg5qvcGyCBY3IF CocoaSecurityResult is the result class of CocoaSecurity. It provides convert result data to NSData, NSString, HEX string, Base64 string. ```objective-c -@property (retain) NSData *data; -@property (retain, readonly) NSString *utf8String; -@property (retain, readonly) NSString *hex; -@property (retain, readonly) NSString *hexLower; -@property (retain, readonly) NSString *base64; +@property (strong, nonatomic, readonly) NSData *data; +@property (strong, nonatomic, readonly) NSString *utf8String; +@property (strong, nonatomic, readonly) NSString *hex; +@property (strong, nonatomic, readonly) NSString *hexLower; +@property (strong, nonatomic, readonly) NSString *base64; ``` @@ -68,13 +65,13 @@ CocoaSecurityResult is the result class of CocoaSecurity. It provides convert re CocoaSecurityEncoder provides convert NSData to HEX string, Base64 string. ```objective-c -- (NSString *)base64: (NSData *)data; -- (NSString *)hex: (NSData *)data useLower: (bool)isOutputLower; +- (NSString *)base64:(NSData *)data; +- (NSString *)hex:(NSData *)data useLower:(bool)isOutputLower; ``` **example:** ```objective-c -CocoaSecurityEncoder *encoder = [[[CocoaSecurityEncoder alloc] init] autorelease]; -NSString *str1 = [encoder hex:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding] useLower:false]; +CocoaSecurityEncoder *encoder = [CocoaSecurityEncoder new]; +NSString *str1 = [encoder hex:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding] useLower:NO]; // str1 = '6B656C70' NSString *str2 = [encoder base64:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding]]; // str2 = 'a2VscA==' @@ -84,17 +81,18 @@ NSString *str2 = [encoder base64:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding CocoaSecurityEncoder provides convert HEX string or Base64 string to NSData. ```objective-c -- (NSData *)base64: (NSString *)data; -- (NSData *)hex: (NSString *)data; +- (NSData *)base64:(NSString *)data; +- (NSData *)hex:(NSString *)data; ``` **example:** ```objective-c -CocoaSecurityDecoder *decoder = [[[CocoaSecurityDecoder alloc] init] autorelease]; +CocoaSecurityDecoder *decoder = [CocoaSecurityDecoder new]; NSData *data1 = [decoder hex:@"CC0A69779E15780ADAE46C45EB451A23"]; // data1 = NSData *data2 = [decoder base64:@"zT1PS64MnXIUDCUiy13RRg=="]; // data2 = ``` -[Apache]: http://www.apache.org/licenses/LICENSE-2.0 -[MIT]: http://www.opensource.org/licenses/mit-license.php \ No newline at end of file +##History +1.1 Only for ARC project. +1.0.1 The last version support disable ARC. \ No newline at end of file