N
N
Nikita2022-02-11 15:25:04
Cryptography
Nikita, 2022-02-11 15:25:04

Is it possible to use the first 128 bits of a SHA256 hash as a 128 bit key?

You need to convert the password to a 128 bit key. I decided to use a hash function as this goal. Having studied 128 bit hash functions, I realized that these are either obsolete MD hash functions or SHA1 in which they found a bunch of holes. I realized that both options, for obvious reasons, are not suitable and began to look at 256 -bit hash functions. I found it seems like a normal SHA256 , but it returns a hash of 256 bits, and the key must be 128 bits long. Is it possible to just take the first half of the hash and discard the second?

PS SHA256 hash is considered not 1 time, but 10,000 to counteract brute force. Here is the C++ function:

std::vector<unsigned char> Cryptor::generate_key_from_password(std::vector<unsigned char> password) {
    std::vector<unsigned char> hash = SHA256::get_hash(std::move(password));
    for (int i = 0; i < 9999; i = i + 1) {
        hash = SHA256::get_hash(hash);
    }
    hash.resize(16);
    return hash;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lander, 2022-02-11
@gth-other

You can, of course, if the increase in collisions does not bother you much. The hash length is 256 bits - not just taken.
upd:

SHA256 hash is counted not 1 time, but 10,000 to counteract brute force.

And this is even more stupidity. The hash function is NOT bijective! So by calculating its value 10,000 times you also increase the number of collisions.

L
Lynn "Coffee Man", 2022-02-13
@Lynn

No need to reinvent the wheel in cryptography
https://ru.m.wikipedia.org/wiki/PBKDF2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question