elm-cardano/sha2/1.0.0

SHA224

SHA-224 is a cryptographic hash function that gives 112 bits of security.

It is a truncated variant of SHA-256 with different initial hash values.

type Digest

An abstract sha224 digest.

Creating digests

fromString : String -> Digest

Create a digest from a String.

import SHA224


SHA224.fromString "hello world"
    |> SHA224.toHex
--> "2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b"
fromBytes : Bytes -> Digest

Create a digest from Bytes.

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


Encode.unsignedInt32 Encode.BE 42
    |> Encode.encode
    |> SHA224.fromBytes
    |> SHA224.toHex
--> "793ce43981dc8ea9c80d5518905c629b54dec6c94152e7dbb08a4177"
fromByteValues : List Int -> Digest

Create a digest from a list of byte values (0-255).

import SHA224


SHA224.fromByteValues [ 72, 105, 33, 32, 240, 159, 152, 132 ]
    == SHA224.fromString "Hi! \u{1F604}"
--> True

Formatting digests

toHex : Digest -> String

Turn a digest into a hex string.

import SHA224


SHA224.fromString "And our friends are all aboard"
    |> SHA224.toHex
--> "43baf0c15656c9c0ecce1e4ccb8491e6e5fe01c50e33d73338e899cb"
toBase64 : Digest -> String

Turn a digest into a base64 encoded string.

import SHA224


SHA224.fromString "Many more of them live next door"
    |> SHA224.toBase64
--> "jGqILHEjFHl4RGN0oaRtFhktytsyncZyOHob4g=="

To binary data

toBytes : Digest -> Bytes

Turn a digest into Bytes. The width is 28 bytes or 224 bits.

toByteValues : Digest -> List Int

Turn a digest into a list of byte values (0-255).

import SHA224


SHA224.fromString "And the band begins to play"
    |> SHA224.toByteValues
--> [ 0xac, 0x41, 0xa7, 0x63, 0x89, 0xc4, 0xe1, 0x5a
--> , 0x7e, 0x3b, 0x9d, 0x4a, 0x24, 0x20, 0xef, 0xd0
--> , 0x32, 0x78, 0xd8, 0xfc, 0xcb, 0x23, 0x39, 0xa1
--> , 0xe6, 0xaf, 0xcd, 0x18
--> ]