def private_key_to_public_key_add(public_key_class, curve): """ Accept a hex private key and convert it to its respective public key. Because converting a private key to a public key requires SECP256k1 ECDSA signing, this function is the most time consuming and is a bottleneck in the overall speed of the program. Average Time: 0.0031567731 seconds """ publicPoint = add(p=curve.G, q=public_key_class.point, A=curve.A, P=curve.P) public_key_class = PublicKey(x=publicPoint.x, y=publicPoint.y, curve=curve) return '04' + public_key_class.toString().hex().upper(), public_key_class
def testStringConversion(self): privateKey = PrivateKey() publicKey1 = privateKey.publicKey() string = publicKey1.toString() publicKey2 = PublicKey.fromString(fromLatin(string)) self.assertEqual(publicKey1.point.x, publicKey2.point.x) self.assertEqual(publicKey1.point.y, publicKey2.point.y) self.assertEqual(publicKey1.curve, publicKey2.curve)
def testDerConversion(self): privateKey = PrivateKey() publicKey1 = privateKey.publicKey() der = publicKey1.toDer() publicKey2 = PublicKey.fromDer(fromLatin(der)) self.assertEqual(publicKey1.point.x, publicKey2.point.x) self.assertEqual(publicKey1.point.y, publicKey2.point.y) self.assertEqual(publicKey1.curve, publicKey2.curve)
def _verify_signature(content, signature, user=None, refresh=False): signature = Signature.fromBase64(signature) public_key = cache.get("starkbank-public-key") if public_key is None or refresh: pem = _get_public_key_pem(user) public_key = PublicKey.fromPem(pem) cache["starkbank-public-key"] = public_key return Ecdsa.verify(message=content, signature=signature, publicKey=public_key)
def testPemConversion(self): privateKey = PrivateKey() publicKey1 = privateKey.publicKey() pem = publicKey1.toPem() publicKey2 = PublicKey.fromPem(pem) self.assertEqual(publicKey1.point.x, publicKey2.point.x) self.assertEqual(publicKey1.point.y, publicKey2.point.y) self.assertEqual(publicKey1.curve, publicKey2.curve)
def generate_wallet_address(public_key): """ Generates a wallet's address from public key :param public_key: :return: """ address = (hashlib.sha256( PublicKey.fromPem(public_key).toDer().encode()).hexdigest())[0:8] return address
def convert_public_key_to_ecdsa(self, public_key): """ Convert the public key string to a ECPublicKey. :param public_key: verification key under Mail Settings :type public_key string :return: public key using the ECDSA algorithm :rtype PublicKey """ return PublicKey.fromPem(public_key)
def testVerifySignature(self): # openssl ec -in privateKey.pem -pubout -out publicKey.pem publicKeyPem = File.read("./publicKey.pem") # openssl dgst -sha256 -sign privateKey.pem -out signature.binary message.txt signatureDer = File.read("./signatureDer.txt", "rb") message = File.read("./message.txt") publicKey = PublicKey.fromPem(publicKeyPem) signature = Signature.fromDer(string=signatureDer) self.assertTrue(Ecdsa.verify(message=message, signature=signature, publicKey=publicKey))
def valid_signature(self, transaction): """ Validates the transaction signature : was the transaction really created by the sender ? :param transaction: <dict> :return: <bool> """ transaction_copy = transaction.copy() # les dictionnaires sont passés par référence signature = transaction_copy.pop("signature") ecdsa_signature = ellipticcurve.signature.Signature.fromBase64(signature) key = transaction_copy['sender'] ecdsa_key = PublicKey.fromPem(key) try: return Ecdsa.verify(self.hash(transaction_copy), ecdsa_signature, ecdsa_key) except: return False
def ppk_get_back_object(public_key=None, private_key=None): if config.DIGITAL_SIGNATURE_ALGO == 'ECDSA': if private_key: private_key = PrivateKey.fromPem(private_key) if public_key: public_key = PublicKey.fromPem(public_key) elif config.DIGITAL_SIGNATURE_ALGO == 'SCHNORR': if private_key: private_key = hex_to_bytes(private_key) if public_key: public_key = hex_to_bytes(public_key) else: log_error( "[security.ppk_keygen.ppk_get_back_object] Unknown keygen algo defined" ) return public_key, private_key
def verify_Signature(message, signature_input, publicKey_input): publicKey_input = '-----BEGIN PUBLIC KEY-----\n' + publicKey_input + '\n-----END PUBLIC KEY-----\n' signature = Signature.fromBase64(signature_input) publicKey = PublicKey.fromPem(publicKey_input) return (Ecdsa.verify(message, signature, publicKey))
def verify_Signature(message, sign64, str1): signature = Signature.fromBase64(sign64) publicKey = PublicKey.fromPem(str1) return (Ecdsa.verify(message, signature, publicKey))
def serialize_public_key(public_key): """ Serializes public key """ return PublicKey.toPem(public_key)
from json import dumps from ellipticcurve.ecdsa import Ecdsa from ellipticcurve.privateKey import PrivateKey from ellipticcurve.ecdsa import Ecdsa from ellipticcurve.signature import Signature from ellipticcurve.publicKey import PublicKey from ellipticcurve.utils.file import File from sendgrid.helpers.eventwebhook import EventWebhook import requests publicKeyPem = File.read("publicKey.pem") publicKey = PublicKey.fromPem(publicKeyPem) privateKeyPem = File.read("privateKey.pem") privateKey = PrivateKey.fromPem(privateKeyPem) def send_mock_webhook(payload, timestamp): message = timestamp + payload signature = Ecdsa.sign(message, privateKey) base64_signature = signature.toBase64() # print("Verified:", Ecdsa.verify(message, signature, publicKey)) url = 'https://cryptic-caverns-35958.herokuapp.com/mock_webhook' headers = { 'x-twilio-email-event-webhook-timestamp': timestamp, 'x-twilio-email-event-webhook-signature': base64_signature } data = payload.encode('utf-8') response = requests.post(url, headers=headers, data=data)
def verify_Signature(message, sign64, publicKey_input): signature = Signature.fromBase64(sign64) a = publicKey_input.split(".") pt = Point(int(a[0]), int(a[1]), int(a[2])) publicKey = PublicKey(point = pt, curve = secp256k1) return (Ecdsa.verify(message, signature, publicKey))
def get_public_key(self): return PublicKey.fromPem(self.public_key)
def deserialize_public_key(public_key): """ Deserializes public key """ return PublicKey.fromPem(public_key)