diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e675254 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target +/Cargo.lock +/*.iml +/.idea diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e7c1a83 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "crypto" +version = "0.0.1" +authors = ["zofrex"] + +[[bin]] + +name = "hex_to_base64" diff --git a/hex_to_base64 b/hex_to_base64 new file mode 100755 index 0000000..f975421 Binary files /dev/null and b/hex_to_base64 differ diff --git a/src/hex.rs b/src/hex.rs new file mode 100644 index 0000000..cd51f13 --- /dev/null +++ b/src/hex.rs @@ -0,0 +1,31 @@ +pub fn hex_to_dec(hex: char) -> int { + if hex >= '0' && hex <= '9' { + hex as int - '0' as int + } + else if hex >= 'a' && hex <= 'f' { + hex as int - 'a' as int + 10 + } + else { + fail!("Character {} out of range", hex); + } +} + +#[test] +fn all_hexes() { + let hexes = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']; + for i in range(0u, hexes.len()) { + assert_eq!(hex_to_dec(hexes[i]), i as int); + } +} + +#[test] +#[should_fail] +fn test_too_large() { + hex_to_dec('g'); +} + +#[test] +#[should_fail] +fn test_too_small() { + hex_to_dec('/'); +} \ No newline at end of file diff --git a/src/hex_to_base64.rs b/src/hex_to_base64.rs new file mode 100644 index 0000000..7d4e631 --- /dev/null +++ b/src/hex_to_base64.rs @@ -0,0 +1,5 @@ +mod hex; + +fn main() { + println!("Output: {}", hex::hex_to_dec('0')); +}