5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
15 // 2. Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials
18 // provided with the distribution.
20 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
33 // The views and conclusions contained in the software and
34 // documentation are those of the authors and should not be
35 // interpreted as representing official policies, either expressed
36 // or implied, of GRNET S.A.
38 #import "HashMapHash.h"
39 #import <CommonCrypto/CommonHMAC.h>
41 @implementation HashMapHash
43 + (NSString *)calculateHashMapHash:(NSArray *)hashes {
44 const char padding[] = {
45 '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0',
46 '\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'};
48 if ([hashes count] == 0)
49 return [HashMapHash formatHash:[HashMapHash calculateHash:[NSData dataWithBytes:padding length:0]]];
50 if ([hashes count] == 1)
51 return [HashMapHash formatHash:[hashes objectAtIndex:0]];
54 while (s < [hashes count])
57 NSMutableArray *h = [NSMutableArray array];
58 for (int i = 0; i < [hashes count]; i++)
59 [h addObject:[hashes objectAtIndex:i]];
60 for (int i = 0; i < s - [hashes count]; i++)
61 [h addObject:[NSMutableData dataWithBytes:padding length:32]];
63 while ([h count] > 1) {
64 for (int i = 0; i < [h count]; i = i + 2) {
65 NSMutableData *bytesData = [h objectAtIndex:i];
66 [bytesData appendData:[h objectAtIndex:i + 1]];
67 [h replaceObjectAtIndex:i withObject:[HashMapHash calculateHash:bytesData]];
70 NSMutableIndexSet *indicesToRemove = [NSMutableIndexSet indexSet];
71 for (int i = 0; i < [h count]; i = i + 2)
72 [indicesToRemove addIndex:i + 1];
74 [h removeObjectsAtIndexes:indicesToRemove];
76 return [HashMapHash formatHash:[h objectAtIndex:0]];
79 + (NSData *)calculateHash:(NSData *)data {
80 unsigned char hash[CC_SHA256_DIGEST_LENGTH];
81 CC_SHA256([data bytes], (unsigned int)[data length], hash);
82 return [NSMutableData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
85 + (NSString *)formatHash:(NSData *)data {
87 hashString = [NSString stringWithFormat:@"%@", data];
88 hashString = [hashString stringByReplacingOccurrencesOfString:@" " withString:@""];
89 hashString = [hashString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
93 // XXX hardcoded hash algorithm for now
94 + (NSArray *)calculateObjectHashMap:(NSString *)filePath withBlockHash:(NSString *)blockHash andBlockSize:(NSUInteger)blockSize {
95 NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:filePath];
97 NSLog(@"failed to open file: %@", filePath);
101 NSFileManager *fm = [NSFileManager defaultManager];
102 NSDictionary *attributes = [fm attributesOfItemAtPath:filePath error:nil];
103 NSUInteger endOfFile = [attributes fileSize];
105 NSUInteger fileOffset = 0;
106 [fh seekToFileOffset:fileOffset];
108 NSMutableArray *hashes = [[[NSMutableArray alloc] init] autorelease];
110 while(fileOffset < endOfFile){
111 NSData *data = [fh readDataOfLength:blockSize];
113 // Strip trailing slashes.
114 const char *bytes = [data bytes];
116 for (i = [data length] - 1; i >= 0; i--)
117 if (bytes[i] != '\0')
119 if (i != [data length] - 1) {
121 data = [data subdataWithRange:NSMakeRange(0, i + 1)];
123 data = [NSData data];
126 unsigned char hash[CC_SHA256_DIGEST_LENGTH];
128 if ( CC_SHA256([data bytes], (unsigned int)[data length], hash) ) {
129 NSData *sha256 = [NSMutableData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
130 [hashes addObject:sha256];
133 fileOffset += blockSize;
134 [fh seekToFileOffset:fileOffset];
139 NSArray *hashMap = [NSArray arrayWithArray:hashes];
144 + (NSArray *)objectHashMapStrings:(NSString *)filePath withBlockHash:(NSString *)blockHash andBlockSize:(NSUInteger)blockSize {
145 NSArray *hashHexes = [HashMapHash calculateObjectHashMap:filePath withBlockHash:blockHash andBlockSize:blockSize];
146 if (hashHexes == nil)
149 NSMutableArray *hashStrings = [[[NSMutableArray alloc] init] autorelease];
151 for (NSData *hashHex in hashHexes) {
152 [hashStrings addObject:[HashMapHash formatHash:hashHex]];
155 NSArray *hashMap = [NSArray arrayWithArray:hashStrings];