MyNixOS website logo
Description

Apache specific MD5 digest algorighm.

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.

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

  • -fpedantic (disabled by default)

    Pass additional warning flags to GHC.

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. 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

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.

Contributions

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

Metadata

Version

0.6.1.4

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows