def build_profile(policy): print("{0} => Profiling with policy {1}".format(time.strftime("%Y-%m-%d %H:%M:%S"), policy)) p = Profile(name=config.get("archive", "name"), description=config.get("archive", "description"), homepage=config.get("archive", "homepage"), accesspoint=config.get("archive", "accesspoint"), memento_compliance=config.get("archive", "memento_compliance"), timegate=config.get("archive", "timegate"), timemap=config.get("archive", "timemap"), established=config.get("archive", "established"), profile_updated=time.strftime("%Y-%m-%dT%H:%M:%SZ"), source="cdx", type="urikey#{0}".format(policy)) opkfn = "intermediate-{0}.keys".format(policy) opkfpath = os.path.join(tempdir, opkfn) print("{0} => Storing intermediate keys in {1}".format(time.strftime("%Y-%m-%d %H:%M:%S"), opkfpath)) opkf = open(opkfpath, "w") kg = KeyGenerator(policy) kg.generate_keys_from_files(sys.argv[1:], opkf) opkf.close() oppfn = "profile-{0}.cdxj".format(policy) oppfpath = os.path.join(profiledir, oppfn) print("{0} => Generating profile in {1}".format(time.strftime("%Y-%m-%d %H:%M:%S"), oppfpath)) oppf = open(oppfpath, "w") preamble = "@context https://oduwsdl.github.io/contexts/archiveprofile.jsonld\n@id {0}\n".format(p.about["homepage"]) pabout = json.dumps(p.about, sort_keys=True) oppf.write(preamble + "@about " + pabout + "\n") oppf.close() os.system('LC_ALL=C sort ' + opkfpath + ' | uniq -c | awk \'{ print $2 " {\\"frequency\\": "$1", \\"spread\\": 1}" }\' >> ' + oppfpath) print("{0} => Storing compressed profile in {1}".format(time.strftime("%Y-%m-%d %H:%M:%S"), oppfpath + ".gz")) os.system('gzip -c ' + oppfpath + ' > ' + oppfpath + '.gz')
def build_profile(policy): print("Profiling {0} with Policy: {1}".format(collection, policy)) profile_id = "{0}-{1}".format(col_id, policy) profiling_start = time.time() p = Profile(name="{0} Policy {1}".format(collection, policy), description="{0} collection profile with policy {1}.".format(collection, policy), homepage="http://www.webarchive.org.uk/ukwa/", accesspoint="http://www.webarchive.org.uk/wayback/", memento_compliance="https://oduwsdl.github.io/terms/mementosupport#native", timegate="http://www.webarchive.org.uk/wayback/archive/", timemap="http://www.webarchive.org.uk/wayback/archive/timemap/link/", established="2004", profile_updated=time.strftime("%Y-%m-%dT%H:%M:%SZ"), mechanism="https://oduwsdl.github.io/terms/mechanism#cdx", policy="https://oduwsdl.github.io/terms/policy#{0}".format(policy)) opkfn = "intermediate-{0}.keys".format(profile_id) opkfpath = os.path.join(bmdir, opkfn) opkf = open(opkfpath, "w") kg = KeyGenerator(policy) kg.generate_keys_from_files(sys.argv[1:], opkf) opkf.close() key_generation_done = time.time() oppfn = "profile-{0}.cdxj".format(profile_id) oppfpath = os.path.join(bmdir, oppfn) oppf = open(oppfpath, "w") pabout = json.dumps(p.about, sort_keys=True) oppf.write("@about " + pabout + "\n") oppf.close() os.system('LC_ALL=C sort ' + opkfpath + ' | uniq -c | awk \'{ print $2 " {\\"frequency\\": "$1", \\"spread\\": 1}" }\' >> ' + oppfpath) profile_generation_done = time.time() os.system('gzip -c ' + oppfpath + ' > ' + oppfpath + '.gz') profile_compression_done = time.time() bm = { "profile": oppfn, "collection": col_id, "policy": policy, "cdx_size": cdx_size, "extract_size": extract_size, "profile_size": os.path.getsize(oppfpath), "profile_size_compressed": os.path.getsize(oppfpath + ".gz"), "urir_count": urir_count, "urim_count": urim_count, "keys_count": file_len(oppfpath), "key_generation_time": key_generation_done - profiling_start, "profile_generation_time": profile_generation_done - key_generation_done, "profile_compression_time": profile_compression_done - profile_generation_done, "profiling_time": profile_generation_done - profiling_start } all_bms["bms"][policy] = bm jsonstr = json.dumps(bm, sort_keys=True, indent=4, separators=(",", ": ")) opf = "bm-{0}.json".format(profile_id) opfpath = os.path.join(bmdir, opf) write_json(jsonstr, filepath=opfpath)
def lambda_handler(event, context): try: print('REQUEST_EVENT {}\n'.format(str(event))) """ init """ cache = Cache() """ validation """ validator = SpekeRequestValidator(event['body'], event['isBase64Encoded']) if validator.fail(): raise SyntaxError('Please check your request') body = validator.get_body() content_id = validator.get_content_id() key_id = validator.get_key_id() print('BODY {}\n'.format(body)) print('CONTENT_ID {}\n'.format(content_id)) print('KID {}\n'.format(key_id)) """ Generate KeyBytes """ Generator = KeyGenerator(cache) key_bytes, derived_url = Generator.hls_aes_128(content_id, key_id) print('KEY_BYTES {}\n'.format(key_bytes)) print('URL {}\n'.format(derived_url)) """ Generate response body """ response = SpekeResponseBuilder(body, content_id, key_id, key_bytes, derived_url).get_response() return response except Exception as exception: t, v, tb = sys.exc_info() print('EXCEPTION {}\n'.format(str(exception))) print(traceback.format_exception(t, v, tb)) print(exception.__traceback__) return { 'isBase64Encoded': False, 'statusCode': 500, 'headers': { 'Content-Type': 'text/plain' }, 'body': str(exception) }
def server_handler(event, context): """ This function is the entry point for the SPEKE reference key server Lambda. This is invoked from the API Gateway resource. """ try: print(event) body = event['body'] if event['isBase64Encoded']: body = base64.b64decode(body) cache = KeyCache(BUCKET_NAME, CLIENT_URL_PREFIX) generator = KeyGenerator() response = ServerResponseBuilder(body, cache, generator).get_response() print(response) return response except Exception as exception: print("EXCEPTION {}".format(exception)) return { "isBase64Encoded": False, "statusCode": 500, "headers": { "Content-Type": "text/plain" }, "body": str(exception) }
def build_profile(policy): print("{0} => Profiling with policy {1}".format( time.strftime("%Y-%m-%d %H:%M:%S"), policy)) p = Profile(name=config.get("archive", "name"), description=config.get("archive", "description"), homepage=config.get("archive", "homepage"), accesspoint=config.get("archive", "accesspoint"), memento_compliance=config.get("archive", "memento_compliance"), timegate=config.get("archive", "timegate"), timemap=config.get("archive", "timemap"), established=config.get("archive", "established"), profile_updated=time.strftime("%Y-%m-%dT%H:%M:%SZ"), source="cdx", type="urikey#{0}".format(policy)) opkfn = "intermediate-{0}.keys".format(policy) opkfpath = os.path.join(tempdir, opkfn) print("{0} => Storing intermediate keys in {1}".format( time.strftime("%Y-%m-%d %H:%M:%S"), opkfpath)) opkf = open(opkfpath, "w") kg = KeyGenerator(policy) kg.generate_keys_from_files(sys.argv[1:], opkf) opkf.close() oppfn = "profile-{0}.cdxj".format(policy) oppfpath = os.path.join(profiledir, oppfn) print("{0} => Generating profile in {1}".format( time.strftime("%Y-%m-%d %H:%M:%S"), oppfpath)) oppf = open(oppfpath, "w") preamble = "@context https://oduwsdl.github.io/contexts/archiveprofile.jsonld\n@id {0}\n".format( p.about["homepage"]) pabout = json.dumps(p.about, sort_keys=True) oppf.write(preamble + "@about " + pabout + "\n") oppf.close() os.system( 'LC_ALL=C sort ' + opkfpath + ' | uniq -c | awk \'{ print $2 " {\\"frequency\\": "$1", \\"spread\\": 1}" }\' >> ' + oppfpath) print("{0} => Storing compressed profile in {1}".format( time.strftime("%Y-%m-%d %H:%M:%S"), oppfpath + ".gz")) os.system('gzip -c ' + oppfpath + ' > ' + oppfpath + '.gz')
def do_swnew(self, args): """swnew [HRP] - generate new private key and public address\nswnew [HRP] [seed] - generate key with text seed\n HRP - Human Readable Part stands for type of network for which you want to create new address:\n BC - Bitcoin Network\n TB - Testnet Network\n""" usage = 'swnew [HRP] - generate new private key and public address\nswnew [HRP] [seed] - generate key with text seed\n' \ 'HRP - Human Readable Part stands for type of network for which you want to create new address:\n' \ '0) BC - Bitcoin Network\n' \ '1) TB - Testnet Network\n' keygen = KeyGenerator() splitted = args.split(' ') if len(list(splitted)) < 1 or splitted[0] not in ('BC', 'TB'): prRed("Wrong parameters!") prCyan(usage) return if splitted[0] == 'BC': network = NETWORKS.BITCOIN elif splitted[0] == 'TB': network = NETWORKS.TESTNET else: prRed("How i didn't found it in usage check? ") return for arg in args: keygen.seed_input(arg) private_key = keygen.generate_key() while len(private_key) != 64: private_key = KeyGenerator().generate_key() prPurple('raw private key:') prLightPurple(private_key) prPurple('segwit address:') swaddress = wallet.Wallet.bech32_addr_from_privkey( private_key, network) prLightPurple(swaddress) # Write new address to file f = open(WALLET_SEGWIT_ADDRESS_FILE, 'w') f.write(swaddress) f.close() privkey_wif = wallet.Wallet.priv_to_WIF(private_key) # Write privkey in WIF to file f = open(WALLET_SEGWIT_PRIVKEY_FILE, 'w') f.write(privkey_wif) f.close()
def __create_priv_key(self, seed=None, key_to_file=False): keygen = KeyGenerator() if seed: keygen.seed_input(seed) key = keygen.generate_key() while len(key) != 64: key = KeyGenerator().generate_key() if key_to_file: f = open(WALLET_PRIVKEY_FILE, "w+") f.write(key) f.close() else: print(key) del key del keygen
def do_new(self, args): """new - generate new private key and public address\nnew [seed] - generate key with text seed""" keygen = KeyGenerator() if args: for arg in args: keygen.seed_input(arg) private_key = keygen.generate_key() while len(private_key) != 64: private_key = KeyGenerator().generate_key() prPurple('private key:') prLightPurple(private_key) prPurple('address:') address = w.private_key_to_addr(private_key) prLightPurple(address) f = open(WALLET_ADDRESS_FILE, 'w') f.write(address) f.close()
def build_profile(policy): print("Profiling {0} with Policy: {1}".format(collection, policy)) profile_id = "{0}-{1}".format(col_id, policy) profiling_start = time.time() p = Profile( name="{0} Policy {1}".format(collection, policy), description="{0} collection profile with policy {1}.".format( collection, policy), homepage="http://www.webarchive.org.uk/ukwa/", accesspoint="http://www.webarchive.org.uk/wayback/", memento_compliance= "https://oduwsdl.github.io/terms/mementosupport#native", timegate="http://www.webarchive.org.uk/wayback/archive/", timemap="http://www.webarchive.org.uk/wayback/archive/timemap/link/", established="2004", profile_updated=time.strftime("%Y-%m-%dT%H:%M:%SZ"), mechanism="https://oduwsdl.github.io/terms/mechanism#cdx", policy="https://oduwsdl.github.io/terms/policy#{0}".format(policy)) opkfn = "intermediate-{0}.keys".format(profile_id) opkfpath = os.path.join(bmdir, opkfn) opkf = open(opkfpath, "w") kg = KeyGenerator(policy) kg.generate_keys_from_files(sys.argv[1:], opkf) opkf.close() key_generation_done = time.time() oppfn = "profile-{0}.cdxj".format(profile_id) oppfpath = os.path.join(bmdir, oppfn) oppf = open(oppfpath, "w") pabout = json.dumps(p.about, sort_keys=True) oppf.write("@about " + pabout + "\n") oppf.close() os.system( 'LC_ALL=C sort ' + opkfpath + ' | uniq -c | awk \'{ print $2 " {\\"frequency\\": "$1", \\"spread\\": 1}" }\' >> ' + oppfpath) profile_generation_done = time.time() os.system('gzip -c ' + oppfpath + ' > ' + oppfpath + '.gz') profile_compression_done = time.time() bm = { "profile": oppfn, "collection": col_id, "policy": policy, "cdx_size": cdx_size, "extract_size": extract_size, "profile_size": os.path.getsize(oppfpath), "profile_size_compressed": os.path.getsize(oppfpath + ".gz"), "urir_count": urir_count, "urim_count": urim_count, "keys_count": file_len(oppfpath), "key_generation_time": key_generation_done - profiling_start, "profile_generation_time": profile_generation_done - key_generation_done, "profile_compression_time": profile_compression_done - profile_generation_done, "profiling_time": profile_generation_done - profiling_start } all_bms["bms"][policy] = bm jsonstr = json.dumps(bm, sort_keys=True, indent=4, separators=(",", ": ")) opf = "bm-{0}.json".format(profile_id) opfpath = os.path.join(bmdir, opf) write_json(jsonstr, filepath=opfpath)
from key_generator import KeyGenerator from matrix_encryption import MatrixEncryption from matrix_decryption import MatrixDecryption from encryption_helper import EncryptionHelper import numpy as np import sys def not_found_message(): print("Value not found") raise Exception key_gen = KeyGenerator() factor = 10**3 message = 0.01243 message_scaled = round(message*factor) message1 = 0.00417 message1_scaled = round(message1*factor) retries = 0 is_not_found = True while is_not_found: try: secret_key = key_gen.generate_secret_key() encrypted_matrix = MatrixEncryption().encrypt_message(secret_key, message_scaled) count = 0 while True:
import initializer from key_generator import KeyGenerator from wallet import Wallet from miner_cli import MinerCli from wallet_cli import WalletCli # initializer.main() privkey_1 = KeyGenerator().generate_key() privkey_2 = KeyGenerator().generate_key() privkey_3 = KeyGenerator().generate_key() f = open('../miner_data/privkey_1', 'w+') f.write(Wallet.priv_to_WIF(privkey_1)) f.close() f = open('../miner_data/privkey_2', 'w+') f.write(Wallet.priv_to_WIF(privkey_2)) f.close() f = open('../miner_data/privkey_3', 'w+') f.write(Wallet.priv_to_WIF(privkey_3)) f.close() publkey_1 = Wallet.private_to_public(privkey_1) publkey_2 = Wallet.private_to_public(privkey_2) publkey_3 = Wallet.private_to_public(privkey_3) f = open('../miner_data/publkey_1', 'w+') f.write(publkey_1) f.close() f = open('../miner_data/publkey_2', 'w+') f.write(publkey_2)
from modulo import modulo class Encoder(AlphabetEncoder): def __init__(self, public_key): super().__init__() self.public_key = public_key self.pulverizer = Pulverizer() def encode(self, string): message = super().encode(string) gcd, _, _ = self.pulverizer.calculate_gcd(message, self.public_key[1]) if gcd != 1: raise Exception('Incorrect key. Generate another one.') # encoded_message = pow(message, self.public_key[0]) % self.public_key[1] encoded_message = modulo(message, self.public_key[0], self.public_key[1]) return encoded_message keyGenerator = KeyGenerator(size=1024, test=False) print('Public key:', keyGenerator.public_key, '\n') print('Private key:', keyGenerator.private_key, '\n') encoder = Encoder(keyGenerator.public_key) message = 'Some arbitrary string with spaces, commas (and exclamation marks)!' print('Original message:', message, '\n') encoded_message = encoder.encode(message) print('Encoded message:', encoded_message, '\n') print('Decoded message:', keyGenerator.decode(encoded_message))