{-# LANGUAGE Trustworthy #-}
module Data.Structured.MD5 (
MD5,
showMD5,
md5,
md5FromInteger,
) where
import Data.Bits (complement, shiftR, (.&.))
import Foreign.Ptr (castPtr)
import GHC.Fingerprint (Fingerprint (..), fingerprintData)
import Numeric (showHex)
import System.IO.Unsafe (unsafeDupablePerformIO)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BS
type MD5 = Fingerprint
showMD5 :: MD5 -> String
showMD5 (Fingerprint a b) = pad a' ++ pad b' where
a' = showHex a ""
b' = showHex b ""
pad s = replicate (16 - length s) '0' ++ s
md5 :: BS.ByteString -> MD5
md5 bs = unsafeDupablePerformIO $ BS.unsafeUseAsCStringLen bs $ \(ptr, len) ->
fingerprintData (castPtr ptr) len
md5FromInteger :: Integer -> MD5
md5FromInteger i = Fingerprint hi lo where
mask = complement 0
lo = mask .&. fromInteger i
hi = mask .&. fromInteger (i `shiftR` 64)