Learn Hash Functions

Understand cryptographic hash functions and their applications.

Introduction

Hash functions are mathematical algorithms that convert arbitrary data into a fixed-size string of characters. They are widely used for data integrity, password storage, and digital signatures.

Beginner Guide

Getting Started with Hash Functions

#

What is a Hash Function?

A hash function takes input data (of any size) and produces a fixed-size output (hash value). Key properties:

1. Deterministic: Same input always produces same output 2. Fixed Size: Output length is constant regardless of input size 3. One-Way: Cannot be reversed to get original input 4. Avalanche Effect: Small change in input produces large change in output

#

Common Hash Algorithms

MD5:

  • 128-bit output
  • Now considered insecure
  • Still used for checksums
  • SHA-1:

  • 160-bit output
  • Cryptographically broken
  • Deprecated for security purposes
  • SHA-256:

  • 256-bit output
  • Secure for most applications
  • Widely recommended
  • SHA-512:

  • 512-bit output
  • More secure but slower
  • Used for high-security applications
  • #

    Example Usage

    ``javascript // Create SHA-256 hash const hash = await crypto.subtle.digest('SHA-256', data);

    // Convert to hex string const hexHash = Array.from(new Uint8Array(hash)) .map(b => b.toString(16).padStart(2, '0')) .join(''); ``

    Advanced Guide

    Advanced Hash Concepts

    #

    Collision Resistance

    A collision occurs when two different inputs produce the same hash. A secure hash function should be collision-resistant.

  • MD5: Collisions found
  • SHA-1: Collisions found
  • SHA-256: No known collisions
  • #

    Preimage Resistance

    Preimage resistance means it's computationally infeasible to find an input that produces a given hash.

    #

    Second Preimage Resistance

    Second preimage resistance means it's computationally infeasible to find a second input that produces the same hash as a given input.

    #

    Hash Applications

    1. Data Integrity: Verify file downloads 2. Password Storage: Store hash of passwords (always use salt!) 3. Digital Signatures: Sign documents 4. Blockchain: Hash blocks for integrity 5. Deduplication: Identify duplicate files

    #

    Salted Hashing

    Never store plain passwords! Always use salt:

    `` hash = SHA-256(salt + password) ``

    Salt should be:

  • Unique for each user
  • Random
  • Stored with the hash
  • #

    Key Derivation Functions

    For passwords, use specialized KDFs:

  • bcrypt
  • scrypt
  • Argon2
  • These are designed to be slow to prevent brute-force attacks.

    Common Errors

    • Using MD5 or SHA-1 for security purposes
    • Storing passwords without salting
    • Using fast hash functions for passwords
    • Assuming hash functions provide encryption
    • Not verifying file hashes after download
    • Using weak salt generation
    • Reusing salts across users
    • Storing salt separately from the hash

    FAQ

    What is a hash function?

    A hash function converts arbitrary data into a fixed-size output with specific properties.

    Can hash functions be reversed?

    No, hash functions are designed to be one-way. You cannot get the original input from a hash.

    Which hash algorithm should I use?

    Use SHA-256 for most applications. Use bcrypt/scrypt/Argon2 for passwords.

    Is SHA-256 secure?

    Yes, SHA-256 is considered secure for cryptographic purposes.

    What is salting?

    Salting is adding random data to input before hashing to prevent rainbow table attacks.

    Can two different files have the same hash?

    It should be computationally infeasible for secure hash functions. This is called a collision.

    How do I verify file integrity?

    Compare the hash of the downloaded file with the published hash.

    What is the difference between hash and encryption?

    Hashing is one-way, encryption is two-way (can be decrypted with a key).

    Ready to Practice?

    Try our tools to apply what you have learned in real-time.

    DevKitFlow - Free Online Developer Tools