folkertdev / elm-sha2 / SHA384

SHA-384 is a cryptographic hash function that gives 192 bits of security.


type Digest

Abstract representation of a sha384 digest.

Creating digests

fromString : String -> Digest

Create a digest from a String.

"hello world"
    |> SHA384.fromString
    |> SHA384.toHex
--> "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd"

fromBytes : Bytes -> Digest

Create a digest from a Bytes

import Bytes.Encode as Encode
import Bytes exposing (Bytes, Endianness(..))

buffer : Bytes
buffer = Encode.encode (Encode.unsignedInt32 BE 42)

SHA384.fromBytes buffer
    |> SHA384.toHex
    --> "169c6e0f2a73b8a3f0c6dad952ab62ee64136652d1bfcf5901951186384324070819bba50666c9371265b68b7a57410d"

fromByteValues : List Basics.Int -> Digest

Create a digest from integer byte values. Values are considered mod 256, which means that larger than 255 overflow.

SHA384.fromByteValues
    [72, 105, 33, 32, 240, 159, 152, 132]
--> SHA384.fromString "Hi! 😄"

[0x00, 0xFF, 0x34, 0xA5]
    |> SHA384.fromByteValues
    |> SHA384.toBase64
--> "6uus8pWKLDBg2APcRPhqSZrfwu+Y71cgQjcEKu+k80yR4H8s6NimoiR00HKMGSW9"

Formatting digests

toHex : Digest -> String

Represent the digest as a hexadecimal string.

"And our friends are all aboard"
    |> SHA384.fromString
    |> SHA384.toHex
--> "8b955c0b596df8b93db7c9a0105098c5be18bd4dbbea4cccf9b4b138c54668d0c9295485dc3b20a1ecd1bf97762f3b47"

toBase64 : Digest -> String

Represent a digest as its base-64 encoding.

"Many more of them live next door"
    |> SHA384.fromString
    |> SHA384.toBase64
--> "pKq5Z/Msjg14oJ2TGHS21h+L9lMWkASENRmCgur5mpwRNoE3dAPWV6kw+aNX1gmB"

To binary data

toBytes : Digest -> Bytes

Turn a digest into Bytes.

The digest is stored as 7 big-endian 64-bit unsigned integers, so the width is 48 bytes or 384 bits.

toByteValues : Digest -> List Basics.Int

Get the individual byte values as integers.

"And the band begins to play"
    |> SHA384.fromString
    |> SHA384.toByteValues
--> [216,234,215,67,129,199,177,6,4,113,130,141,149,211,213,72,182,77,43,191,48,162,210,207,88,239,69,109,211,248,187,238,97,27,125,162,116,132,44,35,116,33,51,81,115,241,201,137]