Giter VIP home page Giter VIP logo

apache-md5's Introduction

Apache MD5

Hackage Hackage Dependencies Haskell Programming Language BSD3 License

Build

Description

Haskell implementation of Apache specific MD5 digest algorithm that uses OpenSSL MD5.

Documentation

Stable releases with API documentation are available on Hackage.

Installation

Requires OpenSSL library with header files. On Debian and Ubuntu Linux it's provided by libssl-dev package that can be installed using apt-get:

$ apt-get install libssl-dev

For more see apt-get(8) manual page or e.g. Ubuntu Documentation: AptGet Howto.

After that just use cabal-install as you would normally do. For details see HaskellWiki: How to install a Cabal package.

Building Options

  • pedantic (disabled by default)

    Pass additional warning flags to GHC.

  • deepseq (disabled by default)

    Define instance of NFData for Salt newtype. This dependency is enforced for benchmark.

  • examples (disabled by default)

    Build example(s).

Example

Create htpasswd like entry and print it to stdout:

module Main (main)
    where

import Control.Applicative ((<$>))
import System.Environment (getArgs, getProgName)
import System.Exit (exitFailure)
import System.IO (hPutStrLn, stderr)

import Control.Monad.Random (evalRandIO, getRandomRs)
    -- MonadRandom package http://hackage.haskell.org/package/MonadRandom/
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS (index, pack)
import qualified Data.ByteString.Char8 as C8 (concat, pack, putStrLn, singleton)
    -- bytestring package http://hackage.haskell.org/package/bytestring
import Data.Digest.ApacheMD5 (Salt, apacheMD5, mkSalt, unSalt)
import Data.Digest.ApacheMD5.Internal (alpha64)


htpasswdEntry :: String -> String -> Salt -> ByteString
htpasswdEntry username password salt = C8.concat
    [ C8.pack username
    , C8.pack ":$apr1$"
    , unSalt salt
    , C8.singleton '$'
    , apacheMD5 (C8.pack password) salt
    ]

genSalt :: IO Salt
genSalt = do
    Just s <- evalRandIO $ mkSalt . BS.pack . map (BS.index alpha64) . take 8
        <$> getRandomRs (0, 63)
        -- We know that Salt is correctly generated, since we use alpha64 to do
        -- it. That is the reason why we can pattern match on Just.
        --
        -- Other option would be to use Salt value constructor from
        -- Data.Digest.ApacheMD5.Internal module.
    return s

main :: IO ()
main = do
    args <- getArgs
    progName <- getProgName
    case args of
        [userName, password] ->
            genSalt >>= C8.putStrLn . htpasswdEntry userName password
        _ -> do
            hPutStrLn stderr $ "Usage: " ++ progName ++ " USER_NAME PASSWORD"
            exitFailure

Compiling and running above example:

$ ghc -Wall example.hs
[1 of 1] Compiling Main             ( example.hs, example.o )
Linking example ...
$ ./example
Usage: example USER_NAME PASSWORD
$ ./example foo 123456
foo:$apr1$yM9AMlr2$EHssuHrqSAe8HPrAdN7HC/

Unit Tests

Requires htpasswd command line utility installed. It is used as a reference implementation of Apache MD5 algorithm by unit tests. On Debian and Ubuntu Linux it is provided by apache2-utils package that can be installed using apt-get:

$ apt-get install apache2-utils

For more see apt-get(8) manual page or e.g. Ubuntu Documentation: AptGet Howto.

There is also a possibility to compile Apache htpasswd your self, see test/apache-htpasswd/README.md for more information on how to do that.

To run tests use command similar to this:

$ cabal configure --enable-tests && cabal build && cabal test

Benchmarks

This package provides Criterion benchmarks, to run them you can use something like:

$ cabal configure --enable-benchmarks && cabal build && cabal bench

To generate HTML output one needs to specify output file. Then the last command in above chain would look like:

$ cabal bench --benchmark-option=--output=benchmarks.html

Where benchmarks.html is the name of Criterion generated HTML file.

License

The BSD 3-Clause License, see LICENSE file for details.

Contributions

Contributions, pull requests and bug reports are welcome! Please don't be afraid to contact the author.

apache-md5's People

Contributors

bergmark avatar trskop avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

bergmark

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.