Skip to content

Commit

Permalink
2.0.1 - Performance
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Apr 25, 2019
1 parent b3a7cc7 commit 6eaa78c
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog


#### 2.0.1

Major performance improvements.


#### 2.0.0

The hash functions now take `Binary.Bits` instead of `String`s.
Expand Down
2 changes: 1 addition & 1 deletion docs.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"SHA","comment":" An implementation of several [SHA](https://en.wikipedia.org/wiki/Secure_Hash_Algorithms) cryptographic hash functions. This package uses [icidasset/elm-binary](https://package.elm-lang.org/packages/icidasset/elm-binary/latest/) to work with binary numbers.\n\n@docs sha224, sha256, sha384, sha512\n\n","unions":[],"aliases":[],"values":[{"name":"sha224","comment":" SHA-224.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha224\n ..> |> Binary.toHex\n \"23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7\"\n\n >>> \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha224\n ..> |> Binary.toHex\n \"C97CA9A559850CE97A04A96DEF6D99A9E0E0E2AB14E6B8DF265FC0B3\"\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"sha256","comment":" SHA-256.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha256\n ..> |> Binary.toHex\n \"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD\"\n\n >>> \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha256\n ..> |> Binary.toHex\n \"CF5B16A778AF8380036CE59E7B0492370B249B11E8F07A51AFAC45037AFEE9D1\"\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"sha384","comment":" SHA-384.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha384\n ..> |> Binary.toHex\n \"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7\"\n\n >>> \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha384\n ..> |> Binary.toHex\n \"09330C33F71147E83D192FC782CD1B4753111B173B3B05D22FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039\"\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"sha512","comment":" SHA-512.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha512\n ..> |> Binary.toHex\n \"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F\"\n\n >>> \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha512\n ..> |> Binary.toHex\n \"8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA17299AEADB6889018501D289E4900F7E4331B99DEC4B5433AC7D329EEB6DD26545E96E55B874BE909\"\n\n","type":"Binary.Bits -> Binary.Bits"}],"binops":[]}]
[{"name":"SHA","comment":" An implementation of several [SHA](https://en.wikipedia.org/wiki/Secure_Hash_Algorithms) cryptographic hash functions. This package uses [icidasset/elm-binary](https://package.elm-lang.org/packages/icidasset/elm-binary/latest/) to work with binary numbers.\n\n@docs sha224, sha256, sha384, sha512\n\n","unions":[],"aliases":[],"values":[{"name":"sha224","comment":" SHA-224.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha224\n ..> |> Binary.toHex\n \"23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7\"\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"sha256","comment":" SHA-256.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha256\n ..> |> Binary.toHex\n \"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD\"\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"sha384","comment":" SHA-384.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha384\n ..> |> Binary.toHex\n \"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7\"\n\n","type":"Binary.Bits -> Binary.Bits"},{"name":"sha512","comment":" SHA-512.\n\n >>> import Binary\n\n >>> \"abc\"\n ..> |> Binary.fromStringAsUtf8\n ..> |> SHA.sha512\n ..> |> Binary.toHex\n \"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F\"\n\n","type":"Binary.Bits -> Binary.Bits"}],"binops":[]}]
2 changes: 1 addition & 1 deletion elm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "icidasset/elm-sha",
"summary": "SHA cryptographic hash functions.",
"license": "MIT",
"version": "2.0.0",
"version": "2.0.1",
"exposed-modules": [
"SHA"
],
Expand Down
24 changes: 0 additions & 24 deletions src/SHA.elm
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import SHA.Internal.SHA512
..> |> Binary.toHex
"23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha224
..> |> Binary.toHex
"C97CA9A559850CE97A04A96DEF6D99A9E0E0E2AB14E6B8DF265FC0B3"
-}
sha224 : Bits -> Bits
sha224 message =
Expand All @@ -50,12 +44,6 @@ sha224 message =
..> |> Binary.toHex
"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha256
..> |> Binary.toHex
"CF5B16A778AF8380036CE59E7B0492370B249B11E8F07A51AFAC45037AFEE9D1"
-}
sha256 : Bits -> Bits
sha256 message =
Expand All @@ -75,12 +63,6 @@ sha256 message =
..> |> Binary.toHex
"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha384
..> |> Binary.toHex
"09330C33F71147E83D192FC782CD1B4753111B173B3B05D22FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039"
-}
sha384 : Bits -> Bits
sha384 message =
Expand All @@ -100,12 +82,6 @@ sha384 message =
..> |> Binary.toHex
"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha512
..> |> Binary.toHex
"8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA17299AEADB6889018501D289E4900F7E4331B99DEC4B5433AC7D329EEB6DD26545E96E55B874BE909"
-}
sha512 : Bits -> Bits
sha512 message =
Expand Down
5 changes: 3 additions & 2 deletions src/SHA/Internal/Common.elm
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ combine sizeInBits x y =
width - sizeInBits
in
-- Is the sum larger than the modulo constant (ie. 2 ^ sizeInBits)?
(True :: List.repeat (sizeInBits + excess - 1) False)
sum
|> Binary.toBooleans
|> List.drop excess
|> Binary.fromBooleans
|> Binary.subtract sum
|> Binary.dropLeadingZeros
|> Binary.ensureSize sizeInBits

