├── LICENSE ├── NSString+Hashes.h ├── NSString-Hashes.podspec ├── NSData+Hashes.h ├── NSString+Hashes.m ├── NSData+Hashes_Internal.h ├── Tests.m └── NSData+Hashes.m /LICENSE: -------------------------------------------------------------------------------- 1 | Public Domain 2 | -------------------------------------------------------------------------------- /NSString+Hashes.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSString+Hashes.h 3 | // 4 | // Created by Klaus-Peter Dudas on 26/07/2011. 5 | // Copyright: Do whatever you want with this, i.e. Public Domain 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface NSString (Hashes) 13 | 14 | @property (nonatomic, readonly) NSString *md5; 15 | @property (nonatomic, readonly) NSString *sha1; 16 | @property (nonatomic, readonly) NSString *sha224; 17 | @property (nonatomic, readonly) NSString *sha256; 18 | @property (nonatomic, readonly) NSString *sha384; 19 | @property (nonatomic, readonly) NSString *sha512; 20 | 21 | @end 22 | 23 | NS_ASSUME_NONNULL_END 24 | -------------------------------------------------------------------------------- /NSString-Hashes.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | 3 | s.name = "NSString-Hashes" 4 | s.version = "1.3.1" 5 | s.summary = "Simple Category of NSString and NSData that allows for easy MD5, SHA1 and SHA2 hashing." 6 | s.homepage = "https://github.com/hypercrypt/NSString-Hashes" 7 | 8 | s.license = { :type => "public domain", :file => 'LICENSE' } 9 | 10 | s.author = { "Klaus-Peter Dudas" => "klaus@hypercrypt.net" } 11 | 12 | s.source = { 13 | :git => "https://github.com/hypercrypt/NSString-Hashes.git", 14 | :tag => "#{s.version}" 15 | } 16 | 17 | s.ios.deployment_target = '5.0' 18 | s.osx.deployment_target = '10.8' 19 | 20 | s.source_files = '[^Tests]*.{h,m}' 21 | s.public_header_files = 'NSString+Hashes.h', 'NSData+Hashes.h' 22 | 23 | s.requires_arc = true 24 | 25 | s.test_spec do |ts| 26 | ts.source_files = 'Tests.m' 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /NSData+Hashes.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSData+Hashes.h 3 | // 4 | // Created by anivaros on 24.02.2020. 5 | // Copyright: Do whatever you want with this, i.e. Public Domain 6 | // 7 | 8 | #import 9 | 10 | NS_ASSUME_NONNULL_BEGIN 11 | 12 | @interface NSData (Hashes) 13 | 14 | @property (nonatomic, readonly) NSString *md5; 15 | @property (nonatomic, readonly) NSString *sha1; 16 | @property (nonatomic, readonly) NSString *sha224; 17 | @property (nonatomic, readonly) NSString *sha256; 18 | @property (nonatomic, readonly) NSString *sha384; 19 | @property (nonatomic, readonly) NSString *sha512; 20 | 21 | @property (nonatomic, readonly) NSData *md5Data; 22 | @property (nonatomic, readonly) NSData *sha1Data; 23 | @property (nonatomic, readonly) NSData *sha224Data; 24 | @property (nonatomic, readonly) NSData *sha256Data; 25 | @property (nonatomic, readonly) NSData *sha384Data; 26 | @property (nonatomic, readonly) NSData *sha512Data; 27 | 28 | @end 29 | 30 | NS_ASSUME_NONNULL_END 31 | -------------------------------------------------------------------------------- /NSString+Hashes.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSString+Hashes.m 3 | // 4 | // Created by Klaus-Peter Dudas on 26/07/2011. 5 | // Copyright: Do whatever you want with this, i.e. Public Domain 6 | // 7 | 8 | #import "NSString+Hashes.h" 9 | #import "NSData+Hashes_Internal.h" 10 | 11 | @implementation NSString (Hashes) 12 | 13 | - (NSString *)md5 14 | { 15 | return NSStringCCHashFunction(CC_MD5, CC_MD5_DIGEST_LENGTH, [self dataUsingEncoding:NSUTF8StringEncoding]); 16 | } 17 | 18 | - (NSString *)sha1 19 | { 20 | return NSStringCCHashFunction(CC_SHA1, CC_SHA1_DIGEST_LENGTH, [self dataUsingEncoding:NSUTF8StringEncoding]); 21 | } 22 | 23 | - (NSString *)sha224 24 | { 25 | return NSStringCCHashFunction(CC_SHA224, CC_SHA224_DIGEST_LENGTH, [self dataUsingEncoding:NSUTF8StringEncoding]); 26 | } 27 | 28 | - (NSString *)sha256 29 | { 30 | return NSStringCCHashFunction(CC_SHA256, CC_SHA256_DIGEST_LENGTH, [self dataUsingEncoding:NSUTF8StringEncoding]); 31 | } 32 | 33 | - (NSString *)sha384 34 | { 35 | return NSStringCCHashFunction(CC_SHA384, CC_SHA384_DIGEST_LENGTH, [self dataUsingEncoding:NSUTF8StringEncoding]); 36 | } 37 | - (NSString *)sha512 38 | { 39 | return NSStringCCHashFunction(CC_SHA512, CC_SHA512_DIGEST_LENGTH, [self dataUsingEncoding:NSUTF8StringEncoding]); 40 | } 41 | 42 | @end 43 | -------------------------------------------------------------------------------- /NSData+Hashes_Internal.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSData+Hashes_Internal.h 3 | // 4 | // Created by anivaros on 24.02.2020. 5 | // Copyright: Do whatever you want with this, i.e. Public Domain 6 | // 7 | 8 | #import 9 | #import 10 | 11 | 12 | // MARK - Private 13 | 14 | static inline uint8_t * NSDataInternalCCHashFunction(unsigned char *(function)(const void *data, CC_LONG len, unsigned char *md), CC_LONG digestLength, NSData *data) 15 | { 16 | uint8_t *digest = malloc(digestLength); 17 | function(data.bytes, (CC_LONG)data.length, digest); 18 | return digest; 19 | } 20 | 21 | // MARK - Internal 22 | 23 | static inline NSData *NSDataCCHashFunction(unsigned char *(function)(const void *function, CC_LONG len, unsigned char *md), CC_LONG digestLength, NSData *data) 24 | { 25 | uint8_t *digest = NSDataInternalCCHashFunction(function, digestLength, data); 26 | NSData *hashData = [NSData dataWithBytes:digest length:digestLength]; 27 | free(digest); 28 | return hashData; 29 | } 30 | 31 | static inline NSString *NSStringCCHashFunction(unsigned char *(function)(const void *data, CC_LONG len, unsigned char *md), CC_LONG digestLength, NSData *data) 32 | { 33 | uint8_t *digest = NSDataInternalCCHashFunction(function, digestLength, data); 34 | 35 | NSMutableString *output = [NSMutableString stringWithCapacity:digestLength * 2]; 36 | 37 | for (int i = 0; i < digestLength; i++) 38 | { 39 | [output appendFormat:@"%02x", digest[i]]; 40 | } 41 | free(digest); 42 | return output; 43 | } 44 | -------------------------------------------------------------------------------- /Tests.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSData+Hashes.m 3 | // 4 | // Created by anivaros on 24.02.2020. 5 | // Copyright: Do whatever you want with this, i.e. Public Domain 6 | // 7 | 8 | @import XCTest; 9 | @import NSString_Hashes; 10 | 11 | @interface Tests : XCTestCase 12 | @end 13 | 14 | @implementation Tests 15 | 16 | - (void)testStringMd5 17 | { 18 | NSString *testString = @"test"; 19 | 20 | NSString *hashOfTestString = @"098f6bcd4621d373cade4e832627b4f6"; 21 | 22 | XCTAssertEqualObjects(testString.md5, hashOfTestString); 23 | } 24 | 25 | - (void)testStringSha1 26 | { 27 | NSString *testString = @"test"; 28 | 29 | NSString *hashOfTestString = @"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"; 30 | 31 | XCTAssertEqualObjects(testString.sha1, hashOfTestString); 32 | } 33 | 34 | - (void)testStringSha256 35 | { 36 | NSString *testString = @"test"; 37 | 38 | NSString *hashOfTestString = @"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"; 39 | 40 | XCTAssertEqualObjects(testString.sha256, hashOfTestString); 41 | } 42 | NSString *testString = @"test"; 43 | 44 | - (void)testDataMd5 45 | { 46 | NSString *testString = @"test"; 47 | XCTAssertEqualObjects([testString dataUsingEncoding:NSUTF8StringEncoding].md5, @"098f6bcd4621d373cade4e832627b4f6"); 48 | } 49 | 50 | - (void)testDataMd5Data 51 | { 52 | NSString *testString = @"test"; 53 | 54 | char hexHashOfTestString[] = {0x09, 0x8f, 0x6b, 0xcd, 0x46, 0x21, 0xd3, 0x73, 0xca, 0xde, 0x4e, 0x83, 0x26, 0x27, 0xb4, 0xf6}; 55 | 56 | XCTAssertEqualObjects([testString dataUsingEncoding:NSUTF8StringEncoding].md5Data, [NSData dataWithBytes:hexHashOfTestString length:16]); 57 | } 58 | 59 | @end 60 | -------------------------------------------------------------------------------- /NSData+Hashes.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSData+Hashes.m 3 | // 4 | // Created by anivaros on 24.02.2020. 5 | // Copyright: Do whatever you want with this, i.e. Public Domain 6 | // 7 | 8 | #import "NSData+Hashes.h" 9 | #import "NSData+Hashes_Internal.h" 10 | 11 | @implementation NSData (Hashes) 12 | 13 | - (NSString *)md5 14 | { 15 | return NSStringCCHashFunction(CC_MD5, CC_MD5_DIGEST_LENGTH, self); 16 | } 17 | 18 | - (NSString *)sha1 19 | { 20 | return NSStringCCHashFunction(CC_SHA1, CC_SHA1_DIGEST_LENGTH, self); 21 | } 22 | 23 | - (NSString *)sha224 24 | { 25 | return NSStringCCHashFunction(CC_SHA224, CC_SHA224_DIGEST_LENGTH, self); 26 | } 27 | 28 | - (NSString *)sha256 29 | { 30 | return NSStringCCHashFunction(CC_SHA256, CC_SHA256_DIGEST_LENGTH, self); 31 | } 32 | 33 | - (NSString *)sha384 34 | { 35 | return NSStringCCHashFunction(CC_SHA384, CC_SHA384_DIGEST_LENGTH, self); 36 | } 37 | - (NSString *)sha512 38 | { 39 | return NSStringCCHashFunction(CC_SHA512, CC_SHA512_DIGEST_LENGTH, self); 40 | } 41 | 42 | - (NSData *)md5Data 43 | { 44 | return NSDataCCHashFunction(CC_MD5, CC_MD5_DIGEST_LENGTH, self); 45 | } 46 | 47 | - (NSData *)sha1Data 48 | { 49 | return NSDataCCHashFunction(CC_SHA1, CC_SHA1_DIGEST_LENGTH, self); 50 | } 51 | 52 | - (NSData *)sha224Data 53 | { 54 | return NSDataCCHashFunction(CC_SHA224, CC_SHA224_DIGEST_LENGTH, self); 55 | } 56 | 57 | - (NSData *)sha256Data 58 | { 59 | return NSDataCCHashFunction(CC_SHA256, CC_SHA256_DIGEST_LENGTH, self); 60 | } 61 | 62 | - (NSData *)sha384Data 63 | { 64 | return NSDataCCHashFunction(CC_SHA384, CC_SHA384_DIGEST_LENGTH, self); 65 | } 66 | - (NSData *)sha512Data 67 | { 68 | return NSDataCCHashFunction(CC_SHA512, CC_SHA512_DIGEST_LENGTH, self); 69 | } 70 | 71 | @end 72 | --------------------------------------------------------------------------------