|
@@ -7,6 +7,14 @@
|
|
|
//
|
|
|
|
|
|
#import "PQBridgeObject.h"
|
|
|
+#import "CommonCrypto/CommonDigest.h"
|
|
|
+#import "CommonCrypto/CommonHMAC.h"
|
|
|
+int32_t const CHUNK_SIZE = 8 * 1024;
|
|
|
+
|
|
|
+#define CC_MD5_DIGEST_LENGTH 16 /* digest length in bytes */
|
|
|
+#define CC_MD5_BLOCK_BYTES 64 /* block size in bytes */
|
|
|
+#define CC_MD5_BLOCK_LONG (CC_MD5_BLOCK_BYTES / sizeof(CC_LONG))
|
|
|
+
|
|
|
|
|
|
@implementation PQBridgeObject
|
|
|
+ (NSString *)getByteRate {
|
|
@@ -53,4 +61,41 @@
|
|
|
return @"0B/s";
|
|
|
};
|
|
|
}
|
|
|
+
|
|
|
++ (NSString*)base64ForData:(uint8_t *)input length:(int32_t)length {
|
|
|
+ if (input == nil) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+ NSData * data = [NSData dataWithBytes:input length:length];
|
|
|
+ return [data base64EncodedStringWithOptions: NSDataBase64Encoding64CharacterLineLength];
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
++ (NSString *)base64Md5ForFilePath:(NSString *)filePath {
|
|
|
+ uint8_t * bytes = (uint8_t *)[[self fileMD5:filePath] bytes];
|
|
|
+ return [self base64ForData:bytes length:CC_MD5_DIGEST_LENGTH];
|
|
|
+}
|
|
|
+
|
|
|
++ (NSData *)fileMD5:(NSString*)path {
|
|
|
+ NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
|
|
|
+ if(handle == nil) {
|
|
|
+ return nil;
|
|
|
+ }
|
|
|
+ CC_MD5_CTX md5;
|
|
|
+ CC_MD5_Init(&md5);
|
|
|
+ BOOL done = NO;
|
|
|
+ while(!done) {
|
|
|
+ @autoreleasepool{
|
|
|
+ NSData* fileData = [handle readDataOfLength: CHUNK_SIZE];
|
|
|
+ CC_MD5_Update(&md5, [fileData bytes], (CC_LONG)[fileData length]);
|
|
|
+ if([fileData length] == 0) {
|
|
|
+ done = YES;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ unsigned char digestResult[CC_MD5_DIGEST_LENGTH * sizeof(unsigned char)];
|
|
|
+ CC_MD5_Final(digestResult, &md5);
|
|
|
+ return [NSData dataWithBytes:(const void *)digestResult length:CC_MD5_DIGEST_LENGTH * sizeof(unsigned char)];
|
|
|
+}
|
|
|
+
|
|
|
@end
|