romariolopezc / elm-hmac-sha1 / HmacSha1

Computes a Hash-based Message Authentication Code (HMAC) using the SHA-1 hash function


type Digest

An HMAC-SHA1 digest.

fromString : Internals.Key -> String -> Digest

Pass a Key and your message as a String to compute a Digest

import HmacSha1.Key as Key

"The quick brown fox jumps over the lazy dog"
    |> fromString (Key.fromString "key")
    |> toHex
--> "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"

"The quick brown fox jumps over the lazy dog"
    |> fromString (Key.fromString "key")
    |> toBase64
--> "3nybhbi3iqa8ino29wqQcBydtNk="

fromBytes : Internals.Key -> Bytes -> Digest

Pass a Key and your message in Bytes to compute a Digest

import HmacSha1.Key as Key
import Bytes.Encode

Bytes.Encode.sequence []
    |> Bytes.Encode.encode
    |> fromBytes (Key.fromString "")
    |> toBase64
--> "+9sdGxiqbAgyS31ktx+3Y3BpDh0="

Representation

toBytes : Digest -> Bytes

Convert a Digest into elm/bytes Bytes. You can use this to map it to your own representations. I use it to convert it to Base16 and Base64 string representations.

import Bytes
import HmacSha1.Key as Key

fromString (Key.fromString "key") "message"
    |> toBytes
    |> Bytes.width
--> 20

toByteValues : Digest -> List Basics.Int

Convert a Digest into a List of Integers. Each Integer is in the range 0-255, and represents one byte. Can be useful for passing digest on to other packages that make use of this convention.

import HmacSha1.Key as Key

fromString (Key.fromString "key") "message"
    |> toByteValues
--> [32, 136, 223, 116, 213, 242, 20, 107, 72, 20, 108, 175, 73, 101, 55, 126, 157, 11, 227, 164]

toHex : Digest -> String

Convert a Digest into a base16 String

import HmacSha1.Key as Key

fromString (Key.fromString "key") "message"
    |> toHex
--> "2088df74d5f2146b48146caf4965377e9d0be3a4"

toBase64 : Digest -> String

Convert a Digest into a base64 String

import HmacSha1.Key as Key

fromString (Key.fromString "key") "message"
    |> toBase64
--> "IIjfdNXyFGtIFGyvSWU3fp0L46Q="