• 我们在哪一颗星上见过 ,以至如此相互思念 ;我们在哪一颗星上相互思念过,以至如此相互深爱
  • 我们在哪一颗星上分别 ,以至如此相互辉映 ;我们在哪一颗星上入睡 ,以至如此唤醒黎明
  • 认识世界 克服困难 洞悉所有 贴近生活 寻找珍爱 感受彼此

python模块:PyOpenSSL模块

python知识点 云涯 4年前 (2019-12-06) 1794次浏览

官方网址:https://www.pyopenssl.org/en/stable/api.html

官方网址2:  https://www.pyopenssl.org/en/0.15.1/api/crypto.html#x509name-objects

import OpenSSL
from dateutil import parser

初始化对象
cert=OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM,open(r”.\result\certificate_file\yunyawu.top\1294797009.crt”).read())

 

get_issuer()表示证书颁发者:certIssue = cert.get_issuer()

获得颁发者:
print (“颁发者:              “,certIssue.commonName)

print(“CN : 通用名称  OU : 机构单元名称”)
print(“O  : 机构名    L  : 地理位置”)
print(“S  : 州/省名   C  : 国名”)
get_components()以2个元组的序列返回此名称的组成部分。
for item in certIssue.get_components():  print(item[0].decode(“utf-8″), ”  ——  “,item[1].decode(“utf-8”))
获得证书版本:

print (“证书版本:            “,cert.get_version() + 1)

获得证书序列号:(cert.get_serial_number()会返回十进制数据)
print (“证书序列号:          “,hex(cert.get_serial_number()))

获得签名算法:
print (“证书中使用的签名算法: “,cert.get_signature_algorithm().decode(“UTF-8”))

获取证书开始与到期时间:
datetime_struct = parser.parse(cert.get_notBefore().decode(“UTF-8”))
print (“有效期从:             “,datetime_struct.strftime(‘%Y-%m-%d %H:%M:%S’))
datetime_struct = parser.parse(cert.get_notAfter().decode(“UTF-8”))
print (“到:                   “,datetime_struct.strftime(‘%Y-%m-%d %H:%M:%S’))

判断证书是否过期:
print (“证书是否已经过期:      “,cert.has_expired())

判断公钥长度:
print(“公钥长度” ,cert.get_pubkey().bits())

判断公钥类型:
print(“公钥类型” ,cert.get_pubkey().type())

获得公钥的base64加密:
print(“公钥:\n” ,OpenSSL.crypto.dump_publickey(OpenSSL.crypto.FILETYPE_PEM, cert.get_pubkey()).decode(“utf-8”))

获得公钥扩展:

get_extension_count()获得扩展数,通过get_extension()里面添加索引来获得需要的扩展
print(‘X509v3 Subject Key Identifier:’,str(cert.get_extension(1)))
# for index in range(get_extension_count()):
#             ext = cert.get_extension(index)


云涯历险记 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python模块:PyOpenSSL模块
喜欢 (2)