Skip to content

Commit

Permalink
Panic on invalid input and added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AleSod committed Mar 24, 2020
1 parent 0596ade commit aa19903
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "logged_fu_skater"
version = "1.0.0"
version = "1.0.1"
authors = ["AleSod <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down
48 changes: 37 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,47 @@ mod nouns;
#[cfg(test)]
mod tests;

/**
obf obfuscates the input string into a human readable hash.
*/
/// Obfuscates the input string into a human readable hash.
///
/// # Arguments
/// * `input` - The string to obfuscate.
///
/// # Examples
///
/// ```
/// let result: String = logged_fu_skater::obf("hello");
/// assert_eq!(&result, "TerriblyHolyThrill");
/// ```
///
pub fn obf(input: &str) -> String {
obfp(input, 0)
}

/**
obfp obfuscates the input string into a human readable hash with 0-8 padding_bytes at the end.
*/
/// Obfuscates the input string into a human readable hash with 0-8 padding_bytes at the end.
///
/// # Arguments
/// * `input` - The string to obfuscate.
/// * `padding_bytes` - Number of bytes to add as padding (0-8).
///
/// # Examples
///
/// ```
/// let result: String = logged_fu_skater::obfp("hello", 2);
/// assert_eq!(&result, "TerriblyHolyThrill3B48");
/// ```
///
/// # Panics
///
/// Panics if padding_bytes is greater than 8.
///
/// ```rust,should_panic
/// logged_fu_skater::obfp("hello", 99);
/// ```
///
pub fn obfp(input: &str, padding_bytes: u8) -> String {
let safe_padding = match padding_bytes {
0..=8 => padding_bytes,
_ => 8,
};
if padding_bytes > 8 {
panic!("padding_bytes cannot exceed 8.");
}
let mut hasher = Sha1::new();
hasher.input_str(input);
let mut result: [u8;20] = [0;20];
Expand All @@ -48,7 +74,7 @@ pub fn obfp(input: &str, padding_bytes: u8) -> String {
let adjective = adjectives::WORDS[adjectives_index % adjectives::WORDS.len()];
let noun = nouns::WORDS[nouns_index % nouns::WORDS.len()];

let padding: Vec<u8> = result[12..(12 + safe_padding as usize)].try_into().expect("Could not convert padding to Vec");
let padding: Vec<u8> = result[12..(12 + padding_bytes as usize)].try_into().expect("Could not convert padding to Vec");

format!("{}{}{}{}", adverb, adjective, noun, hex::encode_upper(padding))
}
7 changes: 1 addition & 6 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct TestData {
}

#[test]
fn test() {
fn test_positive() {
for test in TEST_CASES {
let result = obfp(test.input, test.padding);

Expand Down Expand Up @@ -43,11 +43,6 @@ const TEST_CASES: &[TestData] = &[
padding: 8,
expected_result: "HonestlyErgonomicSloth5012F6C60B27661C",
},
TestData {
input: "asdf",
padding: 200,
expected_result: "HonestlyErgonomicSloth5012F6C60B27661C",
},
// Test a few unique UUID:s
TestData {
input: "ac968750-7ca2-4dde-908b-aacbbed2f470",
Expand Down

0 comments on commit aa19903

Please sign in to comment.