Coder Social home page Coder Social logo

hs-cryptohash's Introduction

CryptoHash

hs-cryptohash provides many different secure digest algorithms, also called cryptographic hash functions or, simply, cryptohashes. The package exports common hash functions, as well as some more exotic ones, and provides a single API for them all.

The general performance is comparable to the most optimised implementations available.

Here is the complete list of supported algorithms:

  • MD2, MD4, MD5
  • RIPEMD160
  • SHA1
  • SHA-2 family: 224, 256, 384, 512 and the newer 512t
  • SHA-3 (aka Keccak)
  • Skein: 256, 512
  • Tiger
  • Whirlpool

You can easily import any hash with the following:

import qualified Crypto.Hash.<HASH> as <Hash>

We recommend using import qualified because the APIs are similar and many of the modules reuse the same names. However, if you are ony using one module, there is no need to qualify the names.

Incremental API

it's based on 4 different functions, similar to the lowlevel operations of a typical hash:

  • init: create a new hash context
  • update: update non-destructively a new hash context with a strict bytestring
  • updates: same as update, except that it takes a list of strict bytestring
  • finalize: finalize the context and returns a digest bytestring.

all those operations are completely pure, and instead of changing the context as usual in others language, it re-allocates a new context each time.

One Pass API

The one pass API use the incremental API under the hood, but expose common operations to create digests out of a bytestring and lazy bytestring.

  • hash: create a digest (init+update+finalize) from a strict bytestring
  • hashlazy: create a digest (init+update+finalize) from a lazy bytestring

More Type safety

A more type safe API is also available from Crypto.Hash. The API provides all the supported hashes in the same namespace, through unified functions.

It introduces 2 new types, the Context type and the Digest type. Both those types are parametrized with the HashAlgorithm used.

The API is very similar to each single hash module, except the types are slightly different.

import Crypto.Hash

-- use the incremental API to hash the byte [1,2,3] with SHA1
-- and print the hexadecimal digest.
example1 = do
    let ctx = hashInit
        ctx' = hashUpdates ctx [ Data.ByteString.pack [1,2,3] ]
        dgt  = hashFinalize ctx' :: Digest SHA1
    putStrLn $ show dgt

-- use the one-pass API to hash the byte 1,2,3 with SHA3_512
-- and print the hexadecimal digest.
example2 = do
    let dgt  = hash (Data.ByteString.pack [1,2,3]) :: Digest SHA3_512
    putStrLn $ show dgt

Performance

Cryptohash uses C implementations to provide maximum performance. see the cbits directory for more information

hs-cryptohash's People

Contributors

vincenthz avatar ppelleti avatar alevy avatar cjwatson avatar iquiw avatar juhp avatar snoyberg avatar mightybyte avatar spl avatar singpolyma avatar

Watchers

Herbert Valerio Riedel avatar James Cloos avatar

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.