ripemd160

Pure Haskell RIPEMD-160, HMAC-RIPEMD160 (docs.ppad.tech/ripemd160).
git clone git://git.ppad.tech/ripemd160.git
Log | Files | Refs | README | LICENSE

Main.hs (6105B)


      1 {-# LANGUAGE OverloadedStrings #-}
      2 {-# LANGUAGE ViewPatterns #-}
      3 
      4 module Main where
      5 
      6 import qualified Crypto.Hash.RIPEMD160 as RIPEMD160
      7 import qualified Data.ByteString as BS
      8 import qualified Data.ByteString.Lazy as BL
      9 import qualified Data.ByteString.Base16 as B16
     10 import Test.Tasty
     11 import Test.Tasty.HUnit
     12 
     13 main :: IO ()
     14 main = defaultMain $ testGroup "ppad-ripemd160" [
     15     unit_tests
     16   ]
     17 
     18 unit_tests :: TestTree
     19 unit_tests = testGroup "unit tests" [
     20     testGroup "hash" [
     21       cmp_hash "hv0" hv0_put hv0_pec
     22     , cmp_hash "hv1" hv1_put hv1_pec
     23     , cmp_hash "hv2" hv2_put hv2_pec
     24     , cmp_hash "hv3" hv3_put hv3_pec
     25     , cmp_hash "hv4" hv4_put hv4_pec
     26     , cmp_hash "hv5" hv5_put hv5_pec
     27     , cmp_hash "hv6" hv6_put hv6_pec
     28     , cmp_hash "hv7" hv7_put hv7_pec
     29     , cmp_hash "hv8" hv8_put hv8_pec
     30     ]
     31   , testGroup "hash_lazy" [
     32       cmp_hash_lazy "hv0" hv0_put hv0_pec
     33     , cmp_hash_lazy "hv1" hv1_put hv1_pec
     34     , cmp_hash_lazy "hv2" hv2_put hv2_pec
     35     , cmp_hash_lazy "hv3" hv3_put hv3_pec
     36     , cmp_hash_lazy "hv4" hv4_put hv4_pec
     37     , cmp_hash_lazy "hv5" hv5_put hv5_pec
     38     , cmp_hash_lazy "hv6" hv6_put hv6_pec
     39     , cmp_hash_lazy "hv7" hv7_put hv7_pec
     40     , cmp_hash_lazy "hv8" hv8_put hv8_pec
     41     ]
     42   , testGroup "hmac" [
     43       cmp_hmac "hmv1" hmv1_key hmv1_put hmv1_pec
     44     , cmp_hmac "hmv2" hmv2_key hmv2_put hmv2_pec
     45     , cmp_hmac "hmv3" hmv3_key hmv3_put hmv3_pec
     46     , cmp_hmac "hmv4" hmv4_key hmv4_put hmv4_pec
     47     , cmp_hmac "hmv5" hmv5_key hmv5_put hmv5_pec
     48     , cmp_hmac "hmv6" hmv6_key hmv6_put hmv6_pec
     49     , cmp_hmac "hmv7" hmv7_key hmv7_put hmv7_pec
     50     ]
     51   , testGroup "hmac_lazy" [
     52       cmp_hmac_lazy "hmv1" hmv1_key hmv1_put hmv1_pec
     53     , cmp_hmac_lazy "hmv2" hmv2_key hmv2_put hmv2_pec
     54     , cmp_hmac_lazy "hmv3" hmv3_key hmv3_put hmv3_pec
     55     , cmp_hmac_lazy "hmv4" hmv4_key hmv4_put hmv4_pec
     56     , cmp_hmac_lazy "hmv5" hmv5_key hmv5_put hmv5_pec
     57     , cmp_hmac_lazy "hmv6" hmv6_key hmv6_put hmv6_pec
     58     , cmp_hmac_lazy "hmv7" hmv7_key hmv7_put hmv7_pec
     59     ]
     60   ]
     61 
     62 cmp_hash :: String -> BS.ByteString -> BS.ByteString -> TestTree
     63 cmp_hash msg put pec = testCase msg $ do
     64   let out = B16.encode (RIPEMD160.hash put)
     65   assertEqual mempty pec out
     66 
     67 cmp_hash_lazy :: String -> BS.ByteString -> BS.ByteString -> TestTree
     68 cmp_hash_lazy msg (BL.fromStrict -> put) pec = testCase msg $ do
     69   let out = B16.encode (RIPEMD160.hash_lazy put)
     70   assertEqual mempty pec out
     71 
     72 hv0_put, hv0_pec :: BS.ByteString
     73 hv0_put = mempty
     74 hv0_pec = "9c1185a5c5e9fc54612808977ee8f548b2258d31"
     75 
     76 hv1_put, hv1_pec :: BS.ByteString
     77 hv1_put = "a"
     78 hv1_pec = "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"
     79 
     80 hv2_put, hv2_pec :: BS.ByteString
     81 hv2_put = "abc"
     82 hv2_pec = "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"
     83 
     84 hv3_put, hv3_pec :: BS.ByteString
     85 hv3_put = "message digest"
     86 hv3_pec = "5d0689ef49d2fae572b881b123a85ffa21595f36"
     87 
     88 hv4_put, hv4_pec :: BS.ByteString
     89 hv4_put = "abcdefghijklmnopqrstuvwxyz"
     90 hv4_pec = "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"
     91 
     92 hv5_put, hv5_pec :: BS.ByteString
     93 hv5_put = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
     94 hv5_pec = "12a053384a9c0c88e405a06c27dcf49ada62eb2b"
     95 
     96 hv6_put, hv6_pec :: BS.ByteString
     97 hv6_put = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
     98 hv6_pec = "b0e20b6e3116640286ed3a87a5713079b21f5189"
     99 
    100 hv7_put, hv7_pec :: BS.ByteString
    101 hv7_put = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
    102 hv7_pec = "9b752e45573d4b39f4dbd3323cab82bf63326bfb"
    103 
    104 hv8_put, hv8_pec :: BS.ByteString
    105 hv8_put = BS.replicate 1000000 0x61
    106 hv8_pec = "52783243c1697bdbe16d37f97f68f08325dc1528"
    107 
    108 -- vectors from
    109 --
    110 -- https://www.rfc-editor.org/rfc/rfc2286.html#section-2
    111 
    112 cmp_hmac
    113   :: String -> BS.ByteString -> BS.ByteString -> BS.ByteString -> TestTree
    114 cmp_hmac msg key put pec = testCase msg $ do
    115   let out = B16.encode (RIPEMD160.hmac key put)
    116   assertEqual mempty pec out
    117 
    118 cmp_hmac_lazy
    119   :: String -> BS.ByteString -> BS.ByteString -> BS.ByteString -> TestTree
    120 cmp_hmac_lazy msg key (BL.fromStrict -> put) pec = testCase msg $ do
    121   let out = B16.encode (RIPEMD160.hmac_lazy key put)
    122   assertEqual mempty pec out
    123 
    124 hmv1_key :: BS.ByteString
    125 hmv1_key = B16.decodeLenient "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
    126 
    127 hmv1_put :: BS.ByteString
    128 hmv1_put = "Hi There"
    129 
    130 hmv1_pec :: BS.ByteString
    131 hmv1_pec = "24cb4bd67d20fc1a5d2ed7732dcc39377f0a5668"
    132 
    133 hmv2_key :: BS.ByteString
    134 hmv2_key = "Jefe"
    135 
    136 hmv2_put :: BS.ByteString
    137 hmv2_put = "what do ya want for nothing?"
    138 
    139 hmv2_pec :: BS.ByteString
    140 hmv2_pec = "dda6c0213a485a9e24f4742064a7f033b43c4069"
    141 
    142 hmv3_key :: BS.ByteString
    143 hmv3_key = B16.decodeLenient "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    144 
    145 hmv3_put :: BS.ByteString
    146 hmv3_put = B16.decodeLenient "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
    147 
    148 hmv3_pec :: BS.ByteString
    149 hmv3_pec = "b0b105360de759960ab4f35298e116e295d8e7c1"
    150 
    151 hmv4_key :: BS.ByteString
    152 hmv4_key = B16.decodeLenient "0102030405060708090a0b0c0d0e0f10111213141516171819"
    153 
    154 hmv4_put :: BS.ByteString
    155 hmv4_put = B16.decodeLenient "cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"
    156 
    157 hmv4_pec :: BS.ByteString
    158 hmv4_pec = "d5ca862f4d21d5e610e18b4cf1beb97a4365ecf4"
    159 
    160 hmv5_key :: BS.ByteString
    161 hmv5_key = B16.decodeLenient "0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c"
    162 
    163 hmv5_put :: BS.ByteString
    164 hmv5_put = "Test With Truncation"
    165 
    166 hmv5_pec :: BS.ByteString
    167 hmv5_pec = "7619693978f91d90539ae786500ff3d8e0518e39"
    168 
    169 hmv6_key :: BS.ByteString
    170 hmv6_key = B16.decodeLenient "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    171 
    172 hmv6_put :: BS.ByteString
    173 hmv6_put = "Test Using Larger Than Block-Size Key - Hash Key First"
    174 
    175 hmv6_pec :: BS.ByteString
    176 hmv6_pec = "6466ca07ac5eac29e1bd523e5ada7605b791fd8b"
    177 
    178 hmv7_key :: BS.ByteString
    179 hmv7_key = hmv6_key
    180 
    181 hmv7_put :: BS.ByteString
    182 hmv7_put = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
    183 
    184 hmv7_pec :: BS.ByteString
    185 hmv7_pec = "69ea60798d71616cce5fd0871e23754cd75d5a0a"
    186