Hash different for the same object, Swift, Hashable

前端 未结 1 950
太阳男子
太阳男子 2021-01-15 18:49

Inside of the Hashable we can see:

/// Hash values are not guaranteed to be equal across different executions of
/// your program. Do not save h         


        
相关标签:
1条回答
  • 2021-01-15 19:13

    Hash randomization was enforced in Swift 4.2, with the implementation of SE 0206 Hashable Enhancements. From the proposal:

    However, Hasher may generate entirely different hash values in other executions, even if it is fed the exact same byte sequence. This randomization is a critical feature, as it makes it much harder for potential attackers to predict hash values. Hashable has always been documented to explicitly allow such nondeterminism.

    In addition, it allows the actual implementation to be changed (e.g. improved) in the Swift standard library, without breaking compatibility.

    For debugging purposes the hash randomization can be disabled by defining the SWIFT_DETERMINISTIC_HASHING environment variable with a value of 1.

    The implementation of the Swift standard hasher can be found in the open source repository:

    • https://github.com/apple/swift/blob/master/stdlib/public/core/Hasher.swift
    • https://github.com/apple/swift/blob/master/stdlib/public/core/SipHash.swift

    It is based on SipHash.

    0 讨论(0)
提交回复
热议问题