Coder Social home page Coder Social logo

tsfoster / elm-sha1 Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 3.0 1.02 MB

Calculate SHA-1 digests in Elm

Home Page: http://package.elm-lang.org/packages/TSFoster/elm-sha1/latest/

License: BSD 3-Clause "New" or "Revised" License

Elm 93.59% Makefile 5.26% Awk 1.15%
elm elmlang elm-lang sha sha1 sha-1

elm-sha1's Introduction

SHA1

Calculate SHA-1 digests in Elm.

This package supports hashing of:

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

And can represent the digest as:

Release notes

Upgrading from 1.0.0 <= v < 1.0.4 is strongly recommended. Later versions are more rigiorously tested, are significantly more performant, contain bug fixes, and work with elm v0.19.1.

  • 2.0.0 prioritises use of Bytes over [List Int].
  • 1.1.0 introduces the option to hash Bytes and represent Digests as Bytes, without breaking the API.
  • 1.0.5 provides the performance improvements of the hashing algorithm in a non-breaking manner.

Please see the Contributors section for thanks and more information.

Examples

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.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.toByteValues digest3
--> [ 0x80, 0x7C, 0x1E, 0x38
--> , 0x5E, 0x4B, 0xCA, 0x0F
--> , 0x87, 0x6B, 0xBB, 0xA3
--> , 0xAE, 0x56, 0x30, 0x35
--> , 0xAF, 0xC7, 0xC2, 0x09
--> ]

Validation

Not officially validated through CAVP/CMVP, although digests are tested against CAVS responses via pyca/cryptography.

Validation tests can be run by executing make cavs. This requires make, curl, elm, elm-test and gawk. Alternatively, to run all tests, execute make test (which also requires elm-format and elm-verify-examples). See the Makefile for details.

This package is also tested against additional hashes in the documentation (using elm-verify-examples), tests/Tests.elm, and indirectly via romariolopezc/elm-hmac-sha1’s tests, and TSFoster/elm-uuid’s tests.

Please note that SHA-1 is not “considered secure against well-funded opponents”, but it does have its uses, including, but not limited to, version 5 UUIDs.

Contributors

Special thanks to Folkert de Vries for a major rewrite of this library, including huge performance gains, more rigorous testing, and support for elm/bytes. More information can be found on this Elm Discourse thread.

TODO

  • Benchmarks
    • Add way to run benchmarks
    • Automate testing that changes do not negatively impact performance?
  • Write CONTRIBUTING.md

elm-sha1's People

Contributors

bendingbender avatar colinta avatar folkertdev avatar tsfoster avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

elm-sha1's Issues

Incorrect sha1 calculation

Input bytes

[54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46]

Expected bytes
[189, 139, 32, 137, 84, 157, 87, 160, 91, 236, 186, 206, 81, 18, 194, 197, 147, 177, 175, 139]
Actual
[84,163,206,165,165,139,250,8,32,82,97,58,200,153,115,37,77,191,211,183]

Tested with Python code:

import hashlib
h = hashlib.new('sha1')
h.update(bytearray([54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,54,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46]))
[ord(c) for c in h.digest()]
>>> [189, 139, 32, 137, 84, 157, 87, 160, 91, 236, 186, 206, 81, 18, 194, 197, 147, 177, 175, 139]

I discovered this while trying to debug the HmacSha1 library. It has something to do (?) with the length of the input data being 311 characters. The 64 54 in the front is an empty key, followed by a string of 311 . characters.

Can you put a note about algorithm validation in your README?

Hi @TSFoster!

I have two requests for you for this package's README:

  1. Can you note in your README whether your implementation has been validated by a program like CAVP or otherwise reviewed for side-channel attacks? I would consider this request to be important.
  2. Would you consider also noting in the README that SHA-1 is not a cryptographically strong hashing algorithm? I think this is less important than (1) since there are plenty of non-cryptographic uses for hashing.

Thank you!

Tests not running

Hi there

I'm having trouble running the tests. Perhaps i'm doing something wrong? Here are my steps:

  • cloned the repository.
  • ran elm make
  • installed elm-test
  • then when i run `elm-test' I get this error:
-- MODULE NOT FOUND - /home/koshy/Documents/www/elm_projects/elm-sha1/tests/Tests.elm

You are trying to import a `Generated.SHA1LongMsg` module:

8| import Generated.SHA1LongMsg as Long
          ^^^^^^^^^^^^^^^^^^^^^
I checked the "dependencies" and "source-directories" listed in your elm.json,
but I cannot find it! Maybe it is a typo for one of these names?

    Test.Html.Event
    Test.Html.Query
    Test.Html.Selector
    Bytes.Decode

Hint: If it is not a typo, check the "dependencies" and "source-directories" of
your elm.json to make sure all the packages you need are listed there!

Am I missing a package?

Hashing elm/bytes

The readme says:

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!

I’d like both :) I want to hash some images (megabytes of data) before uploading them somewhere, see my question at Elm Discourse for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.