SHA-256 is a cryptographic hash function that gives 128 bits of security.
Internal.Digest
An abstract sha256 digest
fromString : String -> Digest
Create a digest from a String
.
"hello world"
|> SHA256.fromString
|> SHA256.toHex
--> "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
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)
SHA256.fromBytes buffer
|> SHA256.toHex
--> "ae3c8b8d99a39542f78af83dbbb42c81cd94199ec1b5f60a0801063e95842570"
fromByteValues : List Basics.Int -> Digest
Create a digest from integer byte values. Values are considered mod 256, which means that larger than 255 overflow.
SHA256.fromByteValues
[72, 105, 33, 32, 240, 159, 152, 132]
--> SHA256.fromString "Hi! 😄"
[0x00, 0xFF, 0x34, 0xA5]
|> SHA256.fromByteValues
|> SHA256.toBase64
--> "SQ/kWBD948Wwg8toEA/O0cPo+oodOMYnZc8HmexxyCs="
toHex : Digest -> String
Represent the digest as a string of hexadecimal digits.
"And our friends are all aboard"
|> SHA256.fromString
|> SHA256.toHex
--> "a40bc1de58430a446e4b446a722fdfd493375c93bf93b1066793909f717da796"
toBase64 : Digest -> String
Represent the digest as its base-64 encoding.
"Many more of them live next door"
|> SHA256.fromString
|> SHA256.toBase64
--> "1ov4iAbzsCXuC2R9heu+y57YF/Seb5Vu8cRvVyEY6jM="
toBytes : Digest -> Bytes
Turn a digest into Bytes
. The digest is stored as 8 big-endian 32-bit unsigned integers, so the width is 32 bytes or 256 bits.
toByteValues : Digest -> List Basics.Int
Get the individual byte values as integers
"And the band begins to play"
|> SHA256.fromString
|> SHA256.toByteValues
--> [ 0xb1, 0x13, 0x61, 0x72, 0xce, 0xf9, 0x6d, 0xe6, 0xf0, 0x61, 0x58, 0xd1, 0x43, 0x34, 0x32, 0xaa, 0xaf, 0xe7, 0x68, 0xf, 0xd3, 0xb4, 0x6f, 0x55, 0x92, 0xcd, 0xed, 0xb3, 0x3a, 0xf5, 0x7a, 0x50 ]