TSFoster/elm-sha1/1.0.0

NOTE: the latest version is 2.1.1

SHA1

SHA-1 is a cryptographic hash function. Although it is no longer considered strong enough for certain high-risk situations, 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 Strings and 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 a List Int to represent the bytes.

Note: Currently, the package can only create digests for around 200kb of data. If there is any interest in using this package for hashing >200kb, or for hashing elm/bytes, let me know!

type Digest

A type to represent a message digest. SHA1.Digests are equatable, and you may want to consider keeping any digests you need in your Model as Digests, not as Strings created by toHex or toBase64.

Creating digests

fromString : String -> Digest

Create a digest from a String.

"hello world" |> SHA1.fromString |> SHA1.toHex
--> "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"

Formatting digests

toHex : Digest -> String

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"
toBase64 : Digest -> String

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="

Binary data

fromBytes : List Int -> Digest

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="
toBytes : Digest -> List Int

If you need the raw digest instead of the textual representation (for example, if using SHA-1 as part of another algorithm), toBytes is what you're looking for!

"And the band begins to play"
    |> SHA1.fromString
    |> SHA1.toBytes
--> [ 0xF3, 0x08, 0x73, 0x13
--> , 0xD6, 0xBC, 0xE5, 0x5B
--> , 0x60, 0x0C, 0x69, 0x2F
--> , 0xE0, 0x92, 0xF4, 0x53
--> , 0x87, 0x3F, 0xAE, 0x91
--> ]