Hash2Pub/src/asn1test.hs

51 lines
1.8 KiB
Haskell
Raw Normal View History

2020-04-21 14:21:36 +02:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import qualified Data.ASN1.BinaryEncoding as ASN1
import qualified Data.ASN1.Encoding as ASN1
import qualified Data.ASN1.Error as ASN1
import qualified Data.ASN1.Parse as ASN1P
import qualified Data.ASN1.Types as ASN1
import qualified Data.ByteString as BS
import Data.Maybe (fromMaybe)
import Debug.Trace (trace)
2020-04-21 14:21:36 +02:00
-- import Hash2Pub.Fedichord
-- encoding values as ASN.1 types is done using Data.ASN1.Prim
someASN1 :: [ASN1.ASN1]
someASN1 = ASN1.Start ASN1.Sequence : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.Visible domain) : ASN1.ASN1String (ASN1.asn1CharacterString ASN1.UTF8 unicode) : fmap ASN1.IntVal xs <> [ASN1.End ASN1.Sequence]
2020-04-21 14:21:36 +02:00
where
domain = "domains.are.ascii.on.ly"
unicode = "Hähä, but unicode string!"
xs = [ 23, 42, 2342 ]
asn1AsDer :: [ASN1.ASN1] -> BS.ByteString
asn1AsDer = ASN1.encodeASN1' ASN1.DER
derToAsn1 :: BS.ByteString -> Either ASN1.ASN1Error [ASN1.ASN1]
derToAsn1 = ASN1.decodeASN1' ASN1.DER
getUnicodeField :: [ASN1.ASN1] -> String
getUnicodeField (ASN1.Start ASN1.Sequence : _ : ASN1.ASN1String strASN1 : _) = fromMaybe "" $ ASN1.asn1CharacterToString strASN1
testParser :: ASN1P.ParseASN1 String
testParser = do
foo <- ASN1P.onNextContainer ASN1.Sequence getAll
pure $ show foo
getAll :: ASN1P.ParseASN1 [ASN1.ASN1]
getAll = ASN1P.getMany ASN1P.getNext
--stringParser asn1obj =
-- fmap (fromMaybe "" $ ASN1.asn1CharacterToString) ASN1P.getNext
2020-04-21 14:21:36 +02:00
main = do
print someASN1
print $ asn1AsDer someASN1
print $ derToAsn1 . asn1AsDer $ someASN1
putStrLn $ getUnicodeField someASN1
print $ ASN1.decodeASN1Repr' ASN1.DER $ asn1AsDer someASN1
putStrLn "\nLet's try a real parser combinator:"
print $ ASN1P.runParseASN1State testParser someASN1