argon2 0.6.0-rc.8

Pure Rust implementation of the Argon2 password hashing function with support for the Argon2d, Argon2i, and Argon2id algorithmic variants
Documentation
//! Error type

use core::fmt;

/// Result with argon2's [`Error`] type.
pub type Result<T> = core::result::Result<T, Error>;

/// Error type.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
    /// Associated data is too long.
    AdTooLong,

    /// Algorithm identifier invalid.
    AlgorithmInvalid,

    /// "B64" encoding is invalid.
    B64Encoding(base64ct::Error),

    /// Key ID is too long.
    KeyIdTooLong,

    /// Memory cost is too small.
    MemoryTooLittle,

    /// Memory cost is too large.
    MemoryTooMuch,

    /// Output is too short.
    OutputTooShort,

    /// Output is too long.
    OutputTooLong,

    /// Password is too long.
    PwdTooLong,

    /// Salt is too short.
    SaltTooShort,

    /// Salt is too long.
    SaltTooLong,

    /// Secret is too long.
    SecretTooLong,

    /// Not enough threads.
    ThreadsTooFew,

    /// Too many threads.
    ThreadsTooMany,

    /// Time cost is too small.
    TimeTooSmall,

    /// Invalid version.
    VersionInvalid,

    /// Out of memory (heap allocation failure).
    OutOfMemory,
}

impl core::error::Error for Error {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match self {
            Self::B64Encoding(err) => Some(err),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Error::AdTooLong => "associated data is too long",
            Error::AlgorithmInvalid => "algorithm identifier invalid",
            Error::B64Encoding(inner) => return write!(f, "B64 encoding invalid: {inner}"),
            Error::KeyIdTooLong => "key ID is too long",
            Error::MemoryTooLittle => "memory cost is too small",
            Error::MemoryTooMuch => "memory cost is too large",
            Error::OutputTooShort => "output is too short",
            Error::OutputTooLong => "output is too long",
            Error::PwdTooLong => "password is too long",
            Error::SaltTooShort => "salt is too short",
            Error::SaltTooLong => "salt is too long",
            Error::SecretTooLong => "secret is too long",
            Error::ThreadsTooFew => "not enough threads",
            Error::ThreadsTooMany => "too many threads",
            Error::TimeTooSmall => "time cost is too small",
            Error::VersionInvalid => "invalid version",
            Error::OutOfMemory => "out of memory",
        })
    }
}

impl From<base64ct::Error> for Error {
    fn from(err: base64ct::Error) -> Error {
        Error::B64Encoding(err)
    }
}

#[cfg(feature = "kdf")]
impl From<Error> for kdf::Error {
    fn from(_err: Error) -> kdf::Error {
        kdf::Error
    }
}

#[cfg(feature = "password-hash")]
impl From<Error> for password_hash::Error {
    fn from(err: Error) -> password_hash::Error {
        match err {
            Error::AdTooLong => password_hash::Error::ParamInvalid { name: "data" },
            Error::AlgorithmInvalid => password_hash::Error::Algorithm,
            Error::B64Encoding(_) => password_hash::Error::EncodingInvalid,
            Error::KeyIdTooLong => password_hash::Error::ParamInvalid { name: "keyid" },
            Error::MemoryTooLittle | Error::MemoryTooMuch => {
                password_hash::Error::ParamInvalid { name: "m" }
            }
            Error::OutOfMemory => password_hash::Error::OutOfMemory,
            Error::OutputTooShort | Error::OutputTooLong => password_hash::Error::OutputSize,
            Error::PwdTooLong => password_hash::Error::PasswordInvalid,
            Error::SaltTooShort | Error::SaltTooLong => password_hash::Error::SaltInvalid,
            Error::SecretTooLong => password_hash::Error::ParamsInvalid,
            Error::ThreadsTooFew | Error::ThreadsTooMany => {
                password_hash::Error::ParamInvalid { name: "p" }
            }
            Error::TimeTooSmall => password_hash::Error::ParamInvalid { name: "t" },
            Error::VersionInvalid => password_hash::Error::Version,
        }
    }
}