Skip to content

Commit

Permalink
Add method to make Base64 SHA1 digest from string. Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pk committed Apr 23, 2014
1 parent d61b842 commit 95665e1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Source/Categories/NSString+PKAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
// Copyright (c) 2013 Pavel Kunc. All rights reserved.
//

#import <Foundation/Foundation.h>
@import Foundation;
#import <CommonCrypto/CommonDigest.h>

@interface NSString (PKAdditions)

- (id)pk_stringByReplacingNumericHTMLEntities;
- (instancetype)pk_stringByReplacingNumericHTMLEntities;
- (instancetype)pk_SHA1_Base64;

@end
14 changes: 13 additions & 1 deletion Source/Categories/NSString+PKAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

@implementation NSString (PKAdditions)

- (id)pk_stringByReplacingNumericHTMLEntities {
//TODO: This only handles decimal HTML entity codes but not hexa &#xDDDD;
// (pavel, Mon 4 Nov 20:39:27 2013)
- (instancetype)pk_stringByReplacingNumericHTMLEntities {
NSString *string = [self copy];

NSRange entityEnd;
Expand All @@ -36,4 +38,14 @@ - (id)pk_stringByReplacingNumericHTMLEntities {
return string;
}

- (instancetype)pk_SHA1_Base64 {
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
NSData *stringBytes = [self dataUsingEncoding:NSUTF8StringEncoding];
if (CC_SHA1([stringBytes bytes], [stringBytes length], digest) != digest) return nil;

NSData *sha = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
return [sha base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

@end

62 changes: 62 additions & 0 deletions Specs/Categories/NSString+PKAdditionsSpec.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// NSString+PKAdditionsSpec.m
// NSString+PKAdditions Spec
//
// Created by Pavel Kunc on 04/11/2013.
//

#import "Kiwi.h"
#import "NSString+PKAdditions.h"

SPEC_BEGIN(NSString_PKAdditionsSpec)

describe(@"pk_stringByReplacingNumericHTMLEntities", ^{

context(@"when given string without any HTML entities", ^{
it(@"should return the unchanged string", ^{
NSString *original = @"Lorem ipsum...";
[[[original pk_stringByReplacingNumericHTMLEntities] should] equal:original];
});
});

context(@"when given empty string", ^{
it(@"should return empty string", ^{
[[[@"" pk_stringByReplacingNumericHTMLEntities] should] equal:@""];
});
});


// http://www.fileformat.info/info/unicode/char/2018/index.htm
// http://www.fileformat.info/info/unicode/char/2019/index.htm
context(@"when given string with HTML entities", ^{
it(@"should return the string with all entities converted to characters", ^{
NSString *original = @"Lorem &#8216;ipsum&#8217;...";
NSString *result = @"Lorem ‘ipsum’...";
[[[original pk_stringByReplacingNumericHTMLEntities] should] equal:result];
});
});

});

describe(@"pk_SHA1_Base64", ^{

context(@"when string is empty", ^{
it(@"should return SHA1 special value for empty string", ^{
// Digest::SHA1.hexdigest('') => "da39a3ee5e6b4b0d3255bfef95601890afd80709"
// Digest::SHA1.base64digest('') => "2jmj7l5rSw0yVb/vlWAYkK/YBwk="
[[[@"" pk_SHA1_Base64] should] equal:@"2jmj7l5rSw0yVb/vlWAYkK/YBwk="];
});
});

context(@"when string is not empty", ^{
it(@"should return Base64 representation of the SHA1 of the string", ^{
NSString *string = @"abcdefghijklmnopqrstuvwxyz";
NSString *hash = @"MtEMe4z5ZXDKBM438qGdhCQNOok=";
[[[string pk_SHA1_Base64] should] equal:hash];
});
});

});

SPEC_END

0 comments on commit 95665e1

Please sign in to comment.