libhardcopy 1.0.1

Generalized library for hardcopy/softcopy command line utilities.
Documentation
//  S/KEY OTP encodes data in 66-bit blocks: 64 data bits followed by a
//  2-bit checksum.  Each block yields 6 words (6 x 11 bits) from a
//  pre-determined 2048-word list.  The checksum is the sum of all 32
//  two-bit pairs of the 64-bit data value, taken mod 4.
//
mod algo;

pub use crate::plugins::skeyotp::algo::SKEY_OTP_INSTRUCTIONS;
use crate::plugins::skeyotp::algo::{bytes_to_wordlist, max_word_len, wordlist_to_bytes};
use crate::plugins::{GenerationError, HardCopyGenerator, LibPlugin, SoftCopyGenerator};
use crate::{
    HardCopy, HardCopyInput, HardCopySection, HardCopySectionContent, SoftCopy, SoftCopyInput,
};

const WORDS_PER_LINE: usize = 6;

pub struct SKeyOtpPlugin;

const PLUGIN_NAME: &str = "S/KEY OTP";
const PLUGIN_DESCRIPTION: &str = "Wordlist generation using S/KEY OTP words (RFC 2289).";

impl LibPlugin for SKeyOtpPlugin {
    fn name(&self) -> String {
        PLUGIN_NAME.to_string()
    }
    fn description(&self) -> String {
        PLUGIN_DESCRIPTION.to_string()
    }
    fn instructions(&self) -> String {
        SKEY_OTP_INSTRUCTIONS.to_string()
    }
}

impl HardCopyGenerator for SKeyOtpPlugin {
    fn generate_hard_copy(&self, softinput: &SoftCopyInput) -> Result<HardCopy, GenerationError> {
        let words = bytes_to_wordlist(softinput.to_bytes());
        let col_width = max_word_len();
        let lines: Vec<String> = words
            .chunks(WORDS_PER_LINE)
            .enumerate()
            .map(|(i, chunk)| {
                let padded: Vec<String> = chunk
                    .iter()
                    .map(|w| format!("{:width$}", w, width = col_width))
                    .collect();
                format!("{:04}: {}", i + 1, padded.join(" "))
            })
            .collect();
        let mut hardcopy = HardCopy::new();
        hardcopy.push(HardCopySection {
            name: PLUGIN_NAME.to_string(),
            content: HardCopySectionContent::Words(lines),
        });
        Ok(hardcopy)
    }
}

impl SoftCopyGenerator for SKeyOtpPlugin {
    fn generate_soft_copy(&self, input: &mut HardCopyInput) -> Result<SoftCopy, GenerationError> {
        let word_refs: Vec<&str> = input.words().iter().map(String::as_str).collect();
        let bytes = wordlist_to_bytes(&word_refs);
        Ok(SoftCopy { bytes })
    }
}