elm-cardano/sha2/1.0.0

SHA256

SHA-256 is a cryptographic hash function that gives 128 bits of security.

type Digest

An abstract sha256 digest.

Creating digests

fromString : String -> Digest

Create a digest from a String.

import SHA256


SHA256.fromString "hello world"
    |> SHA256.toHex
--> "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
fromBytes : Bytes -> Digest

Create a digest from Bytes.

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


Encode.unsignedInt32 Encode.BE 42
    |> Encode.encode
    |> SHA256.fromBytes
    |> SHA256.toHex
--> "ae3c8b8d99a39542f78af83dbbb42c81cd94199ec1b5f60a0801063e95842570"
fromByteValues : List Int -> Digest

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

import SHA256


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

Formatting digests

toHex : Digest -> String

Turn a digest into a hex string.

import SHA256


SHA256.fromString "And our friends are all aboard"
    |> SHA256.toHex
--> "a40bc1de58430a446e4b446a722fdfd493375c93bf93b1066793909f717da796"
toBase64 : Digest -> String

Turn a digest into a base64 encoded string.

import SHA256


SHA256.fromString "Many more of them live next door"
    |> SHA256.toBase64
--> "1ov4iAbzsCXuC2R9heu+y57YF/Seb5Vu8cRvVyEY6jM="

To binary data

toBytes : Digest -> Bytes

Turn a digest into Bytes. The width is 32 bytes or 256 bits.

toByteValues : Digest -> List Int

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

import SHA256


SHA256.fromString "And the band begins to play"
    |> SHA256.toByteValues
--> [ 0xb1, 0x13, 0x61, 0x72, 0xce, 0xf9, 0x6d, 0xe6
--> , 0xf0, 0x61, 0x58, 0xd1, 0x43, 0x34, 0x32, 0xaa
--> , 0xaf, 0xe7, 0x68, 0x0f, 0xd3, 0xb4, 0x6f, 0x55
--> , 0x92, 0xcd, 0xed, 0xb3, 0x3a, 0xf5, 0x7a, 0x50
--> ]