happy_cracking/crypto/
base32.rs1use anyhow::{Context, Result};
2use clap::Subcommand;
3use umt_rust::crypto::{umt_decode_base32, umt_encode_base32};
4
5#[derive(Subcommand)]
6pub enum Base32Action {
7 #[command(about = "Encode to Base32")]
8 Encode {
9 #[arg(help = "Input text")]
10 input: String,
11 },
12 #[command(about = "Decode from Base32")]
13 Decode {
14 #[arg(help = "Base32 encoded string")]
15 input: String,
16 },
17}
18
19pub fn run(action: Base32Action) -> Result<()> {
20 match action {
21 Base32Action::Encode { input } => {
22 println!("{}", encode(&input));
23 }
24 Base32Action::Decode { input } => {
25 println!("{}", decode(&input)?);
26 }
27 }
28 Ok(())
29}
30
31pub fn encode(input: &str) -> String {
32 umt_encode_base32(input.as_bytes())
33}
34
35pub fn decode(input: &str) -> Result<String> {
36 let decoded = umt_decode_base32(input.trim()).context("Failed to decode Base32")?;
37 String::from_utf8(decoded).context("Decoded data is not valid UTF-8")
38}