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 testDerConversion(self): privateKey = PrivateKey() message = "This is a text message" signature1 = Ecdsa.sign(message, privateKey) der = signature1.toDer() signature2 = Signature.fromDer(fromLatin(der)) self.assertEqual(signature1.r, signature2.r) self.assertEqual(signature1.s, signature2.s)
def testBase64Conversion(self): privateKey = PrivateKey() message = "This is a text message" signature1 = Ecdsa.sign(message, privateKey) base64 = signature1.toBase64() signature2 = Signature.fromBase64(base64) self.assertEqual(signature1.r, signature2.r) self.assertEqual(signature1.s, signature2.s)
def testDerConversion(self): privateKey = PrivateKey() message = "This is a text message" signature1 = Ecdsa.sign(message, privateKey) der = signature1.toDer(withRecoveryId=True) signature2 = Signature.fromDer(toBytes(der), recoveryByte=True) self.assertEqual(signature1.r, signature2.r) self.assertEqual(signature1.s, signature2.s) self.assertEqual(signature1.recid, signature2.recid)
def testBase64Conversion(self): privateKey = PrivateKey() message = "This is a text message" signature1 = Ecdsa.sign(message, privateKey) base64 = signature1.toBase64(withRecoveryId=True) signature2 = Signature.fromBase64(base64, recoveryByte=True) self.assertEqual(signature1.r, signature2.r) self.assertEqual(signature1.s, signature2.s) self.assertEqual(signature1.recid, signature2.recid)
def verify(dictionary_msg, signature, sender_public_key): vk, _ = ppk_get_back_object(public_key=sender_public_key) if config.DIGITAL_SIGNATURE_ALGO == 'ECDSA': h = str(dictionary_msg) check = Ecdsa.verify(h, Signature.fromPem(signature), vk) # True # log_info("[security.digital_signature.verify] ECDSA Verify result: {}".format(check)) return check elif config.DIGITAL_SIGNATURE_ALGO == 'SCHNORR': h = dict_to_hash(dictionary_msg) check = schnorr_verify(h, vk, signature) # log_info("[security.digital_signature.verify] Schnorr Verify result: {}".format(check)) return check else: log_error("[security.digital_signature.verify] Unknown DSA in config -- cannot verify signature!")
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 verify_signature(self, payload, signature, timestamp, public_key=None): """ Verify signed event webhook requests. :param payload: event payload in the request body :type payload: string :param signature: value obtained from the 'X-Twilio-Email-Event-Webhook-Signature' header :type signature: string :param timestamp: value obtained from the 'X-Twilio-Email-Event-Webhook-Timestamp' header :type timestamp: string :param public_key: elliptic curve public key :type public_key: PublicKey :return: true or false if signature is valid """ timestamped_payload = timestamp + payload decoded_signature = Signature.fromBase64(signature) key = public_key or self.public_key return Ecdsa.verify(timestamped_payload, decoded_signature, 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 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))