Expand Down
113 changes: 113 additions & 0 deletions src/SHA/Internal/Tests.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
module SHA.Internal.Tests exposing (sha224, sha256, sha384, sha512)

{-| Tests.
>>> import Binary
-}

import SHA


{-| SHA-224 tests.
>>> "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
..> |> Binary.fromStringAsUtf8
..> |> sha224
..> |> Binary.toHex
"75388B16512776CC5DBA5DA1FD890150B0C6455CB4F58B1952522525"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> sha224
..> |> Binary.toHex
"C97CA9A559850CE97A04A96DEF6D99A9E0E0E2AB14E6B8DF265FC0B3"
>>> "a"
..> |> String.repeat 10000
..> |> Binary.fromStringAsUtf8
..> |> sha224
..> |> Binary.toHex
"00568FBA93E8718C2F7DCD82FA94501D59BB1BBCBA2C7DC2BA5882DB"
-}
sha224 =
SHA.sha224


{-| SHA-256 tests.
>>> "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
..> |> Binary.fromStringAsUtf8
..> |> sha256
..> |> Binary.toHex
"248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> sha256
..> |> Binary.toHex
"CF5B16A778AF8380036CE59E7B0492370B249B11E8F07A51AFAC45037AFEE9D1"
>>> "a"
..> |> String.repeat 10000
..> |> Binary.fromStringAsUtf8
..> |> sha256
..> |> Binary.toHex
"27DD1F61B867B6A0F6E9D8A41C43231DE52107E53AE424DE8F847B821DB4B711"
-}
sha256 =
SHA.sha256


{-| SHA-384 tests.
>>> "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
..> |> Binary.fromStringAsUtf8
..> |> sha384
..> |> Binary.toHex
"3391FDDDFC8DC7393707A65B1B4709397CF8B1D162AF05ABFE8F450DE5F36BC6B0455A8520BC4E6F5FE95B1FE3C8452B"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> sha384
..> |> Binary.toHex
"09330C33F71147E83D192FC782CD1B4753111B173B3B05D22FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039"
>>> "a"
..> |> String.repeat 10000
..> |> Binary.fromStringAsUtf8
..> |> sha384
..> |> Binary.toHex
"2BCA3B131BB7E922BCD1DE98C44786D32E6B6B2993E69C4987EDF9DD49711EB501F0E98AD248D839F6BF9E116E25A97C"
-}
sha384 =
SHA.sha384


{-| SHA-512 tests.
>>> "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
..> |> Binary.fromStringAsUtf8
..> |> sha512
..> |> Binary.toHex
"204A8FC6DDA82F0A0CED7BEB8E08A41657C16EF468B228A8279BE331A703C33596FD15C13B1B07F9AA1D3BEA57789CA031AD85C7A71DD70354EC631238CA3445"
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> sha512
..> |> Binary.toHex
"8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA17299AEADB6889018501D289E4900F7E4331B99DEC4B5433AC7D329EEB6DD26545E96E55B874BE909"
>>> "a"
..> |> String.repeat 10000
..> |> Binary.fromStringAsUtf8
..> |> sha512
..> |> Binary.toHex
"0593036F4F479D2EB8078CA26B1D59321A86BDFCB04CB40043694F1EB0301B8ACD20B936DB3C916EBCC1B609400FFCF3FA8D569D7E39293855668645094BAF0E"
-}
sha512 =
SHA.sha512

0 comments on commit 6eaa78c

Please sign in to comment.