Wycheproof.hs (1705B)
1 {-# LANGUAGE OverloadedStrings #-} 2 3 module Wycheproof ( 4 Wycheproof(..) 5 , PbkdfTestGroup(..) 6 , PbkdfTest(..) 7 ) where 8 9 import Data.Aeson ((.:)) 10 import qualified Data.Aeson as A 11 import qualified Data.ByteString as BS 12 import qualified Data.ByteString.Base16 as B16 13 import qualified Data.Text as T 14 import qualified Data.Text.Encoding as TE 15 import Data.Word (Word32, Word64) 16 17 data Wycheproof = Wycheproof { 18 wp_numberOfTests :: !Int 19 , wp_testGroups :: ![PbkdfTestGroup] 20 } deriving Show 21 22 instance A.FromJSON Wycheproof where 23 parseJSON = A.withObject "Wycheproof" $ \m -> Wycheproof 24 <$> m .: "numberOfTests" 25 <*> m .: "testGroups" 26 27 data PbkdfTestGroup = PbkdfTestGroup { 28 ptg_type :: !T.Text 29 , ptg_tests :: ![PbkdfTest] 30 } deriving Show 31 32 instance A.FromJSON PbkdfTestGroup where 33 parseJSON = A.withObject "PbkdfTestGroup" $ \m -> PbkdfTestGroup 34 <$> m .: "type" 35 <*> m .: "tests" 36 37 data PbkdfTest = PbkdfTest { 38 pt_tcId :: !Int 39 , pt_comment :: !T.Text 40 , pt_password :: !BS.ByteString 41 , pt_salt :: !BS.ByteString 42 , pt_iterationCount :: !Word64 43 , pt_dkLen :: !Word32 44 , pt_dk :: !BS.ByteString 45 , pt_result :: !T.Text 46 } deriving Show 47 48 decodehex :: T.Text -> BS.ByteString 49 decodehex t = case B16.decode (TE.encodeUtf8 t) of 50 Nothing -> error "bang" 51 Just bs -> bs 52 53 instance A.FromJSON PbkdfTest where 54 parseJSON = A.withObject "PbkdfTest" $ \m -> PbkdfTest 55 <$> m .: "tcId" 56 <*> m .: "comment" 57 <*> fmap decodehex (m .: "password") 58 <*> fmap decodehex (m .: "salt") 59 <*> m .: "iterationCount" 60 <*> m .: "dkLen" 61 <*> fmap decodehex (m .: "dk") 62 <*> m .: "result" 63 64