更新:我找到了问题的解决方案。希望这能帮助其他有同样问题的人
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.String.Conversions (cs)
import Crypto.Hash (hash, SHA256(..), Digest)
import qualified Data.ByteString as BS (ByteString, readFile)
import Data.Text.Encoding (encodeUtf8)
import qualified Data.Text.IO as TIO
import System.IO (hFlush, stdout)
import Data.ByteArray.Encoding (convertToBase, Base (Base64))
import Data.List (head)
import Data.Text (Text(..))
----- Read file import
import Data.Monoid ((<>))
import Control.Applicative
import Data.ASN1.Types
import Data.ASN1.BinaryEncoding
import Data.ASN1.Encoding
import Data.Maybe
import qualified Data.X509 as X509
import Data.X509.Memory (pemToKey)
import Data.PEM (pemParseLBS, pemParseBS, pemWriteBS, pemContent, pemName, PEM)
import qualified Data.ByteString.Lazy as L
-------------------------
main :: IO ()
main = do
--pems <- readPEMs "./certs/test.pem" -- read from file
let cert1 = "-----BEGIN CERTIFICATE-----\n" ++ "MIIFXjCCBEagAwIBAgIIKPOmOJqfnTIwDQYJKoZIhvcNAQELBQAwgbQxCzAJBgNVBAYTAlVTMRAwDgYDVQQIEwdBcml6b25hMRMwEQYDVQQHEwpTY290dHNkYWxlMRowGAYDVQQKExFHb0RhZGR5LmNvbSwgSW5jLjEtMCsGA1UECxMkaHR0cDovL2NlcnRzLmdvZGFkZHkuY29tL3JlcG9zaXRvcnkvMTMwMQYDVQQDEypHbyBEYWRkeSBTZWN1cmUgQ2VydGlmaWNhdGUgQXV0aG9yaXR5IC0gRzIwHhcNMTYwMjA5MjE1NzM5WhcNMTkwMzA3MjIzNTUxWjBrMQswCQYDVQQGEwJVUzENMAsGA1UECBMEVXRhaDEVMBMGA1UEBxMMU291dGggSm9yZGFuMRcwFQYDVQQKEw5BbGxlZ2lhbmNlIEluYzEdMBsGA1UEAwwUKi5hbGxlZ2lhbmNldGVjaC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDHWaH6ziZnWy3Uy/wmM7zbHqrg/AmdsWk54x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KPdlD3wzUEpqLuD1scucRfcjwt6X0K3otthMJ9PZ9tJQMSOLnUFKLvG+ZFlnkY+K3pmKWsk2ZMrua2m9wvqVXq4ZgEKQ6xr91HYkPCjfLt14ExnL3vNdXS0DR7LtjJDyXJeFkT0Ev81W9c5dk7gzbwg+EM4UYxW6qz+7WyHvV+uLNnHNmwm1b17tVfYvu3w==" ++ "\n-----END CERTIFICATE-----"
pems <- readPEM cert1
let certx = X509.decodeSignedObject $ pemContent $ head pems
case certx of
Left st1 -> putStrLn $ "Error reading cert: " ++ st1
Right scrt1 -> do
let crt1 = X509.getCertificate scrt1
print crt1
readPEM :: String -> IO [PEM]
readPEM content = --do
return $ either error id $ pemParseBS $ cs content
readPEMs :: FilePath -> IO [PEM]
readPEMs filepath = do
content <- BS.readFile filepath
return $ either error id $ pemParseBS content