use crate::checkers::CheckerTypes;
use crate::decoders::interface::check_string_success;
use super::crack_results::CrackResult;
use super::interface::Crack;
use super::interface::Decoder;
use log::{debug, info, trace};
pub struct Base58RippleDecoder;
impl Crack for Decoder<Base58RippleDecoder> {
fn new() -> Decoder<Base58RippleDecoder> {
Decoder {
name: "Base58 Ripple",
description: "Base58 is a group of binary-to-text encoding schemes that represent binary data (more specifically, a sequence of 8-bit bytes) in an ASCII string format by translating the data into a radix-32 representation.",
link: "https://en.wikipedia.org/wiki/Base58",
tags: vec!["base58_ripple", "base58", "ripple", "cryptocurrency", "decoder", "base"],
popularity: 0.8,
phantom: std::marker::PhantomData,
}
}
fn crack(&self, text: &str, checker: &CheckerTypes) -> CrackResult {
trace!("Trying Base58_ripple with text {:?}", text);
let decoded_text = decode_base58_ripple_no_error_handling(text);
let mut results = CrackResult::new(self, text.to_string());
if decoded_text.is_none() {
debug!("Failed to decode base58_ripple because Base58RippleDecoder::decode_base58_ripple_no_error_handling returned None");
return results;
}
let decoded_text = decoded_text.unwrap();
if !check_string_success(&decoded_text, text) {
info!(
"Failed to decode base58_ripple because check_string_success returned false on string {}",
decoded_text
);
return results;
}
let checker_result = checker.check(&decoded_text);
results.unencrypted_text = Some(vec![decoded_text]);
results.update_checker(&checker_result);
results
}
fn get_tags(&self) -> &Vec<&str> {
&self.tags
}
fn get_name(&self) -> &str {
self.name
}
}
fn decode_base58_ripple_no_error_handling(text: &str) -> Option<String> {
if let Ok(decoded_text) = bs58::decode(text)
.with_alphabet(bs58::Alphabet::RIPPLE)
.into_vec()
{
return Some(String::from_utf8_lossy(&decoded_text).to_string());
}
None
}
#[cfg(test)]
mod tests {
use super::Base58RippleDecoder;
use crate::{
checkers::{
athena::Athena,
checker_type::{Check, Checker},
CheckerTypes,
},
decoders::interface::{Crack, Decoder},
};
fn get_athena_checker() -> CheckerTypes {
let athena_checker = Checker::<Athena>::new();
CheckerTypes::CheckAthena(athena_checker)
}
#[test]
fn successful_decoding() {
let base58_ripple_decoder = Decoder::<Base58RippleDecoder>::new();
let result = base58_ripple_decoder.crack("StVrDLaUATiyKyV", &get_athena_checker());
let decoded_str = &result
.unencrypted_text
.expect("No unencrypted text for base58_ripple");
assert_eq!(decoded_str[0], "hello world");
}
#[test]
fn base58_ripple_decode_empty_string() {
let base58_ripple_decoder = Decoder::<Base58RippleDecoder>::new();
let result = base58_ripple_decoder
.crack("", &get_athena_checker())
.unencrypted_text;
assert!(result.is_none());
}
#[test]
fn base58_ripple_decode_handles_panics() {
let base58_ripple_decoder = Decoder::<Base58RippleDecoder>::new();
let result = base58_ripple_decoder
.crack(
"hello my name is panicky mc panic face!",
&get_athena_checker(),
)
.unencrypted_text;
if result.is_some() {
panic!("Decode_base58_ripple did not return an option with Some<t>.")
} else {
assert_eq!(true, true);
}
}
#[test]
fn base58_ripple_handle_panic_if_empty_string() {
let base58_ripple_decoder = Decoder::<Base58RippleDecoder>::new();
let result = base58_ripple_decoder
.crack("", &get_athena_checker())
.unencrypted_text;
if result.is_some() {
assert_eq!(true, true);
}
}
#[test]
fn base58_ripple_work_if_string_not_base58_ripple() {
let base58_ripple_decoder = Decoder::<Base58RippleDecoder>::new();
let result = base58_ripple_decoder
.crack("hello good day!", &get_athena_checker())
.unencrypted_text;
if result.is_some() {
assert_eq!(true, true);
}
}
#[test]
fn base58_ripple_handle_panic_if_emoji() {
let base58_ripple_decoder = Decoder::<Base58RippleDecoder>::new();
let result = base58_ripple_decoder
.crack("😂", &get_athena_checker())
.unencrypted_text;
if result.is_some() {
assert_eq!(true, true);
}
}
}