WikiGalaxy

Personalize

Password Storage

Secure Password Storage

Hashing is crucial in securing passwords. Instead of storing passwords in plain text, systems store hashed versions of passwords. This ensures that even if the database is compromised, the actual passwords remain secure.

Salting for Enhanced Security

To further enhance security, a unique salt is added to each password before hashing. This prevents attackers from using precomputed tables (rainbow tables) to crack passwords.


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PasswordHashing {
    public static String hashPassword(String password) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] hash = md.digest(password.getBytes());
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            hexString.append(String.format("%02x", b));
        }
        return hexString.toString();
    }
}
    

Console Output:

e3afed0047b08059d0fada10f400c1e5

Data Integrity

Ensuring Data Integrity

Hash functions are used to ensure data integrity. By generating a hash value for data, any alteration in the data can be detected by comparing the current hash with the original hash.

File Verification

When downloading files, hash values are often provided to verify that the file has not been tampered with during transmission.


import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;

public class FileIntegrityCheck {
    public static String getFileChecksum(String filePath) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));
        byte[] hash = md.digest(fileBytes);
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            hexString.append(String.format("%02x", b));
        }
        return hexString.toString();
    }
}
    

Console Output:

9f86d081884c7d659a2feaa0c55ad015

Digital Signatures

Authenticating Digital Documents

Digital signatures use hashing to authenticate documents. A hash of the document is encrypted with a private key, and the signature is verified using the corresponding public key.

Non-repudiation

Digital signatures provide non-repudiation, meaning the signer cannot deny signing the document as it is uniquely linked to them.


import java.security.*;

public class DigitalSignatureExample {
    public static byte[] signData(byte[] data, PrivateKey privateKey) throws Exception {
        Signature signer = Signature.getInstance("SHA256withRSA");
        signer.initSign(privateKey);
        signer.update(data);
        return signer.sign();
    }
}
    

Console Output:

Signature: [byte array]

Hash Tables

Efficient Data Retrieval

Hash tables use hashing to enable fast data retrieval. They map keys to values using a hash function, allowing for average-case constant time complexity for lookups.

Collision Handling

Collisions occur when two keys hash to the same index. Techniques like chaining and open addressing are used to handle collisions effectively.


import java.util.HashMap;

public class HashTableExample {
    public static void main(String[] args) {
        HashMap map = new HashMap<>();
        map.put("apple", 1);
        map.put("banana", 2);
        System.out.println("Value for apple: " + map.get("apple"));
    }
}
    

Console Output:

Value for apple: 1

Cryptographic Hash Functions

Foundation of Cryptography

Cryptographic hash functions are fundamental to cryptography. They provide properties like pre-image resistance, ensuring it's computationally infeasible to reverse-engineer the original input from its hash.

Applications in Blockchain

In blockchain technology, cryptographic hashes are used to ensure the integrity of blocks and to create links between them, forming a secure chain.


import java.security.MessageDigest;

public class CryptographicHashExample {
    public static String computeHash(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] hash = md.digest(data.getBytes());
        StringBuilder hexString = new StringBuilder();
        for (byte b : hash) {
            hexString.append(String.format("%02x", b));
        }
        return hexString.toString();
    }
}
    

Console Output:

3a7bd3e2360a2e261d832b9e8a7e5f3a

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025