def getPublicKeyHexifiedString(self): """Returns the EC public key as a hex string using the uncompressed point representation. This is intended to be used in the encoder functions, as it surrounds the value with ''H to indicate its type.""" # We need to extract the point that represents this key. # The library encoding of the key is an 8-byte id, followed by 2 # bytes for the key length in bits, followed by the point on the # curve (represented by two python longs). There appear to also # be 2 bytes indicating the length of the point as encoded, but # Decoder takes care of that. encoded = self.key.encode() _, _, points = encoding.Decoder(encoded).int(8).int(2).point(2).out() # '04' indicates that the points are in uncompressed form. return "'%s%s%s'H" % ('04', longToEvenLengthHexString( points[0]), longToEvenLengthHexString(points[1]))
def asSubjectPublicKeyInfo(self): """Returns a subject public key info representing this key for use by pyasn1.""" algorithmIdentifier = rfc2459.AlgorithmIdentifier() algorithmIdentifier['algorithm'] = ecPublicKey algorithmIdentifier['parameters'] = self.keyOID spki = rfc2459.SubjectPublicKeyInfo() spki['algorithm'] = algorithmIdentifier # We need to extract the point that represents this key. # The library encoding of the key is an 8-byte id, followed by 2 # bytes for the key length in bits, followed by the point on the # curve (represented by two python longs). There appear to also # be 2 bytes indicating the length of the point as encoded, but # Decoder takes care of that. encoded = self.key.encode() _, _, points = encoding.Decoder(encoded).int(8).int(2).point(2).out() # '04' indicates that the points are in uncompressed form. hexifiedBitString = "'%s%s%s'H" % ('04', longToEvenLengthHexString(points[0]), longToEvenLengthHexString(points[1])) subjectPublicKey = univ.BitString(hexifiedBitString) spki['subjectPublicKey'] = subjectPublicKey return spki