如何使用 swift 3 对 iOS 上的文件进行哈希处理?

How can I hash a file on iOS using swift 3?

我有许多文件将保存在服务器上。用户可以在设备上创建这些类型的文件 (plist),然后将其上传到所述服务器 (CloudKit)。我想通过内容来区分它们(独特的方法应该能够适应创建日期的变化)。我的理解是我应该对这些文件进行哈希处理以获得它们的唯一文件名。我的问题是:

  1. 我的理解是否正确,我想要的是哈希函数?
  2. 我应该使用哪个函数(来自 CommonCrypto)。
  3. 我需要的是摘要?
  4. 我将如何在代码中处理它? (我假设这应该在 NSData 实例上散列?)。通过谷歌搜索我的理解是我需要一个桥接头包含但除此之外使用 CommonCrypto 让我感到困惑。如果有更简单的方法使用第一方 API(Apple),我会洗耳恭听(我想尽可能避免使用第三方代码)。

非常感谢!

为每个文件创建一个加密散列,您可以将其用于唯一性比较。 SHA-256 是当前的哈希函数,在 iOS 上使用 Common Crypto 非常快,在 iPhone 6S 上 SHA256 将处理大约 1GB/秒减去 I/O 时间。如果您需要更少的字节,只需截断哈希即可。

使用 Common Crypto (Swift3) 的示例

对于字符串的散列:

func sha256(string: String) -> Data {
    let messageData = string.data(using:String.Encoding.utf8)!
    var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))

    _ = digestData.withUnsafeMutableBytes {digestBytes in
        messageData.withUnsafeBytes {messageBytes in
            CC_SHA256(messageBytes, CC_LONG(messageData.count), digestBytes)
        }
    }
    return digestData
}
let testString = "testString"
let testHash = sha256(string:testString)
print("testHash: \(testHash.map { String(format: "%02hhx", [=10=]) }.joined())")

let testHashBase64 = testHash.base64EncodedString()
print("testHashBase64: \(testHashBase64)")

Output:
testHash: 4acf0b39d9c4766709a3689f553ac01ab550545ffa4544dfc0b2cea82fba02a3
testHashBase64: Ss8LOdnEdmcJo2ifVTrAGrVQVF/6RUTfwLLOqC+6AqM=

注意:添加到您的桥接头:

#import <CommonCrypto/CommonCrypto.h>

对于散列数据:

func sha256(data: Data) -> Data {
    var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))

    _ = digestData.withUnsafeMutableBytes {digestBytes in
        data.withUnsafeBytes {messageBytes in
            CC_SHA256(messageBytes, CC_LONG(data.count), digestBytes)
        }
    }
    return digestData
}

let testData: Data = "testString".data(using: .utf8)!
print("testData: \(testData.map { String(format: "%02hhx", [=12=]) }.joined())")
let testHash = sha256(data:testData)
print("testHash: \(testHash.map { String(format: "%02hhx", [=12=]) }.joined())")

Output:
testData: 74657374537472696e67
testHash: 4acf0b39d9c4766709a3689f553ac01ab550545ffa4544dfc0b2cea82fba02a3

另见马丁的 link。

也适用于大文件的解决方案,因为它不需要整个文件都在内存中:

func sha256(url: URL) -> Data? {
    do {
        let bufferSize = 1024 * 1024
        // Open file for reading:
        let file = try FileHandle(forReadingFrom: url)
        defer {
            file.closeFile()
        }

        // Create and initialize SHA256 context:
        var context = CC_SHA256_CTX()
        CC_SHA256_Init(&context)

        // Read up to `bufferSize` bytes, until EOF is reached, and update SHA256 context:
        while autoreleasepool(invoking: {
            // Read up to `bufferSize` bytes
            let data = file.readData(ofLength: bufferSize)
            if data.count > 0 {
                data.withUnsafeBytes {
                    _ = CC_SHA256_Update(&context, [=10=], numericCast(data.count))
                }
                // Continue
                return true
            } else {
                // End of file
                return false
            }
        }) { }

        // Compute the SHA256 digest:
        var digest = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
        digest.withUnsafeMutableBytes {
            _ = CC_SHA256_Final([=10=], &context)
        }

        return digest
    } catch {
        print(error)
        return nil
    }
}

先前创建的名称为 fileURL 的类型 URL 实例的用法:

if let digestData = sha256(url: fileURL) {
    let calculatedHash = digestData.map { String(format: "%02hhx", [=11=]) }.joined()
    DDLogDebug(calculatedHash)
}

截至Swift5,仍然基本正确,但对withUnsafeBytes/withUnsafeMutableBytes进行了一些更新。这些更新使这些方法更加类型安全,但使用起来也更加烦人。

对于使用 withUnsafeBytes 的位,使用:

_ = data.withUnsafeBytes { bytesFromBuffer -> Int32 in
  guard let rawBytes = bytesFromBuffer.bindMemory(to: UInt8.self).baseAddress else {
    return Int32(kCCMemoryFailure)
  }

  return CC_SHA256_Update(&context, rawBytes, numericCast(data.count))
}

对于生成最终摘要数据的位,使用:

var digestData = Data(count: Int(CC_SHA256_DIGEST_LENGTH))
_ = digestData.withUnsafeMutableBytes { bytesFromDigest -> Int32 in
  guard let rawBytes = bytesFromDigest.bindMemory(to: UInt8.self).baseAddress else {
    return Int32(kCCMemoryFailure)
  }

  return CC_SHA256_Final(rawBytes, &context)
}

使用 Apple 的 CryptoKit 的更新: 您可以使用 FileHandle 以块的形式读取数据,并将这些数据传递到哈希器中:

import CryptoKit

func getSHA256(forFile url: URL) throws -> SHA256.Digest {
    let handle = try FileHandle(forReadingFrom: url)
    var hasher = SHA256()
    while autoreleasepool(invoking: {
        let nextChunk = handle.readData(ofLength: SHA256.blockByteCount)
        guard !nextChunk.isEmpty else { return false }
        hasher.update(data: nextChunk)
        return true
    }) { }
    let digest = hasher.finalize()
    return digest

    // Here's how to convert to string form
    //return digest.map { String(format: "%02hhx", [=10=]) }.joined()
}