def _generate_serial(self): timestamp = time.time() # List of characters that will be excluded from the generator excluded_chars = ['O'] # List of characters that will be used by the generator extras = [c for c in string.ascii_uppercase if not c in excluded_chars] # Generate the key using the current timestamp as the seed key_custom = generate(3, SEPARATOR, LENGTH, LENGTH, type_of_value='int', capital='none', extras=extras, seed=timestamp).get_key().upper() # Generate the signature but only on a key cleared of separator char key_without_separator = key_custom.replace(SEPARATOR, '') key_signature = SHA256.SHA256Hash( key_without_separator.encode()).hexdigest() # We keep only the first values of the signature as a checksum key_checksum = key_signature[0:LENGTH].upper() # The checksum is added at the end of the key serial = '{}{}{}'.format(key_custom, SEPARATOR, key_checksum) return serial
def make_gateway_private_key_password(gateway_name, secret): """ Generate a unique gateway private key password. NOTE: its only as secure as the secret; the rest can be guessed by the adversary """ h = HashAlg.SHA256Hash() secret_hex = binascii.hexlify(secret) h.update("%s-%s" % (gateway_name, secret_hex)) return h.hexdigest()
def sender(): while True: randomvalue = randint(0, 2**30) newhash = hexlify( SHA256.SHA256Hash(f"{randomvalue}".encode()).digest()) for client in self.connected_clients: con = socket(AF_INET, SOCK_STREAM) con.connect((client[0], int(client[1]))) con.send(newhash) sleep(1)
def hash(timestamp, lastHash, data, nonce): return hexlify( SHA256.SHA256Hash(f"{timestamp}{lastHash}{data}{nonce}".encode()). digest()).decode()
hashers = { "md4": { "hashlib_hash": hashlib.new("md4"), "crypto_hash": MD4.MD4Hash() }, "md5": { "hashlib_hash": hashlib.new("md5"), "crypto_hash": MD5.MD5Hash() }, "sha224": { "hashlib_hash": hashlib.new("sha224"), "crypto_hash": SHA224.SHA224Hash() }, "sha256": { "hashlib_hash": hashlib.new("sha256"), "crypto_hash": SHA256.SHA256Hash() }, "sha384": { "hashlib_hash": hashlib.new("sha384"), "crypto_hash": SHA384.SHA384Hash() }, "sha512": { "hashlib_hash": hashlib.new("sha512"), "crypto_hash": SHA512.SHA512Hash() }, "sha1": { "hashlib_hash": hashlib.new("sha1"), "crypto_hash": SHA.SHA1Hash() }, "ripemd160": { "hashlib_hash": hashlib.new("ripemd160"),
return Bits(key.ljust(mySHA256.block_size // 8, b'\x00')) def HMAC_encode(key, message): key = HMAC_key(key) o_pad = key ^ Bits('0x' + '5c' * (mySHA256.block_size // 8)) i_pad = key ^ Bits('0x' + '36' * (mySHA256.block_size // 8)) a = o_pad.tobytes() + mySHA256(i_pad.tobytes() + message).bytes return mySHA256(a) if __name__ == '__main__': N = randint(0, 5120) message = urandom(N) print("Message: \n", message, '\n') buildin = SHA256.SHA256Hash(message).hexdigest() print("Crypto.Hash implementation: \n", buildin, '\n') sha = mySHA256(message) print("My implementation: \n", sha._encode().hex, '\n') print( "------------------------------AES-128 KEY GEN------------------------------------------" ) key = b"my good key" sha = mySHA256(key) print("Generatedd key: \n", sha.aes128_key_bytes, '\n') print("Hexdigest: \n", sha.aes128_key_hex_dec, '\n') print( "------------------------------------HMAC-----------------------------------------------" ) key_length = randint(0, 800) key = urandom(key_length)
def __init__(self, data, encode="utf-8"): self.encode = encode data_bytes = self._to_bytes(data, "data") self.hashed = SHA256.SHA256Hash(data_bytes)
def main(): print("TeslaCrypt Decryption Tool 0.2") #print("Emmanuel Tacheau and Andrea Allievi") print("Copyright (C) 2015 Talos Security Intelligence and Research Group") print("Cisco Systems Inc.\n") parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, epilog=dedent(''' Running for example : \npython TeslaDecrypt.py --fic abc.py.ecc --decrypt --key 04684d9d06102effe5cadd3b218d61e37a4c693b995a6cb76db2978a2dbfd2e2 should produce output like "Wrote decrypted file abc.py.ecc.dec" where aby.py.ecc.dec is the decrypted file ''')) parser.add_argument('--fic', type=argparse.FileType('rb'), help='Specify binary file to crypt or decrypt details') parser.add_argument('--decrypt', action='store_true', help='perform a decryption operation') parser.add_argument('--encrypt', action='store_true', help='perform an encryption operation') parser.add_argument('--test_mode', action='store_true', help='Test mode internal testing') parser.add_argument('--key', default=None, help='Specify a key string to use as key encryption') parser.add_argument( '--keyfile', default=None, type=argparse.FileType('rb'), help= 'Specify the \'key.dat\' file to be used to retrieve the master key') parser.add_argument('--iv', default=None, help='Specify IV value to be used') parser.add_argument( '--mode', choices=[AES.MODE_ECB, AES.MODE_CBC, AES.MODE_CFB], default=AES.MODE_CBC, help='Encryption mode to be used with MODE_ECB=1, MODE_CBC=2,MODE_CFB=3' ) args = parser.parse_args() checks = False cipher_key = None iv_key = None # E.T - 05/04/2015 # Fixed: cipher_key must be a binary data if args.key: cipher_key = binascii.unhexlify(args.key) encrypted = None cleardata = None # Get if I need to recover the key from the 'key.dat' file if (args.keyfile != None): # TeslaCrypt 3 Key file: # # PART 1: # + 0x00 - Base58 string of OS data used as Payment ID (size 0x28 - calculated @ 00401F60) # # PART 2: # + 0x64 - 0x20 bytes of a SHA1 key derived from the OS info (calculated @ 41B400) # + 0x84 - 0x20 bytes of a SHA1 key derived from the OS info # + 0xA4 - 0x40 bytes of a SHA1 key array derived from the OS info # + 0xE5 - 0x40 bytes of a SHA1 key array derived from the OS info # # PART 3: # + 0x136 - Shifted Payment Key (size 0x20) # + 0x177 - Shifted Master key (size 0x20) # Open the target 'key.dat' file keybuff = args.keyfile.read() args.keyfile.close() shifted_key = keybuff[0x177:0x197] sha256 = SHA256.SHA256Hash(shifted_key) # Get the Chiper key cipher_key = sha256.digest() if args.fic: # If we're in decryption mode if args.decrypt: try: dataencrypted = args.fic.read() args.fic.close() except IOError: print >> sys.stderr, "Error in opening file" return except Exception, e: print >> sys.stderr, "does not exist" print >> sys.stderr, "Exception: %s" % str(e) return # encrypted data is using the following format # first 16 bytes are containing IV # then next 4 bytes are containing file length # finally rest of file is containing encrypted data # Noticed: Bytes are 2 hex digits, so number are * by 2 if dataencrypted and cipher_key: iv_key = dataencrypted[:16] size_str = dataencrypted[16:20] # Convert size str in an integer size = int(size_str[::-1].encode('hex'), 16) cipherdata = dataencrypted[20:] try: context = AES.new(cipher_key, args.mode, iv_key) cleardata = context.decrypt(cipherdata) except Exception, e: print >> sys.stderr, "Error in crypto handling" print >> sys.stderr, "Exception: %s" % str(e) return if cleardata: try: output_tuple = os.path.splitext(args.fic.name) if (output_tuple[1] == '.ecc'): output_tuple = os.path.splitext(output_tuple[0]) output_filename = output_tuple[ 0] + "_decrypted" + output_tuple[1] fdesc = open(output_filename, 'wb') # Write only the actual real data fdesc.write(cleardata[:size]) fdesc.close() print 'Wrote decrypted file', output_filename except IOError: print >> sys.stderr, "Error in writing file" return except Exception, e: print >> sys.stderr, "does not exist" print >> sys.stderr, "Exception: %s" % str(e) return
for tmp in chunks: w = split(tmp) for i in range(16, 64): s0 = rrot(w[i - 15], 7) ^ rrot(w[i - 15], 18) ^ (w[i - 15] >> 3) s1 = rrot(w[i - 2], 17) ^ rrot(w[i - 2], 19) ^ (w[i - 2] >> 10) w[i] = addition(w[i - 16], s0, w[i - 7], s1) a, b, c, d, e, f, g, h = H[0], H[1], H[2], H[3], H[4], H[5], H[6], H[7] for i in range(64): s1 = rrot(e, 6) ^ rrot(e, 11) ^ rrot(e, 25) ch = (e & f) ^ (~ e & g) sch1 = addition(h, s1, ch, K[i], w[i]) s0 = rrot(a, 2) ^ rrot(a, 13) ^ rrot(a, 22) maj = (a & b) ^ (a & c) ^ (b & c) sch2 = addition(s0, maj) h, g, f, e, d, c, b, a = g, f, e, addition(d + sch1), c, b, a, addition(sch1, sch2) H = [addition(*i) for i in zip(H, [a, b, c, d, e, f, g, h])] return b''.join(list(map(lambda x: bytes.fromhex(hex(x)[2:]), H))).hex() if __name__ == '__main__': test = "London is the capital of Great Britain" print(SHA(test)) print(SHA(test) == SHA256.SHA256Hash(test).hexdigest())