本文整理汇总了Python中pyasn1.type.univ.BitString方法的典型用法代码示例。如果您正苦于以下问题:Python univ.BitString方法的具体用法?Python univ.BitString怎么用?Python univ.BitString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyasn1.type.univ
的用法示例。
在下文中一共展示了univ.BitString方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _key_identifier_from_public_key
# 需要导入模块: from pyasn1.type import univ [as 别名]
# 或者: from pyasn1.type.univ import BitString [as 别名]
def _key_identifier_from_public_key(public_key):
# This is a very slow way to do this.
serialized = public_key.public_bytes(
serialization.Encoding.DER,
serialization.PublicFormat.SubjectPublicKeyInfo
)
spki, remaining = decoder.decode(
serialized, asn1Spec=_SubjectPublicKeyInfo()
)
assert not remaining
# the univ.BitString object is a tuple of bits. We need bytes and
# pyasn1 really doesn't want to give them to us. To get it we'll
# build an integer and convert that to bytes.
bits = 0
for bit in spki.getComponentByName("subjectPublicKey"):
bits = bits << 1 | bit
data = utils.int_to_bytes(bits)
return hashlib.sha1(data).digest()
示例2: convertPKCS1toPKCS8pubKey
# 需要导入模块: from pyasn1.type import univ [as 别名]
# 或者: from pyasn1.type.univ import BitString [as 别名]
def convertPKCS1toPKCS8pubKey(bitsdata):
pubkey_pkcs1_b64 = b''.join(bitsdata.split(b'\n')[1:-2])
pubkey_pkcs1, restOfInput = der_decoder.decode(base64.b64decode(pubkey_pkcs1_b64))
bitstring = univ.Sequence()
bitstring.setComponentByPosition(0, univ.Integer(pubkey_pkcs1[0]))
bitstring.setComponentByPosition(1, univ.Integer(pubkey_pkcs1[1]))
bitstring = der_encoder.encode(bitstring)
try:
bitstring = ''.join([('00000000'+bin(ord(x))[2:])[-8:] for x in list(bitstring)])
except:
bitstring = ''.join([('00000000'+bin(x)[2:])[-8:] for x in list(bitstring)])
bitstring = univ.BitString("'%s'B" % bitstring)
pubkeyid = univ.Sequence()
pubkeyid.setComponentByPosition(0, univ.ObjectIdentifier('1.2.840.113549.1.1.1')) # == OID for rsaEncryption
pubkeyid.setComponentByPosition(1, univ.Null(''))
pubkey_seq = univ.Sequence()
pubkey_seq.setComponentByPosition(0, pubkeyid)
pubkey_seq.setComponentByPosition(1, bitstring)
base64.MAXBINSIZE = (64//4)*3
res = b"-----BEGIN PUBLIC KEY-----\n"
res += base64.encodestring(der_encoder.encode(pubkey_seq))
res += b"-----END PUBLIC KEY-----\n"
return res
示例3: _key_identifier_from_public_key
# 需要导入模块: from pyasn1.type import univ [as 别名]
# 或者: from pyasn1.type.univ import BitString [as 别名]
def _key_identifier_from_public_key(public_key):
if isinstance(public_key, RSAPublicKey):
data = public_key.public_bytes(
serialization.Encoding.DER,
serialization.PublicFormat.PKCS1,
)
elif isinstance(public_key, EllipticCurvePublicKey):
data = public_key.public_numbers().encode_point()
else:
# This is a very slow way to do this.
serialized = public_key.public_bytes(
serialization.Encoding.DER,
serialization.PublicFormat.SubjectPublicKeyInfo
)
spki, remaining = decoder.decode(
serialized, asn1Spec=_SubjectPublicKeyInfo()
)
assert not remaining
# the univ.BitString object is a tuple of bits. We need bytes and
# pyasn1 really doesn't want to give them to us. To get it we'll
# build an integer and convert that to bytes.
bits = 0
for bit in spki.getComponentByName("subjectPublicKey"):
bits = bits << 1 | bit
data = utils.int_to_bytes(bits)
return hashlib.sha1(data).digest()
示例4: realEncode
# 需要导入模块: from pyasn1.type import univ [as 别名]
# 或者: from pyasn1.type.univ import BitString [as 别名]
def realEncode(self, data):
return der.encoder.encode(univ.BitString(data))
示例5: rsa_public_key_pkcs1_to_pkcs8
# 需要导入模块: from pyasn1.type import univ [as 别名]
# 或者: from pyasn1.type.univ import BitString [as 别名]
def rsa_public_key_pkcs1_to_pkcs8(pkcs1_key):
"""Convert a PKCS1-encoded RSA private key to PKCS8."""
algorithm = RsaAlgorithmIdentifier()
algorithm["rsaEncryption"] = RSA_ENCRYPTION_ASN1_OID
pkcs8_key = PublicKeyInfo()
pkcs8_key["algorithm"] = algorithm
pkcs8_key["publicKey"] = univ.BitString.fromOctetString(pkcs1_key)
return encoder.encode(pkcs8_key)