SHA-1 is a cryptographic hash function. Although it is no longer considered cryptographically secure (as collisions can be found faster than brute force), it is still very suitable for a broad range of uses, and is a lot stronger than MD5.
This package provides a way of creating SHA-1 digests from Bytes, Strings,
List Ints (where each Int is between 0 and 255, and represents a byte).
It can also take those Digests and format them in hexadecimal or base64
notation. Alternatively, you can get the binary digest, using either Bytes or
List Int to represent the bytes.
Create a digest from a String.
"hello world" |> SHA1.fromString |> SHA1.toHex
--> "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"One of the two canonical ways of representing a SHA-1 digest is with 40 hexadecimal digits.
"And our friends are all aboard"
|> SHA1.fromString
|> SHA1.toHex
--> "f9a0c23ddcd40f6956b0cf59cd9b8800d71de73d"One of the two canonical ways of representing a SHA-1 digest is in a 20 digit long Base64 binary to ASCII text encoding.
"Many more of them live next door"
|> SHA1.fromString
|> SHA1.toBase64
--> "jfL0oVb5xakab6BMLplGe2XPbj8="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)
SHA1.fromElmBytes buffer
|> SHA1.toHex
--> "25f0c736f1fad0770bbb9a265ded159517c1e68c"Turn a digest into Bytes.
The digest is stored as 5 big-endian 32-bit unsigned integers, so the width is 20 bytes or 160 bits.
Sometimes you have binary data that's not representable in a string. Create
a digest from the raw "bytes", i.e. a List of Ints. Any items not between 0
and 255 are discarded.
SHA1.fromBytes [72, 105, 33, 32, 240, 159, 152, 132]
--> SHA1.fromString "Hi! 😄"
[0x00, 0xFF, 0x34, 0xA5] |> SHA1.fromBytes |> SHA1.toBase64
--> "sVQuFckyE6K3fsdLmLHmq8+J738="
A type to represent a message digest.
SHA1.Digests are equatable, and you may want to consider keeping any digests you need in yourModelasDigests, not asStrings created bytoHexortoBase64.