Skip to content

Commit

Permalink
2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Jan 5, 2019
1 parent f76bece commit b3a7cc7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 48 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Changelog


#### 2.0.0

The hash functions now take `Binary.Bits` instead of `String`s.
The reason for this is that the strings were always converted
to UTF-8 by this package. But there might be cases where we
don't want UTF-8 strings as the input (eg. HMAC).


#### 1.0.2

Upgrade `elm-binary`.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SHA

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/), which can easily be converted to something else.
This package uses [icidasset/elm-binary](https://package.elm-lang.org/packages/icidasset/elm-binary/latest/) to work with binary numbers.

Currently implements:
- SHA224
Expand All @@ -14,6 +14,7 @@ import Binary
import SHA

>>> "abc"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha256
..> |> Binary.toHex
"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"
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/), which can easily be converted to something else.\n\n@docs sha224, sha256, sha384, sha512\n\n","unions":[],"aliases":[],"values":[{"name":"sha224","comment":" SHA-224.\n\n >>> import Binary\n\n\n >>> Binary.toHex (sha224 \"\")\n \"D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F\"\n\n >>> Binary.toHex (sha224 \"abc\")\n \"23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7\"\n\n >>> Binary.toHex (sha224 \"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\")\n \"75388B16512776CC5DBA5DA1FD890150B0C6455CB4F58B1952522525\"\n\n >>> Binary.toHex (sha224 \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\")\n \"C97CA9A559850CE97A04A96DEF6D99A9E0E0E2AB14E6B8DF265FC0B3\"\n\n","type":"String.String -> Binary.Bits"},{"name":"sha256","comment":" SHA-256.\n\n >>> import Binary\n\n\n >>> Binary.toHex (sha256 \"\")\n \"E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855\"\n\n >>> Binary.toHex (sha256 \"abc\")\n \"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD\"\n\n >>> Binary.toHex (sha256 \"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\")\n \"248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1\"\n\n >>> Binary.toHex (sha256 \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\")\n \"CF5B16A778AF8380036CE59E7B0492370B249B11E8F07A51AFAC45037AFEE9D1\"\n\n","type":"String.String -> Binary.Bits"},{"name":"sha384","comment":" SHA-384.\n\n >>> import Binary\n\n >>> Binary.toHex (sha384 \"\")\n \"38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95B\"\n\n >>> Binary.toHex (sha384 \"abc\")\n \"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7\"\n\n >>> Binary.toHex (sha384 \"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\")\n \"3391FDDDFC8DC7393707A65B1B4709397CF8B1D162AF05ABFE8F450DE5F36BC6B0455A8520BC4E6F5FE95B1FE3C8452B\"\n\n >>> Binary.toHex (sha384 \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\")\n \"09330C33F71147E83D192FC782CD1B4753111B173B3B05D22FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039\"\n\n","type":"String.String -> Binary.Bits"},{"name":"sha512","comment":" SHA-512.\n\n >>> import Binary\n\n >>> Binary.toHex (sha512 \"\")\n \"CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E\"\n\n >>> Binary.toHex (sha512 \"abc\")\n \"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F\"\n\n >>> Binary.toHex (sha512 \"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\")\n \"204A8FC6DDA82F0A0CED7BEB8E08A41657C16EF468B228A8279BE331A703C33596FD15C13B1B07F9AA1D3BEA57789CA031AD85C7A71DD70354EC631238CA3445\"\n\n >>> Binary.toHex (sha512 \"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu\")\n \"8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA17299AEADB6889018501D289E4900F7E4331B99DEC4B5433AC7D329EEB6DD26545E96E55B874BE909\"\n\n","type":"String.String -> 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 >>> \"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":[]}]
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": "1.0.2",
"version": "2.0.0",
"exposed-modules": [
"SHA"
],
Expand Down
76 changes: 37 additions & 39 deletions src/SHA.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SHA exposing (sha224, sha256, sha384, sha512)

