zoukankan      html  css  js  c++  java
  • MD5散列

    #define FileHashDefaultChunkSizeForReadingData 1024*8
    +(NSString*)md5WithFile:(NSString*)path
    {
        return (__bridge_transfer NSString *)FileMD5HashCreateWithPath((__bridge CFStringRef)path, FileHashDefaultChunkSizeForReadingData);
    }
    
    CFStringRef FileMD5HashCreateWithPath(CFStringRef filePath,size_t chunkSizeForReadingData) {
        // Declare needed variables
        CFStringRef result = NULL;
        CFReadStreamRef readStream = NULL;
        // Get the file URL
        CFURLRef fileURL =
        CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
                                      (CFStringRef)filePath,
                                      kCFURLPOSIXPathStyle,
                                      (Boolean)false);
        if (!fileURL) goto done;
        // Create and open the read stream
        readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,
                                                (CFURLRef)fileURL);
        if (!readStream) goto done;
        bool didSucceed = (bool)CFReadStreamOpen(readStream);
        if (!didSucceed) goto done;
        // Initialize the hash object
        CC_MD5_CTX hashObject;
        CC_MD5_Init(&hashObject);
        // Make sure chunkSizeForReadingData is valid
        if (!chunkSizeForReadingData) {
            chunkSizeForReadingData = FileHashDefaultChunkSizeForReadingData;
        }
        // Feed the data to the hash object
        bool hasMoreData = true;
        while (hasMoreData) {
            uint8_t buffer[chunkSizeForReadingData];
            CFIndex readBytesCount = CFReadStreamRead(readStream,(UInt8 *)buffer,(CFIndex)sizeof(buffer));
            if (readBytesCount == -1) break;
            if (readBytesCount == 0) {
                hasMoreData = false;
                continue;
            }
            CC_MD5_Update(&hashObject,(const void *)buffer,(CC_LONG)readBytesCount);
        }
        // Check if the read operation succeeded
        didSucceed = !hasMoreData;
        // Compute the hash digest
        unsigned char digest[CC_MD5_DIGEST_LENGTH];
        CC_MD5_Final(digest, &hashObject);
        // Abort if the read operation failed
        if (!didSucceed) goto done;
        // Compute the string result
        char hash[2 * sizeof(digest) + 1];
        for (size_t i = 0; i < sizeof(digest); ++i) {
            snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i]));
        }
        result = CFStringCreateWithCString(kCFAllocatorDefault,(const char *)hash,kCFStringEncodingUTF8);
        
    done:
        if (readStream) {
            CFReadStreamClose(readStream);
            CFRelease(readStream);
        }
        if (fileURL) {
            CFRelease(fileURL);
        }
        return result;
    }
    

      

  • 相关阅读:
    C#多线程中lock的用法
    杭州交通违规处理地点大全
    J2me c/s结构,访问数据库
    谁将成为下一个Twitter?最新最酷的创业奇思妙想
    那些相见恨晚的 JavaScript 技巧
    Adobe Dreamweaver CS5试用点滴感受
    python百炼成钢实例008输出 9*9 乘法口诀表
    python百炼成钢实例006斐波那契数列输出最后一个和整个数列,迭代
    python百炼成钢实例007复制,浅拷贝和深拷贝
    python百炼成钢实例080猴子分桃
  • 原文地址:https://www.cnblogs.com/buakaw/p/6839176.html
Copyright © 2011-2022 走看看