How can I decode a SSL certificate using python?

前端 未结 5 1948
半阙折子戏
半阙折子戏 2020-12-02 14:42

How can I decode a pem-encoded (base64) certificate with Python? For example this here from github.com:

-----BEGIN CERTIFICATE-----
MIIHKjCCBhKgAwIBAgIQDnd2i         


        
5条回答
  •  感情败类
    2020-12-02 14:55

    You can use pyasn1 and pyasn1-modules packages to parse this kind of data. For instance:

    from pyasn1_modules import pem, rfc2459
    from pyasn1.codec.der import decoder
    
    substrate = pem.readPemFromFile(open('cert.pem'))
    cert = decoder.decode(substrate, asn1Spec=rfc2459.Certificate())[0]
    print(cert.prettyPrint())
    

    Read the docs for pyasn1 for the rest.

提交回复
热议问题