GenericHash

public class GenericHash

This class can be used to generate hash arbitrary data. Keyed hashing is supported.

Warning

Do not use this for hashing passwords, as there is no protection against fast brute-force attacks. Use HashedPassword for that purpose.

Examples

Public Hashing

let data = "Hello, World!".utf8Bytes
let hash = GenericHash(bytes: data)

Private Hashing with Persisted Keys

// Create a persona
let alice = Persona(uniqueName: "Alice")

// Generate a personalized hash for that persona
let data = "Hello, World!".utf8Bytes
let hash = GenericHash(bytes: data, for: alice)

// Forget the persona and remove all related Keychain entries
try! Persona.forget(alice)
  • Compares two hashes in constant time.

    Note

    An attacker might be able to identify the length of the hash with a timing attack. But as the size bounds for hashes are publicly known and the minimum size is sufficiently long, this should not be a cause for problems.

    Declaration

    Swift

    public static func == (lhs: GenericHash, rhs: GenericHash) -> Bool

    Parameters

    lhs

    A hash.

    rhs

    Another hash.

    Return Value

    true if both hashes are equal, false else.

  • The hash value according to the Hashable protocol.

    Warning

    This is not the value of the generic hash but a value used for improving performance of data structures.

    Declaration

    Swift

    public var hashValue: Int { get }