def generate_key(key_material, sid_value): sid_value_nob64 = base64.b64decode(sid_value) # part of the decoded SID value contains a random 16-byte value needed to decrypt the response rand_16_bytes = sid_value_nob64[0:16] blake = monocypher.Blake2b(key=rand_16_bytes) blake.update(key_material) hash1 = blake.finalize() blake = monocypher.Blake2b(key=hash1) blake.update(key_material) hash2 = blake.finalize() slice1 = hash2[0:32] slice2 = hash2[32:] key = [] for i, b1 in enumerate(slice1): kb = b1 ^ slice2[i] key.append(kb) key = bytes(key) return key
def generate_JSESSIONID(key): blake = monocypher.Blake2b() blake.update(key) hash1 = blake.finalize() slice1 = hash1[0:16] slice2 = hash1[16:32] buf = [] for i, b1 in enumerate(slice1): kb = b1 ^ slice2[i] buf.append(kb) buf = bytes(buf) JSESSIONID = base64.b64encode(buf) print("JSESSIONID: %s" % JSESSIONID)
def test_blake2b_against_test_vectors(self): with open(BLAKE2_TEST_VECTOR_FILENAME, 'rt') as f: test_vectors = json.load(f) for test_vector in test_vectors: if test_vector['hash'] != 'blake2b': continue if test_vector['key'] != '': # todo remove continue v_in = binascii.unhexlify(test_vector['in']) v_key = binascii.unhexlify(test_vector['key']) v_out = binascii.unhexlify(test_vector['out']) result = monocypher.blake2b(v_in, v_key) self.assertEqual(v_out, result) b = monocypher.Blake2b(key=v_key) b.update(v_in) self.assertEqual(v_out, b.finalize()) result = hashlib.blake2b(v_in).digest() self.assertEqual(v_out, result)