Skip to content

Commit

Permalink
Merge pull request #7 from folkertdev/structured-optimizations
Browse files Browse the repository at this point in the history
elm/bytes support
  • Loading branch information
TSFoster authored Oct 28, 2019
2 parents 9ad53df + 98dfee1 commit b748d3c
Show file tree
Hide file tree
Showing 4 changed files with 557 additions and 218 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@

Calculate SHA-1 digests in Elm.

This package can take a message as a `String` or `List Int` ("bytes") calculate
SHA-1 digests, and represent them in [hexadecimal], [base64] or a `List Int` (as
"bytes").
This package supports hashing of:

* `String` using the utf-8 encoding
* `List Int` where the elements are assumed to be below 256
* `Bytes` an `elm/bytes` sequence of byte values

And can represent the digest as

* a [hexadecimal] string
* a [base64] string
* a `List Int` of byte values
* a `Bytes`

[hexadecimal]: https://en.wikipedia.org/wiki/Hexadecimal
[base64]: https://en.wikipedia.org/wiki/Base64
Expand All @@ -28,26 +37,40 @@ SHA-1 digests, and represent them in [hexadecimal], [base64] or a `List Int` (as
## Examples

```elm
import Bytes.Encode as Encode
import Bytes exposing (Bytes)
import SHA1

digest1 : SHA1.Digest
digest1 = SHA1.fromString "string"

byteValues : List Int
byteValues = [0x00, 0xFF, 0xCE, 0x35, 0x74]

digest2 : SHA1.Digest
digest2 = SHA1.fromBytes [0x00, 0xFF, 0xCE, 0x35, 0x74]
digest2 = SHA1.fromByteValues byteValues

buffer : Bytes
buffer =
List.map Encode.unsignedInt8 byteValues
|> Encode.sequence
|> Encode.encode

digest3 : SHA1.Digest
digest3 = SHA1.fromBytes buffer

SHA1.toHex digest1
--> "ecb252044b5ea0f679ee78ec1a12904739e2904d"

SHA1.toBase64 digest2
--> "gHweOF5Lyg+Ha7ujrlYwNa/Hwgk="

SHA1.toBytes digest1
--> [ 0xEC, 0xB2, 0x52, 0x04, 0x4B
--> , 0x5E, 0xA0, 0xF6, 0x79, 0xEE
--> , 0x78, 0xEC, 0x1A, 0x12, 0x90
--> , 0x47, 0x39, 0xE2, 0x90, 0x4D
SHA1.toByteValues digest3
--> [ 0x80, 0x7C, 0x1E, 0x38
--> , 0x5E, 0x4B, 0xCA, 0x0F
--> , 0x87, 0x6B, 0xBB, 0xA3
--> , 0xAE, 0x56, 0x30, 0x35
--> , 0xAF, 0xC7, 0xC2, 0x09
--> ]
```

Expand Down
38 changes: 20 additions & 18 deletions elm.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"type": "package",
"name": "TSFoster/elm-sha1",
"summary": "Generate SHA1 digests of strings or arbitrary data",
"license": "BSD-3-Clause",
"version": "1.0.4",
"exposed-modules": ["SHA1"],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"elm/core": "1.0.0 <= v < 2.0.0",
"elm-community/list-extra": "8.0.0 <= v < 9.0.0",
"rtfeldman/elm-hex": "1.0.0 <= v < 2.0.0",
"zwilias/elm-utf-tools": "2.0.1 <= v < 3.0.0"
},
"test-dependencies": {
"elm/regex": "1.0.0 <= v < 2.0.0",
"elm-explorations/test": "1.2.2 <= v < 2.0.0"
}
}
"type": "package",
"name": "TSFoster/elm-sha1",
"summary": "Generate SHA1 digests of strings or arbitrary data",
"license": "BSD-3-Clause",
"version": "1.0.4",
"exposed-modules": [
"SHA1"
],
"elm-version": "0.19.0 <= v < 0.20.0",
"dependencies": {
"danfishgold/base64-bytes": "1.0.2 <= v < 2.0.0",
"elm/bytes": "1.0.8 <= v < 2.0.0",
"elm/core": "1.0.2 <= v < 2.0.0",
"rtfeldman/elm-hex": "1.0.0 <= v < 2.0.0"
},
"test-dependencies": {
"elm/regex": "1.0.0 <= v < 2.0.0",
"elm-explorations/test": "1.2.2 <= v < 2.0.0"
}
}
Loading

0 comments on commit b748d3c

Please sign in to comment.