Cryptography ‐ SHA256 - Arrbat/Veil-Forge GitHub Wiki
Introduction
Cryptography is an important part of modern application security. This page describes how Veil-Forge's crypto works and what/why it uses.
SHA256 - hash function
Theory
SHA256 is a widely used cryptographic hash function that converts input data into a 256-bit string.
The main purpose of any hash function is to make it impossible to revert the hash back to the original input — in other words, we cannot convert the hash result into the original data. Also, the same input data must always produce the same hash, which makes the function deterministic — this is essential if we want to verify data integrity.
But here’s a question — what if two different inputs produce the same hash? This situation is called a collision, and a hash function that makes such cases extremely rare (ideally impossible) is called a collision-resistant hash function.
That’s the basic theory. In practice, this project uses SHA256 — it's more secure than SHA1 (which is now broken) and usually faster than SHA512. In most use cases, SHA256 offers a good balance between speed and security.
How SHA256 works?
I'd recommend this page if you want to understand how it works in depth.
In short:
1) Message preparation. The total length must be padded to a multiple of 512 bits. If the message is shorter, we extend it with padding and length encoding.
2) We calculate 8 initial hash values and define 64 round constants.
3) The message is split into 512-bit chunks. For each chunk, we perform 64 rounds of bitwise operations that update the hash state. In each round, one of the precomputed constants is used.
Why SHA256?
Veil-Forge uses SHA256 because:
- It is well-tested and widely supported across platforms and cryptographic libraries.
- It is faster than SHA512, while still offering strong security properties.
- It is resistant to collisions and pre-image attacks, which makes it suitable for integrity checks or obfuscation-related transformations.
- It has no known practical vulnerabilities (as of writing this), unlike older hashes like MD5 or SHA1.
In Veil-Forge, SHA256 is applied after encryption of the payload — this is done intentionally.
The resulting hash is then used as input keying material for the HKDF (HMAC-based Key Derivation Function), which derives additional cryptographic keys.
This approach ensures that during decryption, when only the encrypted payload is available, the correct hash (and therefore the correct key) can be deterministically reproduced. If the hash were taken from the plaintext instead, it would be impossible to recover the key from just the ciphertext.