Cache icon indicating copy to clipboard operation
Cache copied to clipboard

DiskStorage Broken by Changes to Swift Hasher

Open bdhazman opened this issue 4 years ago • 1 comments

In Swift 4.2, the language adopted a proposal that, among other things, adds randomness to the output of the hash(into: ) function implemented by Hashable types. This causes issues when using DiskStorage as all keys are hashed before being used as file names.

  func makeFileName(for key: Key) -> String {
    if let key = key as? String {
        let fileExtension = URL(fileURLWithPath: key).pathExtension
        let fileName = MD5(key)

        switch fileExtension.isEmpty {
        case true:
          return fileName
        case false:
          return "\(fileName).\(fileExtension)"
        }
    }

    var hasher = self.hasher
    key.hash(into: &hasher)
    return String(hasher.finalize())
  }

Since hashes of identical objects vary by execution, it no longer is possible to use the hash of an object as a key for fetching objects stored in DiskStorage. Instead of using the built in Hashable functionality, an insecure hashing method such as SHA1 should be used to fix discrepancies between the hashes of identical objects between executions.

bdhazman avatar Oct 07 '21 03:10 bdhazman

Did you find alternatives to Cache library?

denandreychuk avatar Nov 18 '21 14:11 denandreychuk

Closing, reason outdated issue.

3lvis avatar Mar 26 '24 05:03 3lvis