hyperoslo / Cache

:package: Nothing but Cache.
Other
2.96k stars 335 forks source link

DiskStorage Broken by Changes to Swift Hasher #297

Closed bdhazman closed 5 months ago

bdhazman commented 2 years ago

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.

denandreychuk commented 2 years ago

Did you find alternatives to Cache library?

3lvis commented 5 months ago

Closing, reason outdated issue.