anubis-age 1.4.0

Post-quantum secure encryption library with hybrid X25519+ML-KEM-1024 mode (internal dependency for anubis-rage)
Documentation
use std::io::{self, BufReader};

use super::{ReadError, StdinGuard, UiCallbacks};
use crate::{identity::IdentityFile, Identity};

/// Reads identities from the provided files.
///
/// `filenames` may contain at most one entry of `"-"`, which will be interpreted as
/// reading from standard input. An error will be returned if `stdin_guard` is guarding an
/// existing usage of standard input.
pub fn read_identities(
    filenames: Vec<String>,
    _max_work_factor: Option<u8>,
    stdin_guard: &mut StdinGuard,
) -> Result<Vec<Box<dyn Identity>>, ReadError> {
    let mut identities: Vec<Box<dyn Identity>> = Vec::with_capacity(filenames.len());

    for filename in filenames {
        let reader = stdin_guard.open(filename.clone()).map_err(|e| match e {
            ReadError::Io(e) if matches!(e.kind(), io::ErrorKind::NotFound) => {
                ReadError::IdentityNotFound(filename.clone())
            }
            other => other,
        })?;

        let identity_file = IdentityFile::from_buffer(BufReader::new(reader))
            .map_err(ReadError::from)?
            .with_callbacks(UiCallbacks);

        let new_identities = identity_file
            .into_identities()
            .map_err(ReadError::EncryptedIdentities)?;

        identities.extend(new_identities.into_iter().map(|i| i as Box<dyn Identity>));
    }

    Ok(identities)
}