librage 1.1.0

A safe, simple, and complete Rust wrapper for the rage (age) encryption library.
Documentation
use crate::core::{helpers, parsers};
use crate::handler::error::LibrageError;
use crate::handler::types::{EncryptOutput, LibrageResponse};
use age::armor::{ArmoredWriter, Format};
use std::io::{Read, Write};

pub fn encrypt(plaintext: &[u8], public_key: &str) -> LibrageResponse<EncryptOutput> {
    encrypt_with_options(plaintext, public_key, false)
}

pub fn encrypt_armored(plaintext: &[u8], public_key: &str) -> LibrageResponse<EncryptOutput> {
    encrypt_with_options(plaintext, public_key, true)
}

fn encrypt_with_options(
    plaintext: &[u8],
    public_key: &str,
    armored: bool,
) -> LibrageResponse<EncryptOutput> {
    let recipient = match parsers::parse_x25519_recipient(public_key) {
        Ok(r) => r,
        Err(e) => return LibrageResponse::err(e.into()),
    };

    let mut ciphertext = Vec::new();

    if armored {
        let armored_writer = match ArmoredWriter::wrap_output(&mut ciphertext, Format::AsciiArmor) {
            Ok(w) => w,
            Err(e) => {
                return LibrageResponse::err(LibrageError::EncryptionFailed(e.to_string()).into());
            }
        };
        match helpers::encrypt_to_writer(plaintext, &[recipient], armored_writer) {
            Ok(armored_writer) => {
                if let Err(e) = armored_writer.finish() {
                    return LibrageResponse::err(
                        LibrageError::EncryptionFailed(e.to_string()).into(),
                    );
                }
                LibrageResponse::ok(EncryptOutput::new(ciphertext, true))
            }
            Err(e) => LibrageResponse::err(e.into()),
        }
    } else {
        match helpers::encrypt_to_writer(plaintext, &[recipient], &mut ciphertext) {
            Ok(_) => LibrageResponse::ok(EncryptOutput::new(ciphertext, false)),
            Err(e) => LibrageResponse::err(e.into()),
        }
    }
}

pub fn encrypt_multiple(plaintext: &[u8], public_keys: &[&str]) -> LibrageResponse<EncryptOutput> {
    let recipients = match parsers::parse_x25519_recipients(public_keys) {
        Ok(r) => r,
        Err(e) => return LibrageResponse::err(e.into()),
    };

    let mut ciphertext = Vec::new();
    match helpers::encrypt_to_writer(plaintext, &recipients, &mut ciphertext) {
        Ok(_) => LibrageResponse::ok(EncryptOutput::new(ciphertext, false)),
        Err(e) => LibrageResponse::err(e.into()),
    }
}

pub fn encrypt_multiple_armored(
    plaintext: &[u8],
    public_keys: &[&str],
) -> LibrageResponse<EncryptOutput> {
    let recipients = match parsers::parse_x25519_recipients(public_keys) {
        Ok(r) => r,
        Err(e) => return LibrageResponse::err(e.into()),
    };

    let mut ciphertext = Vec::new();
    let armored_writer = match ArmoredWriter::wrap_output(&mut ciphertext, Format::AsciiArmor) {
        Ok(w) => w,
        Err(e) => {
            return LibrageResponse::err(LibrageError::EncryptionFailed(e.to_string()).into());
        }
    };

    match helpers::encrypt_to_writer(plaintext, &recipients, armored_writer) {
        Ok(armored_writer) => {
            if let Err(e) = armored_writer.finish() {
                return LibrageResponse::err(LibrageError::EncryptionFailed(e.to_string()).into());
            }
            LibrageResponse::ok(EncryptOutput::new(ciphertext, true))
        }
        Err(e) => LibrageResponse::err(e.into()),
    }
}

pub fn encrypt_stream<R: Read, W: Write>(
    reader: R,
    writer: W,
    public_key: &str,
) -> LibrageResponse<()> {
    let recipient = match parsers::parse_x25519_recipient(public_key) {
        Ok(r) => r,
        Err(e) => return LibrageResponse::err(e.into()),
    };
    match helpers::encrypt_stream_to_writer(reader, writer, &[recipient]) {
        Ok(()) => LibrageResponse::ok(()),
        Err(e) => LibrageResponse::err(e.into()),
    }
}

pub fn encrypt_stream_armored<R: Read, W: Write>(
    reader: R,
    writer: W,
    public_key: &str,
) -> LibrageResponse<()> {
    let recipient = match parsers::parse_x25519_recipient(public_key) {
        Ok(r) => r,
        Err(e) => return LibrageResponse::err(e.into()),
    };
    match helpers::encrypt_stream_to_armored_writer(reader, writer, &[recipient]) {
        Ok(()) => LibrageResponse::ok(()),
        Err(e) => LibrageResponse::err(e.into()),
    }
}