extern crate base64;
pub use self::base64::DecodeError;
use std::fmt;
use std::string;
struct Digest(Vec<u8>);
#[derive(Debug)]
pub enum Base64Error {
Decode(base64::DecodeError),
Utf8(string::FromUtf8Error),
}
impl fmt::LowerHex for Digest {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
for byte in & self.0 {
fmt::LowerHex::fmt(byte, f)?;
}
Ok(())
}
}
pub fn decode_hex(input: &str) -> Result<String, DecodeError> {
let output = Digest(base64::decode(input)?);
Ok(format!("{:02x}", output))
}
pub fn decode(input: &str) -> Result<String, Base64Error> {
let bytes = base64::decode(input).map_err(Base64Error::Decode)?;
let output = String::from_utf8(bytes).map_err(Base64Error::Utf8)?;
Ok(output)
}