{-| 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/), which can easily be converted to something else.
{-| 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.
@docs sha224, sha256, sha384, sha512
Expand All @@ -19,21 +19,20 @@ import SHA.Internal.SHA512
>>> import Binary
>>> Binary.toHex (sha224 "")
"D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F"
>>> Binary.toHex (sha224 "abc")
>>> "abc"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha224
..> |> Binary.toHex
"23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7"
>>> Binary.toHex (sha224 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
"75388B16512776CC5DBA5DA1FD890150B0C6455CB4F58B1952522525"
>>> Binary.toHex (sha224 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha224
..> |> Binary.toHex
"C97CA9A559850CE97A04A96DEF6D99A9E0E0E2AB14E6B8DF265FC0B3"
-}
sha224 : String -> Bits
sha224 : Bits -> Bits
sha224 message =
message
|> preprocess { blockLength = 512 }
Expand All @@ -45,21 +44,20 @@ sha224 message =
>>> import Binary
>>> Binary.toHex (sha256 "")
"E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"
>>> Binary.toHex (sha256 "abc")
>>> "abc"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha256
..> |> Binary.toHex
"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"
>>> Binary.toHex (sha256 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
"248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1"
>>> Binary.toHex (sha256 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha256
..> |> Binary.toHex
"CF5B16A778AF8380036CE59E7B0492370B249B11E8F07A51AFAC45037AFEE9D1"
-}
sha256 : String -> Bits
sha256 : Bits -> Bits
sha256 message =
message
|> preprocess { blockLength = 512 }
Expand All @@ -71,20 +69,20 @@ sha256 message =
>>> import Binary
>>> Binary.toHex (sha384 "")
"38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95B"
>>> Binary.toHex (sha384 "abc")
>>> "abc"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha384
..> |> Binary.toHex
"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7"
>>> Binary.toHex (sha384 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
"3391FDDDFC8DC7393707A65B1B4709397CF8B1D162AF05ABFE8F450DE5F36BC6B0455A8520BC4E6F5FE95B1FE3C8452B"
>>> Binary.toHex (sha384 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha384
..> |> Binary.toHex
"09330C33F71147E83D192FC782CD1B4753111B173B3B05D22FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039"
-}
sha384 : String -> Bits
sha384 : Bits -> Bits
sha384 message =
message
|> preprocess { blockLength = 1024 }
Expand All @@ -96,20 +94,20 @@ sha384 message =
>>> import Binary
>>> Binary.toHex (sha512 "")
"CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E"
>>> Binary.toHex (sha512 "abc")
>>> "abc"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha512
..> |> Binary.toHex
"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F"
>>> Binary.toHex (sha512 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
"204A8FC6DDA82F0A0CED7BEB8E08A41657C16EF468B228A8279BE331A703C33596FD15C13B1B07F9AA1D3BEA57789CA031AD85C7A71DD70354EC631238CA3445"
>>> Binary.toHex (sha512 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu")
>>> "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
..> |> Binary.fromStringAsUtf8
..> |> SHA.sha512
..> |> Binary.toHex
"8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA17299AEADB6889018501D289E4900F7E4331B99DEC4B5433AC7D329EEB6DD26545E96E55B874BE909"
-}
sha512 : String -> Bits
sha512 : Bits -> Bits
sha512 message =
message
|> preprocess { blockLength = 1024 }
Expand Down
14 changes: 8 additions & 6 deletions src/SHA/Internal.elm
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,25 @@ import SHA.Internal.HashTable exposing (..)
### SHA-256
>>> ""
..> |> Binary.fromStringAsUtf8
..> |> preprocess { blockLength = 512 }
..> |> Binary.width
512
>>> "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
..> |> Binary.fromStringAsUtf8
..> |> preprocess { blockLength = 512 }
..> |> Binary.width
1024
>>> "abc"
..> |> Binary.fromStringAsUtf8
..> |> preprocess { blockLength = 512 }
..> |> Binary.toHex
"61626380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018"
>>> "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
..> |> Binary.fromStringAsUtf8
..> |> preprocess { blockLength = 512 }
..> |> Binary.toHex
"6162636462636465636465666465666765666768666768696768696A68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F70718000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001C0"
Expand All @@ -47,20 +51,18 @@ import SHA.Internal.HashTable exposing (..)
### SHA-512
>>> "abc"
..> |> Binary.fromStringAsUtf8
..> |> preprocess { blockLength = 1024 }
..> |> Binary.toHex
"6162638000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018"
-}
preprocess : { blockLength : Int } -> String -> Bits
preprocess : { blockLength : Int } -> Bits -> Bits
preprocess { blockLength } message =
let
m =
Binary.fromStringAsUtf8 message

-- Length of the message in bits
l =
Binary.width m
Binary.width message

-- (k + l + 1 + 64) is a multiple of `blockLength`
--
Expand All @@ -69,7 +71,7 @@ preprocess { blockLength } message =
k =
blockLength - modBy blockLength (l + 1 + blockLength // 8)
in
[ m
[ message

-- Append a single 1 bit (represented as a single byte)
, Binary.fromBooleans [ True ]
Expand Down

0 comments on commit b3a7cc7

Please sign in to comment.