update ARC version

This commit is contained in:
Kelp 2012-05-15 21:50:57 +08:00
parent 2d7cd4f038
commit aa3ba0229b
8 changed files with 143 additions and 25 deletions

View File

@ -337,6 +337,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
@ -362,6 +363,7 @@
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ARCHS = "$(ARCHS_STANDARD_32_BIT)";
CLANG_ENABLE_OBJC_ARC = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;

View File

@ -1,9 +1,16 @@
//
// CocoaSecurity.h
//
// Created by Kelp on 12/5/12.
// Copyright (c) 2012 Kelp http://kelp.phate.org/
//
/*
CocoaSecurity 1.0.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 <Foundation/Foundation.h>
#import <Foundation/NSException.h>

View File

@ -3,6 +3,7 @@
//
// Created by Kelp on 12/5/12.
// Copyright (c) 2012 Kelp http://kelp.phate.org/
// MIT License
//
#import "CocoaSecurity.h"
@ -26,7 +27,9 @@
CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init];
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];
}
@ -66,7 +69,11 @@
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;
@ -94,13 +101,19 @@
CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init];
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
return [self aesDecryptWithData:[decoder base64:data] key:key iv:iv];
}
@ -136,7 +149,11 @@
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;
@ -160,7 +177,11 @@
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;
@ -177,7 +198,11 @@
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;
@ -195,7 +220,11 @@
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;
@ -211,7 +240,11 @@
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;
@ -227,7 +260,11 @@
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;
@ -243,7 +280,11 @@
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;
@ -259,7 +300,11 @@
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;
@ -277,7 +322,11 @@
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;
@ -295,7 +344,11 @@
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;
@ -313,7 +366,11 @@
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;
@ -331,7 +388,11 @@
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;
@ -349,7 +410,11 @@
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;
@ -376,7 +441,11 @@
// 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
return result;
}
@ -385,13 +454,21 @@
// 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];
}
- (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];
}
@ -400,7 +477,11 @@
// 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];
}
@ -416,7 +497,11 @@
{
// 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
@ -464,7 +549,11 @@
{
// 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

View File

@ -283,8 +283,13 @@ GTM_INLINE NSUInteger GuessDecodedLength(NSUInteger srcLen) {
charset:kBase64EncodeChars
padded:YES];
if (converted) {
#if __has_feature(objc_arc)
result = [[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding];
#else
result = [[[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding] autorelease];
#endif
}
return result;
}
@ -296,8 +301,13 @@ GTM_INLINE NSUInteger GuessDecodedLength(NSUInteger srcLen) {
charset:kBase64EncodeChars
padded:YES];
if (converted) {
#if __has_feature(objc_arc)
result = [[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding];
#else
result = [[[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding] autorelease];
#endif
}
return result;
}
@ -362,8 +372,13 @@ GTM_INLINE NSUInteger GuessDecodedLength(NSUInteger srcLen) {
charset:kWebSafeBase64EncodeChars
padded:padded];
if (converted) {
#if __has_feature(objc_arc)
result = [[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding];
#else
result = [[[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding] autorelease];
#endif
}
return result;
}
@ -377,8 +392,13 @@ GTM_INLINE NSUInteger GuessDecodedLength(NSUInteger srcLen) {
charset:kWebSafeBase64EncodeChars
padded:padded];
if (converted) {
#if __has_feature(objc_arc)
result = [[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding];
#else
result = [[[NSString alloc] initWithData:converted
encoding:NSASCIIStringEncoding] autorelease];
#endif
}
return result;
}

View File

@ -26,8 +26,8 @@
- (void)testEncodeDecode
{
CocoaSecurityDecoder *decoder = [[[CocoaSecurityDecoder alloc] init] autorelease];
CocoaSecurityEncoder *encoder = [[[CocoaSecurityEncoder alloc] init] autorelease];
CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init];
CocoaSecurityEncoder *encoder = [[CocoaSecurityEncoder alloc] init];
// HEX
STAssertEqualObjects([encoder hex:[decoder hex:@"CC0A69779E15780ADAE46C45EB451A23"] useLower:false],
@ -39,8 +39,8 @@
- (void)testAES
{
CocoaSecurity *cs = [[[CocoaSecurity alloc] init] autorelease];
CocoaSecurityDecoder *decoder = [[[CocoaSecurityDecoder alloc] init] autorelease];
CocoaSecurity *cs = [[CocoaSecurity alloc] init];
CocoaSecurityDecoder *decoder = [[CocoaSecurityDecoder alloc] init];
// AES128
CocoaSecurityResult *aes128 = [cs aesEncryptWithData:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding]
@ -78,7 +78,7 @@
- (void)testMD5
{
CocoaSecurity *cs = [[[CocoaSecurity alloc] init] autorelease];
CocoaSecurity *cs = [[CocoaSecurity alloc] init];
CocoaSecurityResult *md5Result = [cs md5:@"kelp"];
CocoaSecurityResult *hmacMd5Result = [cs hmacMd5:@"kelp" hmacKey:@"key"];
@ -93,7 +93,7 @@
- (void)testSHA
{
CocoaSecurity *cs = [[[CocoaSecurity alloc] init] autorelease];
CocoaSecurity *cs = [[CocoaSecurity alloc] init];
CocoaSecurityResult *sha1Result = [cs sha1:@"kelp"];
CocoaSecurityResult *sha224Result = [cs sha224:@"kelp"];
CocoaSecurityResult *sha256Result = [cs sha256:@"kelp"];

View File

@ -1,4 +1,4 @@
##CocoaSecurity 1.0
#CocoaSecurity 1.0.1
Kelp http://kelp.phate.org/ <br/>
MIT License <br/>
@ -7,7 +7,7 @@ Apache Licence 2.0: GTMBase64 by Google Inc.
CocoaSecurity include 4 classes, **CocoaSecurity**, **CocoaSecurityResult**, **CocoaSecurityEncoder** and **CocoaSecurityDecoder**.
###CocoaSecurity
##CocoaSecurity
CocoaSecurity is core. It provides AES encrypt, AES decrypt, Hash(MD5, HmacMD5, SHA1~SHA512, HmacSHA1~HmacSHA512) messages.
<br/><br/>
**MD5:**
@ -52,7 +52,7 @@ CocoaSecurityResult *aes256Decrypt = [cs aesDecryptWithBase64:@"WQYg5qvcGyCBY3IF
```
###CocoaSecurityResult
##CocoaSecurityResult
CocoaSecurityResult is the result class of CocoaSecurity. It provides convert result data to NSData, NSString, HEX string, Base64 string.
```objective-c
@ -64,7 +64,7 @@ CocoaSecurityResult is the result class of CocoaSecurity. It provides convert re
```
###CocoaSecurityEncoder
##CocoaSecurityEncoder
CocoaSecurityEncoder provides convert NSData to HEX string, Base64 string.
```objective-c
@ -80,7 +80,7 @@ NSString *str2 = [encoder base64:[@"kelp" dataUsingEncoding:NSUTF8StringEncoding
// str2 = 'a2VscA=='
```
###CocoaSecurityDecoder
##CocoaSecurityDecoder
CocoaSecurityEncoder provides convert HEX string or Base64 string to NSData.
```objective-c

View File

@ -14,8 +14,8 @@
- (void)dealloc
{
[_window release];
[super dealloc];
//[_window release];
//[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions