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. UseHashedPassword
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)
-
This class represents a key that can be used for hashing.
See moreDeclaration
Swift
public class Key : KeyMaterial
-
The minimum size of the hash in bytes.
Declaration
Swift
public static let MinimumSizeInBytes: UInt32
-
The maximum size of the hash in bytes.
Declaration
Swift
public static let MaximumSizeInBytes: UInt32
-
The default size of the hash in bytes.
Declaration
Swift
public static let DefaultSizeInBytes: UInt32
-
Hash an arbitrary value.
The size needs to be within the given bounds:
MinimumSizeInBytes
≤outputSizeInBytes
≤MaximumSizeInBytes
.Warning
Do not use this for hashing passwords, as there is no protection against fast brute-force attacks. Use
HashedPassword
for that purpose.This is not protected against rainbow attacks if you do not provide a key.
Declaration
Swift
public init?(bytes: Bytes, outputSizeInBytes: UInt32 = GenericHash.DefaultSizeInBytes, with key: Key? = nil)
Parameters
bytes
The value that should be hashed.
outputSizeInBytes
The size of the hash in bytes.
key
A key/salt used to prevent the hash from being guessed.
-
Hash an arbitrary value for a given persona.
The size needs to be within the given bounds:
MinimumSizeInBytes
≤outputSizeInBytes
≤MaximumSizeInBytes
.This is protected against rainbow attacks.
Warning
Do not use this for hashing passwords, as there is no protection against fast brute-force attacks. Use
HashedPassword
for that purpose.Declaration
Swift
public convenience init?(bytes: Bytes, for persona: Persona, outputSizeInBytes: UInt32 = GenericHash.DefaultSizeInBytes)
Parameters
bytes
The value that should be hashed.
persona
The persona to which the hash is tied to.
outputSizeInBytes
The size of the hash in bytes.
-
Restore a hash from a hex string.
Declaration
Swift
public init?(hex: String)
Parameters
hex
The hash as a hex encoded string.
-
The size of the hash in bytes.
Declaration
Swift
public var sizeInBytes: UInt32 { get }
-
A hex encoded string representing the hash.
Declaration
Swift
public var hexlify: String { get }
-
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 }