Browse Source

修改取 MD5 方法

jsonwang 3 years ago
parent
commit
00f0ec0f9f

+ 3 - 0
BFCommonKit/Classes/BFUtility/PQBridgeObject.h

@@ -17,6 +17,9 @@ NS_ASSUME_NONNULL_BEGIN
 + (NSString *)getByteRate;
 + (NSString *)getByteRate;
 + (long long) getInterfaceBytes;
 + (long long) getInterfaceBytes;
 + (NSString *)formatNetWork:(long long int)rate;
 + (NSString *)formatNetWork:(long long int)rate;
+
+
++ (NSString *)base64Md5ForFilePath:(NSString *)filePath;
 @end
 @end
 
 
 NS_ASSUME_NONNULL_END
 NS_ASSUME_NONNULL_END

+ 45 - 0
BFCommonKit/Classes/BFUtility/PQBridgeObject.m

@@ -7,6 +7,14 @@
 //
 //
 
 
 #import "PQBridgeObject.h"
 #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
 @implementation PQBridgeObject
 + (NSString *)getByteRate {
 + (NSString *)getByteRate {
@@ -53,4 +61,41 @@
         return @"0B/s";
         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
 @end

+ 2 - 2
BFCommonKit/Classes/BFUtility/PQCommonMethodUtil.swift

@@ -818,8 +818,8 @@ public func contentMD5(path: String? = nil, data _: Data? = nil) -> String? {
         BFLog(message: "生成内容md5值:文件大小为0\(size)")
         BFLog(message: "生成内容md5值:文件大小为0\(size)")
         return ""
         return ""
     }
     }
-//    let hash: String = OSSUtil.base64Md5(forFilePath: path)
-    let hash: String = ""
+    
+    let hash: String = PQBridgeObject.base64Md5(forFilePath: path ?? "")
     BFLog(message: "生成内容md5值:contentMD5 = \(hash)")
     BFLog(message: "生成内容md5值:contentMD5 = \(hash)")
     return hash
     return hash
 }
 }