Cryptographic Hash is a Hash function that takes random size input and yields a fixed-size output. It is easy to calculate but challenging to retrieve original data. It is strong and difficult to duplicate the same hash with unique inputs and is a one-way function so revert is not possible. Hashing is also known by different names such as Digest, Message Digest, Checksum, etc.
Properties Of Cryptography Hash Function
The ideal cryptographic hash function has the following main properties:
- Deterministic: This means that the same message always results in the same hash.
- Quick: It is quick to compute the hash value for any given message.
- Avalanche Effect: This means that every minor change in the message results in a major change in the hash value.
- One-Way Function: You cannot reverse the cryptographic hash function to get to the data.
- Collision Resistance: It is infeasible to find two different messages that produce the same hash value.
We often hear the term Cracking a Hash, there are a couple of ways to do that:
- Find an algorithm to generate a collision between two hashes. The more advance the algorithm is, the more difficult it is to crack the hash.
- Another way is to find an algorithm to identify a unique and different input that will produce a given hash. It is similar to a collision, but instead of colliding, we are focusing on finding the input using an algorithm.
- Some common hashes we still use today that are considered “cracked” from a cryptographic point of view are MD5(Message-Digest Algorithm) and SHA-1(Secure Hash Algorithm 1). Keep in mind that these are technically broken Hashes and never use for your security purposes.
How to creating a Cryptographic Hash
- Create a random salt value using SecureRandom class, SecureRandom class generates strong random values. The engineNextBytes(byte bytes) method is used to generate a user-specified number of random bytes.
- Convert two sets of bytes into one using ByteArrayOutputStream class and creating it to ByteArray.
- Create an instance of a messageDigest passing SHA2_ALGORITHM which returns a hash of the given input value.
- UUID is used to generate a random number that is converted to string and passed as input.
- The returned object can be converted to a hex binary format using DatatypeConverter.
Note: Salt is a random value added to the input data(passwords) to defend against pre-computed hash attacks such as Rainbow tables.
How to create Cryptographic Hashing Passwords
As we have seen how to generate a Hash now, let us use Bcrypt to hash a password. Do not use broken Hashing algorithms for Hashing Passwords. Bcrypt is a password Hashing function based on Blowfish Cipher.
- Pass the password to hashpw function which is in Bcrypt class which can also generate the salt by itself and returns a string.
- Verify if the password hash and password are really matching using the checkpw() function. It returns a Boolean value.
Input: Enter the password: GEEKS FOR GEEKS
Output: Hashed-password: $2a$10$u6MFjykfR76nHGfhYYzjjOOe1I3EY.YxpQY4vKRHpKRCqz7w69RTa
- Digital signatures.
- Digital fingerprints.
- Logging sensitive data.
- Saving passwords.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Passwords and Cryptographic hash function
- What are Hash Functions and How to choose a good Hash Function?
- Implementing our Own Hash Table with Separate Chaining in Java
- MD5 hash in Java
- SHA-512 Hash In Java
- SHA-384 Hash In Java
- SHA-224 Hash In Java
- MD2 Hash In Java
- SHA-256 Hash in Java
- Sorting using trivial hash function
- Hash Function for String data in C#
- Blockchain Hash Function
- String hashing using Polynomial rolling hash function
- Advantages of BST over Hash Table
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Hash Table vs STL Map
- Implementing own Hash Table with Open Addressing Linear Probing in C++
- Sort elements by frequency | Set 4 (Efficient approach using hash)
- Graph representations using set and hash
- Hash Functions in System Security
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.