migratrom 0.2.0

Declarative, self-verifying database migrations (core engine).
Documentation
//! Hash algorithm registry for checksums stored in migration history.
//!
//! The canonical encoder owns the bytes; this module owns the named digest
//! algorithms and their stable string prefixes, such as `sha256/...`.

use sha2::{Digest, Sha256};

/// A named hash over canonical bytes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HashAlgorithm {
    /// SHA-256, stored with the `sha256` name.
    Sha256,
}

impl HashAlgorithm {
    /// The checksum algorithm used when no explicit algorithm is selected.
    pub const DEFAULT: HashAlgorithm = HashAlgorithm::Sha256;

    /// The prefix stored before the `/` in a checksum string.
    pub fn name(self) -> &'static str {
        match self {
            HashAlgorithm::Sha256 => "sha256",
        }
    }

    /// Look up an algorithm by its registered name.
    pub fn from_name(name: &str) -> Option<HashAlgorithm> {
        match name {
            "sha256" => Some(HashAlgorithm::Sha256),
            _ => None,
        }
    }

    /// Hash `input`, returning the raw digest bytes.
    pub fn hash(self, input: &[u8]) -> Vec<u8> {
        match self {
            HashAlgorithm::Sha256 => Sha256::digest(input).to_vec(),
        }
    }
}