folkertdev / elm-sha2 / SHA224

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


type Digest

Abstract representation of a sha224 digest.

Creating digests

fromString : String -> Digest

Create a digest from a String.

"hello world"
    |> SHA224.fromString
    |> SHA224.toHex
--> "2f05477fc24bb4faefd86517156dafdecec45b8ad3cf2522a563582b"

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)

SHA224.fromBytes buffer
    |> SHA224.toHex
    --> "793ce43981dc8ea9c80d5518905c629b54dec6c94152e7dbb08a4177"

fromByteValues : List Basics.Int -> Digest

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

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

[0x00, 0xFF, 0x34, 0xA5]
    |> SHA224.fromByteValues
    |> SHA224.toBase64
--> "XNMoqDRg7RdO+hXuACw+BWAVTd8aTNqXyjz35w=="

Formatting digests

toHex : Digest -> String

Represent the digest as a string of hexadecimal digits.

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

toBase64 : Digest -> String

Represent the digest as its base-64 encoding.

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

To binary data

toBytes : Digest -> Bytes

Turn a digest into Bytes. The digest is stored as 7 big-endian 32-bit unsigned integers, so the width is 28 bytes or 224 bits.

toByteValues : Digest -> List Basics.Int

Get the individual byte values as integers.

"And the band begins to play"
    |> SHA224.fromString
    |> 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
--> ]