Permalink
Browse files

Refactor : Move sha1,sha256 from NSData(AES) -> NSData(CryptoHash)

  • Loading branch information...
1 parent 5bc0465 commit d1dd73948c5ad582edbe1a92e31cfb6a91e94dc0 @PureAbstract committed Jun 9, 2012
View
@@ -13,10 +13,6 @@
//
// Note that all these funtions return NSMutableData (vs NSData).
// This is primarily so they can be wiped after use if necessary
-// The first few aren't really AES...
--(NSMutableData *)sha256;
--(NSMutableData *)sha1;
-// Generate some random bytes
+(NSMutableData *)randomBytes:(size_t)length;
// These are though
View
@@ -7,28 +7,14 @@
//
#import "NSData+AES.h"
+#import "NSData+CryptoHash.h"
#import <CommonCrypto/CommonCryptor.h>
-#import <CommonCrypto/CommonDigest.h>
#if !TARGET_OS_IPHONE
#import "OSX_SecRandom.h"
#endif
@implementation NSData (AES)
--(NSMutableData *)sha256
-{
- NSMutableData *hash = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
- CC_SHA256( self.bytes, self.length, hash.mutableBytes );
- return hash;
-}
-
--(NSMutableData *)sha1
-{
- NSMutableData *hash = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH];
- CC_SHA1( self.bytes, self.length, hash.mutableBytes );
- return hash;
-}
-
+(NSMutableData *)randomBytes:(size_t)length
{
NSMutableData *bytes = [NSMutableData dataWithLength:length];
@@ -42,13 +28,18 @@ +(NSMutableData *)randomBytes:(size_t)length
-(NSMutableData *)cryptOperation:(CCOperation) operation
withKey:(NSData *)key
{
- NSAssert( key, @"Null key!");
+ if( !key ) {
+ NSAssert( key, @"Null key!");
+ return nil;
+ }
if( key.length != kCCKeySizeAES256 ) {
// Key the wrong size, go hash with SHA256...
- NSAssert( CC_SHA256_DIGEST_LENGTH == kCCKeySizeAES256, @"This is odd...");
key = [key sha256];
}
NSAssert( key.length == kCCKeySizeAES256, @"Bad key size" );
+ if( key.length != kCCKeySizeAES256 ) {
+ return nil;
+ }
// Calculate the required output buffer size.
// This may be slightly pessimistic, but AFAICT, the worst case is
@@ -0,0 +1,18 @@
+//
+// NSData+CryptoHash.h
+// PWStore
+//
+// Hash functions for NSData objects
+//
+// Created by Andy Sawyer on 09/06/2012.
+// Copyright 2012 Andy Sawyer. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface NSData (CryptoHash)
+// Cryptographic Hash Functions Note that the returned objects are
+// mutable so you can wipe them if you like
+-(NSMutableData *)sha256;
+-(NSMutableData *)sha1;
+@end
@@ -0,0 +1,28 @@
+//
+// NSData+CryptoHash.m
+// PWStore
+//
+// Created by Andy Sawyer on 09/06/2012.
+// Copyright 2012 Andy Sawyer. All rights reserved.
+//
+
+#import "NSData+CryptoHash.h"
+//#import <CommonCrypto/CommonCryptor.h>
+#import <CommonCrypto/CommonDigest.h>
+
+
+@implementation NSData (CryptoHash)
+-(NSMutableData *)sha256
+{
+ NSMutableData *hash = [NSMutableData dataWithLength:CC_SHA256_DIGEST_LENGTH];
+ CC_SHA256( self.bytes, self.length, hash.mutableBytes );
+ return hash;
+}
+
+-(NSMutableData *)sha1
+{
+ NSMutableData *hash = [NSMutableData dataWithLength:CC_SHA1_DIGEST_LENGTH];
+ CC_SHA1( self.bytes, self.length, hash.mutableBytes );
+ return hash;
+}
+@end
@@ -13,6 +13,7 @@
#import "SyncViewController.h"
#import "PWItem.h"
#import "NSData+AES.h"
+#import "NSData+CryptoHash.h"
#import "UIApplication+Utility.h"
#import "NSString+Utility.h"
#import "UIViewController+TabBarItem.h"
@@ -43,6 +43,7 @@
E3995065157AB505001CE6D3 /* PWData+StringExport.m in Sources */ = {isa = PBXBuildFile; fileRef = E3995064157AB505001CE6D3 /* PWData+StringExport.m */; };
E3995284157CEAE7001CE6D3 /* libxml2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E3995283157CEAE7001CE6D3 /* libxml2.dylib */; };
E39AA44D1582928B00A93BCE /* OSX_SecRandom.m in Sources */ = {isa = PBXBuildFile; fileRef = E39AA44C1582928B00A93BCE /* OSX_SecRandom.m */; };
+ E3A39DAD158370DD006754C5 /* NSData+CryptoHash.m in Sources */ = {isa = PBXBuildFile; fileRef = E3A39DAC158370DD006754C5 /* NSData+CryptoHash.m */; };
E3C91235157D3ACC0055AAD6 /* SyncViewController+TestDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C91234157D3ACC0055AAD6 /* SyncViewController+TestDriver.m */; };
E3C912F6157D4A040055AAD6 /* XmlDocument.m in Sources */ = {isa = PBXBuildFile; fileRef = E3C912F5157D4A040055AAD6 /* XmlDocument.m */; };
/* End PBXBuildFile section */
@@ -109,6 +110,8 @@
E3995283157CEAE7001CE6D3 /* libxml2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libxml2.dylib; path = usr/lib/libxml2.dylib; sourceTree = SDKROOT; };
E39AA44B1582928B00A93BCE /* OSX_SecRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OSX_SecRandom.h; path = OSX_Support/OSX_SecRandom.h; sourceTree = "<group>"; };
E39AA44C1582928B00A93BCE /* OSX_SecRandom.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OSX_SecRandom.m; path = OSX_Support/OSX_SecRandom.m; sourceTree = "<group>"; };
+ E3A39DAB158370DD006754C5 /* NSData+CryptoHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+CryptoHash.h"; sourceTree = "<group>"; };
+ E3A39DAC158370DD006754C5 /* NSData+CryptoHash.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+CryptoHash.m"; sourceTree = "<group>"; };
E3C91233157D3ACC0055AAD6 /* SyncViewController+TestDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "SyncViewController+TestDriver.h"; sourceTree = "<group>"; };
E3C91234157D3ACC0055AAD6 /* SyncViewController+TestDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "SyncViewController+TestDriver.m"; sourceTree = "<group>"; };
E3C912F4157D4A040055AAD6 /* XmlDocument.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = XmlDocument.h; sourceTree = "<group>"; };
@@ -227,6 +230,8 @@
E35958ED157A5807005FDA20 /* Categories */ = {
isa = PBXGroup;
children = (
+ E3A39DAB158370DD006754C5 /* NSData+CryptoHash.h */,
+ E3A39DAC158370DD006754C5 /* NSData+CryptoHash.m */,
E38E44FC1575518C00FB0AF6 /* NSData+AES.h */,
E38E44FD1575518C00FB0AF6 /* NSData+AES.m */,
E384DD69157582C400A90DDA /* NSCoder+withDefaults.h */,
@@ -408,6 +413,7 @@
E3C91235157D3ACC0055AAD6 /* SyncViewController+TestDriver.m in Sources */,
E3C912F6157D4A040055AAD6 /* XmlDocument.m in Sources */,
E39AA44D1582928B00A93BCE /* OSX_SecRandom.m in Sources */,
+ E3A39DAD158370DD006754C5 /* NSData+CryptoHash.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit d1dd739

Please sign in to comment.