def testVerifyRightMessage(self): privateKey = PrivateKey() publicKey = privateKey.publicKey() message = "This is the right message" signature = Ecdsa.sign(message, privateKey) self.assertTrue(Ecdsa.verify(message, signature, publicKey))
def testVerifyWrongMessage(self): privateKey = PrivateKey() publicKey = privateKey.publicKey() message1 = "This is the right message" message2 = "This is the wrong message" signature = Ecdsa.sign(message1, privateKey) self.assertFalse(Ecdsa.verify(message2, signature, publicKey))
def _is_valid(content, signature, public_key): if Ecdsa.verify(message=content, signature=signature, publicKey=public_key): return True normalized = dumps(loads(content), sort_keys=True) if Ecdsa.verify(message=normalized, signature=signature, publicKey=public_key): return True return False
def testZeroSignature(self): privateKey = PrivateKey() publicKey = privateKey.publicKey() message2 = "This is the wrong message" self.assertFalse(Ecdsa.verify(message2, Signature(0, 0), publicKey))
def testAssign(self): # Generated by: openssl ecparam -name secp256k1 -genkey -out privateKey.pem privateKeyPem = File.read("privateKey.pem") privateKey = PrivateKey.fromPem(privateKeyPem) message = File.read("message.txt") signature = Ecdsa.sign(message=message, privateKey=privateKey) publicKey = privateKey.publicKey() self.assertTrue( Ecdsa.verify(message=message, signature=signature, publicKey=publicKey))
def testDerConversion(self): privateKey = PrivateKey() message = "This is a text message" signature1 = Ecdsa.sign(message, privateKey) der = signature1.toDer() signature2 = Signature.fromDer(der) 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(der, recoveryByte=True) self.assertEqual(signature1.r, signature2.r) self.assertEqual(signature1.s, signature2.s) self.assertEqual(signature1.recoveryId, signature2.recoveryId)
def testMany(self): for _ in range(1000): privateKey1 = PrivateKey() publicKey1 = privateKey1.publicKey() privateKeyPem = privateKey1.toPem() publicKeyPem = publicKey1.toPem() privateKey2 = PrivateKey.fromPem(privateKeyPem) publicKey2 = PublicKey.fromPem(publicKeyPem) message = "test" signatureBase64 = Ecdsa.sign(message=message, privateKey=privateKey2).toBase64() signature = Signature.fromBase64(signatureBase64) self.assertTrue( Ecdsa.verify(message=message, signature=signature, publicKey=publicKey2))
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 TestOneInput(input_bytes): fdp = atheris.FuzzedDataProvider(input_bytes) privateKey1 = PrivateKey() publicKey1 = privateKey1.publicKey() privateKeyPem = privateKey1.toPem() publicKeyPem = publicKey1.toPem() privateKey2 = PrivateKey.fromPem(privateKeyPem) publicKey2 = PublicKey.fromPem(publicKeyPem) message = fdp.ConsumeUnicode(sys.maxsize) signatureBase64 = Ecdsa.sign(message=message, privateKey=privateKey2).toBase64() signature = Signature.fromBase64(signatureBase64) assert (Ecdsa.verify(message=message, signature=signature, publicKey=publicKey2))
def fetch(method, path, payload=None, query=None, user=None, version="v2"): user = check_user(user or starkbank.user) language = check_language(starkbank.language) url = { Environment.production: "https://api.starkbank.com/", Environment.sandbox: "https://sandbox.api.starkbank.com/", }[user.environment] + version url = "{base_url}/{path}{query}".format(base_url=url, path=path, query=urlencode(query)) agent = "Python-{major}.{minor}.{micro}-SDK-{sdk_version}".format( major=python_version.major, minor=python_version.minor, micro=python_version.micro, sdk_version=starkbank.version, ) access_time = str(time()) body = dumps(payload) if payload else "" message = "{access_id}:{access_time}:{body}".format( access_id=user.access_id(), access_time=access_time, body=body) signature = Ecdsa.sign(message=message, privateKey=user.private_key()).toBase64() try: request = method(url=url, data=body, headers={ "Access-Id": user.access_id(), "Access-Time": access_time, "Access-Signature": signature, "Content-Type": "application/json", "User-Agent": agent, "Accept-Language": language, }, timeout=starkbank.timeout) except Exception as exception: raise UnknownError("{}: {}".format(exception.__class__.__name__, str(exception))) response = Response(status=request.status_code, content=request.content) if response.status == 500: raise InternalServerError() if response.status == 400: raise InputErrors(response.json()["errors"]) if response.status != 200: raise UnknownError(response.content) return response
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.recoveryId, signature2.recoveryId)
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))