HMAC

public final class HMAC

HMAC is a MAC (message authentication code), i.e. a keyed hash function used for message authentication, which is based on a hash function.

let digest = try HMAC.SHA1.authenticate("hello", key: "vapor")
print(digest.hexEncodedString()) // bb2a9aabb537902647f3f40bfecb679bf0d7d64b

Read more about HMAC on Wikipedia.

Read more about OpenSSL’s HMAC methods

  • Creates a new HMAC using the supplied DigestAlgorithm.

    You can use the convenience static variables on HMAC for common algorithms.

    try HMAC.SHA256.authenticate(...)
    

    You can also use this init(algorithm:) method manually to supply custom DigestAlgorithm.

    try HMAC(algorithm: .named("sha256")).authenticate(...)
    

    Declaration

    Swift

    public init(algorithm: DigestAlgorithm)
  • Authenticates the message using the supplied key. This method will first initialize or reset the HMAC context. The supplied message will be digested using one call to update(data:).

    For streaming HMAC authentication, use the reset(key:), update(data:) and finish() methods individually.

    let digest = try HMAC.SHA256.authenticate(hello, key: vapor) print(digest) /// Data

    Throws

    CryptoError if reset, update, or finalization steps fail or data conversion fails.

    Declaration

    Swift

    public func authenticate(_ data: LosslessDataConvertible, key: LosslessDataConvertible) throws -> Data

    Parameters

    data

    Message to digest / authenticate.

    key

    HMAC key

    Return Value

    Digested data

  • Initializes or resets the HMAC context. This method sets this HMAC’s key for subsequent calls to update(data:).

    let hmacsha256 = try HMAC(algorithm: .sha256)
    try hmacsha256.reset(key: "vapor")
    

    Throws

    CryptoError if the initialization / reset fails or data conversion fails.

    Declaration

    Swift

    public func reset(key: LosslessDataConvertible) throws

    Parameters

    key

    HMAC key

  • Updates the HMAC digest with a new chunk of data. This method can be called repeatedly for each new chunk. Use this method for streaming HMAC digests.

    let hmacsha256 = try HMAC(algorithm: .sha256)
    try hmacsha256.reset(key: "vapor")
    try hmacsha256.update(data: "hello")
    try hmacsha256.update(data: "world")
    

    note: You must call .reset(key:) once before streaming data.

    Throws

    CryptoError if the update fails or data conversion fails.

    Declaration

    Swift

    public func update(data: LosslessDataConvertible) throws

    Parameters

    data

    Message chunk to digest / authenticate

  • Completes the HMAC digest. This method should be called once after one call to reset(key:) and one more more calls to update(data:).

    let hmacsha256 = try HMAC(algorithm: .sha256)
    try hmacsha256.reset(key: "vapor")
    try hmacsha256.update(data: "hello")
    try hmacsha256.update(data: "world")
    let digest = try hmacsha256.finish()
    print(digest) // Data
    

    Throws

    CryptoError if the finalization step fails.

    Declaration

    Swift

    public func finish() throws -> Data

    Return Value

    Digest data