def __shake256(msg, digest_len):
     """Returns a bytes object containing the SHAKE-256 hash of msg with 
     digest_len bytes of output"""
     shake_ctx = hashes.Hash(hashes.SHAKE256(digest_len),
                             backend=default_backend())
     shake_ctx.update(msg)
     return shake_ctx.finalize()
Beispiel #2
0
class TestSHAKE256(object):
    test_shake256 = generate_hash_test(
        load_hash_vectors,
        os.path.join("hashes", "SHAKE"),
        [
            "SHAKE256LongMsg.rsp",
            "SHAKE256ShortMsg.rsp",
        ],
        hashes.SHAKE256(digest_size=32),
    )

    @pytest.mark.parametrize("vector",
                             _load_all_params(
                                 os.path.join("hashes", "SHAKE"),
                                 [
                                     "SHAKE256VariableOut.rsp",
                                 ],
                                 load_nist_vectors,
                             ))
    def test_shake256_variable(self, vector, backend):
        output_length = int(vector['outputlen']) // 8
        msg = binascii.unhexlify(vector['msg'])
        shake = hashes.SHAKE256(digest_size=output_length)
        m = hashes.Hash(shake, backend=backend)
        m.update(msg)
        assert m.finalize() == binascii.unhexlify(vector['output'])
Beispiel #3
0
 def test_shake256_variable(self, vector, backend):
     output_length = int(vector['outputlen']) // 8
     msg = binascii.unhexlify(vector['msg'])
     shake = hashes.SHAKE256(digest_size=output_length)
     m = hashes.Hash(shake, backend=backend)
     m.update(msg)
     assert m.finalize() == binascii.unhexlify(vector['output'])
 def test_shake256_variable(self, backend, subtests):
     vectors = _load_all_params(
         os.path.join("hashes", "SHAKE"),
         ["SHAKE256VariableOut.rsp"],
         load_nist_vectors,
     )
     for vector in vectors:
         with subtests.test():
             output_length = int(vector["outputlen"]) // 8
             msg = binascii.unhexlify(vector["msg"])
             shake = hashes.SHAKE256(digest_size=output_length)
             m = hashes.Hash(shake, backend=backend)
             m.update(msg)
             assert m.finalize() == binascii.unhexlify(vector["output"])
    async def SHAKE256(message):

        message = message.encode()

        kdf = PBKDF2HMAC(
            algorithm=hashes.SHAKE256(),
            length=hashLen,
            salt=
            b'\xe7\xde\xc1\xf0\x81\x99\xde}\xa4\xb50u;&\x06\xe7\xa4\xbfn\xbc',
            iterations=hashIter,
            backend=default_backend())

        output = base64.urlsafe_b64encode(kdf.derive(message))

        return (output)
Beispiel #6
0
                                     "SHAKE128VariableOut.rsp",
                                 ],
                                 load_nist_vectors,
                             ))
    def test_shake128_variable(self, vector, backend):
        output_length = int(vector['outputlen']) // 8
        msg = binascii.unhexlify(vector['msg'])
        shake = hashes.SHAKE128(digest_size=output_length)
        m = hashes.Hash(shake, backend=backend)
        m.update(msg)
        assert m.finalize() == binascii.unhexlify(vector['output'])


@pytest.mark.supported(
    only_if=lambda backend: backend.hash_supported(
        hashes.SHAKE256(digest_size=32)),
    skip_message="Does not support SHAKE256",
)
@pytest.mark.requires_backend_interface(interface=HashBackend)
class TestSHAKE256(object):
    test_shake256 = generate_hash_test(
        load_hash_vectors,
        os.path.join("hashes", "SHAKE"),
        [
            "SHAKE256LongMsg.rsp",
            "SHAKE256ShortMsg.rsp",
        ],
        hashes.SHAKE256(digest_size=32),
    )

    @pytest.mark.parametrize("vector",
Beispiel #7
0
        self.last_wrote = datetime.datetime.now()
        self.cur_hashes = []
        self.manifest_seq += 1


# map from names in hash-algorithm-t in
# https://tools.ietf.org/html/draft-ietf-netconf-crypto-types-11
# to options from cryptography.hazmat.primitives import hashes
HASH_ALG_MAP = {
        'sha-224': hashes.SHA3_224,
        'sha-256': hashes.SHA3_256,
        'sha-384': hashes.SHA3_384,
        'sha-512': hashes.SHA3_512,
        'shake-128': hashes.SHAKE128(16),
        # 'shake-224': hashes.SHAKE256(28),  # TBD: is this right?
        'shake-256': hashes.SHAKE256(32),
    }

class ManifestServer(object):
    def __init__(self, addr, port, trust_ca, local_identity):
        self.preconnection = None
        self.addr = addr
        self.port = port
        self.trust_ca = trust_ca
        self.local_identity = local_identity
        self.loop = asyncio.get_event_loop()
        self.connections = []

    async def push_new_manifest(self, manifest):
        for conn in self.connections:
            self.loop.create_task(conn.send_message(manifest))
str_tmp = master + plateform

key = str(int(hashlib.sha256(str_tmp.encode()).hexdigest(), 16) % 10**32)
key = key.encode()

iv = ident
iv = str(int(hashlib.sha256(iv.encode()).hexdigest(), 16) % 10**16)
iv = iv.encode()

msg = master
msg = str(int(hashlib.sha256(msg.encode()).hexdigest(), 16) % 10**16)

while len(msg) != 16:
    msg = msg + '0'

#print("Key = ",key)
#print("iv  =",iv)

encryptor = Cipher(algorithms.AES(key),
                   modes.CBC(iv),
                   backend=default_backend()).encryptor()

ciphtertxt = encryptor.update(msg.encode()) + encryptor.finalize()
#print(ciphtertxt)

digest = hashes.Hash(hashes.SHAKE256(num_char), backend=default_backend())
digest.update(ciphtertxt)
hash_text = digest.finalize()
print(hash_text.hex())