apache-md5: Apache specific MD5 digest algorighm.

[ benchmark, bsd3, cryptography, data, library ] [ Propose Tags ]
Change log ChangeLog.md
Dependencies base (==4.*), bytestring (==0.10.*), deepseq (>= && <2), ghc‑prim [details]
License BSD-3-Clause
Copyright (c) 2009, 2010, 2012-2015 Peter Trško
Author Peter Trško <peter.trsko@gmail.com>
Maintainer peter.trsko@gmail.com
Revised Revision 2 made by PeterTrsko at Wed Jan 20 22:11:12 UTC 2016
Category Data, Cryptography
Home page https://github.com/trskop/apache-md5
Bug tracker https://github.com/trskop/apache-md5/issues
Source repo head: git clone git://github.com/trskop/apache-md5.git
this: git clone git://github.com/trskop/apache-md5.git(tag v0.6.1.4)
Uploaded by PeterTrsko at Sun Jun 21 13:18:59 UTC 2015
Distributions NixOS:
Downloads 3379 total (21 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-06-21 [all 1 reports]
Hackage Matrix CI

Haskell implementation of Apache HTTP server specific MD5 digest algorithm that uses OpenSSL MD5() function.

For usage example see Data.Digest.ApacheMD5 module or README file.

[Skip to Readme]





Pass additional warning flags to GHC.


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


Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees

Readme for apache-md5-

[back to package description]

Apache MD5

Hackage Hackage Dependencies Haskell Programming Language BSD3 License



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


Stable releases with API documentation are available on Hackage.


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

  • -fpedantic (disabled by default)

    Pass additional warning flags to GHC.


Create htpasswd like entry and print it to stdout:

module Main (main)

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"

Compiling and running above example:

$ ghc -Wall example.hs
[1 of 1] Compiling Main             ( example.hs, example.o )
Linking example ...
$ ./example
$ ./example foo 123456

Unit Tests

Requires htpasswd command line utility installed. 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.

To run tests use command similar to this:

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


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.


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