def test_sign(self): private_key = get_key('../../priv/private.pem') for test_case in self.valid_test_cases: signature = sign(private_key, test_case['input']) self.assertEqual(signature, test_case['expected'], "shoud be equal")
def register_listing(config, signed_asset, listing): """Digitally signs the given listing, storing it on the listings service. config - the configuration to read the private key used for digital signatures from as well as the listings service URL. signed_asset - the digitally signed asset that is a part of the listing. listing - the listing to register in JSON format. Returns the signed listing. Throws an exception if something nasty happens. """ # Fill out the config-based information in the given listing storage_url = config.get("general", "listings-url") + listing["id"] # populate the listing populated_listing = populate_listing(config, signed_asset, listing) # include the default context if necessary populated_listing.setdefault("@context", constants.CONTEXT) # Digitally sign the listing sl = signature.sign(config, populated_listing) # Upload the listing req = urllib2.Request(storage_url, headers={"Content-Type": "application/ld+json"}, data=json.dumps(sl, sort_keys=True, indent=2)) urllib2.urlopen(req) return sl
def register_asset(config, asset): """Digitally signs the given asset and stores it on the listings service. config - the configuration to read the private key used for digital signatures from as well as the listings service URL. asset - the asset to register in JSON format. Returns the digitally signed asset. Throws an exception if something nasty happens. """ storage_url = config.get("general", "listings-url") + asset["id"] # fill out the config-based information in the asset populated_asset = populate_asset(config, asset) # include the default context if necessary populated_asset.setdefault("@context", constants.CONTEXT) # digitally sign the asset sa = signature.sign(config, populated_asset) # upload the asset req = urllib2.Request(storage_url, headers={"Content-Type": "application/ld+json"}, data=json.dumps(sa, sort_keys=True, indent=3)) urllib2.urlopen(req) return sa
def register_asset(config, asset): """Digitally signs the given asset and stores it on the listings service. config - the configuration to read the private key used for digital signatures from as well as the listings service URL. asset - the asset to register in JSON format. Returns the digitally signed asset. Throws an exception if something nasty happens. """ storage_url = config.get("general", "listings-url") + asset["id"] # fill out the config-based information in the asset populated_asset = populate_asset(config, asset) # include the default context if necessary populated_asset.setdefault("@context", constants.CONTEXT) # digitally sign the asset sa = signature.sign(config, populated_asset) # upload the asset req = urllib2.Request( storage_url, headers={"Content-Type": "application/ld+json"}, data=json.dumps(sa, sort_keys=True, indent=3) ) urllib2.urlopen(req) return sa
def register_listing(config, signed_asset, listing): """Digitally signs the given listing, storing it on the listings service. config - the configuration to read the private key used for digital signatures from as well as the listings service URL. signed_asset - the digitally signed asset that is a part of the listing. listing - the listing to register in JSON format. Returns the signed listing. Throws an exception if something nasty happens. """ # Fill out the config-based information in the given listing storage_url = config.get("general", "listings-url") + listing["id"] # populate the listing populated_listing = populate_listing(config, signed_asset, listing) # include the default context if necessary populated_listing.setdefault("@context", constants.CONTEXT) # Digitally sign the listing sl = signature.sign(config, populated_listing) # Upload the listing req = urllib2.Request( storage_url, headers={"Content-Type": "application/ld+json"}, data=json.dumps(sl, sort_keys=True, indent=2) ) urllib2.urlopen(req) return sl
def __init__(self, v, X1, X2): # Sanity checks if not isinstance(v, Scalar): raise TypeError('Invalid coin value!') if not isinstance(X1, Point) or not isinstance(X2, Point): raise TypeError('Invalid destination address!') #r = random_scalar() # recovery private key r = Scalar(5) # Minted coin data self.coin = Coin(v, X1, X2, r, 1) self.v = v # Prove the committed value is valid mask = hash_to_scalar('mask', hash_to_scalar(r * X2, 1)) self.proof = signature.sign(hash_to_scalar(self.coin), mask, Gc)
def sign_cli(key, data): r, s = sign(data, key) return ' '.join([hex(r), hex(s)])
from encode import encode,decode import signature # This is the test data to sign data = 'test signed data' # Generate a PyCrypto RSA key using signature module key = signature.generate_key() # Sign the test data with the key sig = signature.sign(key, data) # Dump encoded versions of the keys and data to the console print 'private', signature.key_to_string(key) print 'public', signature.key_to_string(key.publickey()) print 'address', signature.public_key_to_address(key.publickey()) print 'data', data print 'signature', sig, '\n' # Test verification code print 'call to verify() with legit data\t\t\t%s\t(Should be True)' % signature.verify(key, sig, data) print 'call of verify() with tampered data\t\t\t%s\t(Should be False)' % key.verify(data+'asdf',(decode(encode(sig),type='long'),None))
def sign(self, private): message = self.__gather() newsig = signature.sign(message, private) self.sigs.append(newsig)
def RSA(action): if action == '2': title('RSA keygen', fillchar='-') print() print() print('Select directory for the keys.') Tk().withdraw() while True: outfolder = askdirectory( initialdir=cwd, title='Select directory to save keys in...') if not outfolder: print('Please choose a directory.') else: if not os.path.exists(outfolder): os.makedirs(outfolder, exist_ok=True) break bits = int(input('Size of the key (default is 2048): ') or 2048) rsa.generate(bits, outfolder) elif action == '3': title('RSA Encryption / Decryption', fillchar='-') [print() for i in range(5)] print('Do you want to...') print('1. Encrypt a file') print('2. Decrypt a file') print('3. Exit') print('_' * utils.get_terminal_size()[0]) print() action = input('>> ').lower() if action == '1': title('RSA Encryption / Decryption', fillchar='-') print() print() print('Select a file to encrypt in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to encrypt...') print(filename) filedir = os.path.dirname(filename) print('Select the public key to encrypt the file with.') Tk().withdraw() keypath = askopenfilename(initialdir=cwd, title='Choose a public key...') print(keypath) keydir = os.path.dirname(keypath) print('Select the name for the encrypted file.') Tk().withdraw() outfile = asksaveasfilename(initialdir=filedir, title='Save as...') print('Select the name for the encrypted key.') Tk().withdraw() outkeyfile = asksaveasfilename(initialdir=filedir, title='Save as...') print(outfile) chunksize = input( 'Select chunksize (leave empty for default): ') or 64 * 1024 rsa.encrypt(keypath, filename, outfile, outkeyfile, chunksize) elif action == '2': title('RSA Encryption / Decryption', fillchar='-') print() print() print('Select a file to decrypt in the dialog box.') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Choose a file to decrypt...') print(filename) filedir = os.path.dirname(filename) print('Select the private key to decrypt the file with.') Tk().withdraw() keypath = askopenfilename(initialdir=cwd, title='Choose a private key...') print(keypath) keydir = os.path.dirname(keypath) print('Select the encrypted key file used to encrypt the file.') Tk().withdraw() keyfilepath = askopenfilename( initialdir=filedir, title='Choose the encrypted key file...') print(keyfilepath) print('Select the name for the decrypted file.') Tk().withdraw() outfile = asksaveasfilename(initialdir=filedir, title='Save as...') print(outfile) chunksize = input( 'Select chunksize (leave empty for default): ') or 24 * 1024 rsa.decrypt(keypath, filename, keyfilepath, outfile, chunksize) elif action == '3': exit(0) elif action == '4': title('RSA Signature / verification', fillchar='-') [print() for i in range(5)] print('Do you want to...') print('1. Sign a file') print('2. Verify a file') print('3. Exit') print('_' * utils.get_terminal_size()[0]) print() action = input('>> ').lower() if action == '1': title('RSA Signature / verification', fillchar='-') print() print() print('Select file to sign...') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Select file to sign...') print(filename) filedir = os.path.dirname(filedir) print('Select private key...') Tk().withdraw() privKey = askopenfilename(initialdir=cwd, title='Select private key...') print(privKey) print('Select name for signature file...') signature = asksaveasfilename( initialdir=filedir, title='Select signature filename...') or None print(signature) sign(filename, privKey, signature) elif action == '2': title('RSA Signature / verification', fillchar='-') print() print() print('Select file to verify...') Tk().withdraw() filename = askopenfilename(initialdir=cwd, title='Select file to verify...') print(filename) filedir = os.path.dirname(filename) print('Select public key...') Tk().withdraw() pubKey = askopenfilename(initialdir=cwd, title='Select public key...') print(pubKey) print('Select signature file...') signature = askopenfilename(initialdir=filedir, title='Select signature file...') print(signature) valid = verify(filename, signature, pubKey) if valid: print( 'Success! Signature and hash are the same, so the file has not been tampered with.' ) _tmp = input('Press enter to continue...') elif not valid: clear() print('FILE AUTHENTICITY COULD NOT BE VERIFIED!') print( 'Do not trust the file or its sender. Did you use the correct public key?' ) print( 'Error: Verification failed. Authenticity of file could not be verified.' ) elif action == '3': pass else: TypeError('invalid action: \'%s\' in RSA()')
message = "Hello World" print("Message: {}".format(message)) # Set the key length # 128, 196 or 256 N = 256 # Default rounds is 16 rounds as the TwoFish paper define to us. rounds = 16 # Generate Key and ... - TwoFish [K, S] = TwoFish.gen_keys(str(bob_key), N, rounds) bob_sig_key = (str((K[:1])[0]).encode('utf-8')).zfill(16) # Digital Signature bob_digest = sign(message, bob_sig_key) # Alice encrypt plaintext [num_C, Cypher_text] = TwoFish.encrypt_message(message, S, K, rounds=16) print("Enc Message: {}\n Bob digest {}".format(Cypher_text, bob_digest)) # Alice want to know the message # Generate Key and ... - TwoFish [K, S] = TwoFish.gen_keys(str(alice_key), N, rounds) plain_text = TwoFish.decrypt_message(num_C, S, K, rounds=16) alice_sig_key = (str((K[:1])[0]).encode('utf-8')).zfill(16) signature_status = verify(plain_text, alice_sig_key, bob_digest) if signature_status: print(plain_text) else: print("Failed")
def sign_transaction(data, key): return Transaction(data, signature.sign(key, data), signature.public_key_to_address(key.publickey()))
import signature keys = signature.generatePairKey() priv = split(keys, 1) pub = signature.split(keys, 0) signature.sign( priv, "5a822196bf1c45b1e74c6d04e4127d0296d64695932f40909bdab3ba8a9b0528") bool = signature.verify( pub, "5a822196bf1c45b1e74c6d04e4127d0296d64695932f40909bdab3ba8a9b0528", sig) print(bool)