Hash Utilities

Overview

lighty-core provides SHA1 hash verification utilities for both async and sync contexts. These are essential for verifying downloaded files and ensuring data integrity.

Export:

  • Module: lighty_core::hash

  • Re-export: lighty_launcher::core::hash

Functions

verify_file_sha1

Async SHA1 verification for small to medium files.

pub async fn verify_file_sha1(path: &Path, expected_sha1: &str) -> HashResult<bool>

Use case: Files that fit comfortably in memory (< 100MB)

Example:

use lighty_core::hash::verify_file_sha1;
use std::path::Path;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let is_valid = verify_file_sha1(
        Path::new("minecraft-1.21.1.jar"),
        "abc123def456..."
    ).await?;

    if is_valid {
        println!("✓ File integrity verified");
    } else {
        println!("✗ Hash mismatch!");
    }

    Ok(())
}

verify_file_sha1_streaming

Async SHA1 verification with streaming for large files.

Use case: Large files (> 100MB) to minimize memory usage

Example:

Performance: Uses 8KB buffer for streaming reads.

calculate_file_sha1_sync

Synchronous SHA1 calculation.

Use case: Non-async contexts, sync archive processing

Example:

verify_file_sha1_sync

Synchronous SHA1 verification.

Use case: Sync contexts

Example:

calculate_sha1_bytes

Calculate SHA1 hash of arbitrary bytes.

Use case: Hashing strings, usernames, in-memory data

Example:

calculate_sha1_bytes_raw

Calculate SHA1 hash returning raw bytes.

Use case: When you need raw hash bytes instead of hex string

Example:

Error Handling

Usage in Download System

SHA1 verification is commonly used after downloading files:

Performance Comparison

Small Files (< 10MB)

Use: verify_file_sha1 - Reads entire file into memory

  • Faster for small files

  • Simple implementation

Medium Files (10MB - 100MB)

Use: Either function works well

  • verify_file_sha1 for speed

  • verify_file_sha1_streaming to save memory

Large Files (> 100MB)

Use: verify_file_sha1_streaming - Streams file in chunks

  • Minimal memory usage

  • Slightly slower but more memory-efficient

Case Insensitivity

All hash comparison functions are case-insensitive:

Exports

In lighty_core:

In lighty_launcher:

Last updated