def check_private_key(pem): from ellipticcurve.privateKey import PrivateKey try: assert PrivateKey.fromPem(pem).curve.name == "secp256k1" except: raise Exception( "Private-key must be valid secp256k1 ECDSA string in pem format") return pem
def signMessage(): values = request.get_json() print(values.get('message')) message = hash(values.get('message')) print("#####" + values.get('privateKey') + "#####") privateKey = PrivateKey.fromPem((values.get('privateKey'))) print(privateKey) response = sign_tx(privateKey, message) return jsonify(response.toBase64()), 200
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 sign(self, id): application = Application.objects( Q(id=id) & Q(assignedId=get_jwt_identity()['_id']['$oid'])).get() if application.to_hash() != application.hash: return 'Data Tampered', 403 current_stage = int(application.stage) private_key = User.objects( Q(id=get_jwt_identity()['_id']['$oid'])).get().private_key signatures = application.signatures signatures[current_stage] = Ecdsa.sign( json.dumps(application.to_hash()), PrivateKey.fromPem(private_key)).toBase64() application.update(signatures=signatures) if application.stage == application.stages - 1: application.update(stage=current_stage + 1) application.update(status=1) else: workflow = Workflow.objects(id=application.workflowId).get() new_auth_id = workflow.stages[current_stage + 1]['authId'] new_auth_name = workflow.stages[current_stage + 1]['authName'] application.update(assignedId=new_auth_id) application.update(assignedName=new_auth_name) application.update(stage=current_stage + 1) user = User.objects(Q(id=application.creatorId)).get() send_email_async( get_user_email(), 'notification', get_user_name(), notif= f"You have successfully signed {application.name} created by {user.first_name} with " f"your digital signatures") send_email_async( user.email, 'notification', user.first_name, notif= f"{get_user_name()} has successfully signed your {application.name}. Please check " f"E-Daftar portal for more updates.") return signatures[current_stage], 200
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 private_key(self): return PrivateKey.fromPem(self.pem)
def loadKey(self, key_str): self.privateKey = PrivateKey.fromPem(key_str)
from ellipticcurve.ecdsa import Ecdsa from ellipticcurve.privateKey import PrivateKey # Generate new Keys privateKey = PrivateKey.fromPem(""" -----BEGIN EC PARAMETERS----- BgUrgQQACg== -----END EC PARAMETERS----- -----BEGIN EC PRIVATE KEY----- MHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK oUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB RmpeRREXj5aog/Mq8RrdYy75W9q/Ig== -----END EC PRIVATE KEY----- """) publicKey = privateKey.publicKey() message = "1345890371588410854.30135125cargo_info" # Generate Signature signature = Ecdsa.sign(message, privateKey) # To verify if the signature is valid print(Ecdsa.verify(message, signature, publicKey)) #privateKey = '8d1f80536cd8df3948f4a0d54565f0b65dc8a9f8969fb979a2f21ab1f21e05' #publicKey = '0404fb2416c38f8e0e4790973d6cfcae0bffd02db79f651ecba976f55e84406d49218d39cb1adee8a3a911ddfe0fae85491e990d48a8ce451224ab32143c8ac736' #message = "1345890371588410854.30135125cargo_info"
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 testPemConversion(self): privateKey1 = PrivateKey() pem = privateKey1.toPem() privateKey2 = PrivateKey.fromPem(pem) self.assertEqual(privateKey1.secret, privateKey2.secret) self.assertEqual(privateKey1.curve, privateKey2.curve)
def hash(dictionnary): """ Creates a SHA-256 hash of a dict :param block: <dict> :return: <str> """ # We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes dictionnary_string = json.dumps(dictionnary, sort_keys=True).encode() return hashlib.sha256(dictionnary_string).hexdigest() basePrivateKey = PrivateKey.fromPem( '-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEID6TypbwEfEwdW0vgC7C4ObnBlGWmGW7avmg1QK710bfoAcGBSuBBAAK\noUQDQgAE7MTrJ3EZkwF/cz/Hv9OmmK1kI3oRQ4owzqZ0wDQaqMkCSaoNdDgN6Hvj\n38E0VbwZ0cuEnQmuhMjxBJ61EHwiJQ==\n-----END EC PRIVATE KEY-----\n' ) app = Flask(__name__) cors = CORS(app) app.config['CORS_HEADERS'] = 'Content-Type' app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False @app.route('/keys', methods=['POST']) @cross_origin() def getKeys(): basejson = request.get_json() base = basejson.get('base') # base = request.get_json().get('base')