diff --git a/examples/hashtest.rs b/examples/hashtest.rs new file mode 100644 index 0000000..0dd59c7 --- /dev/null +++ b/examples/hashtest.rs @@ -0,0 +1,22 @@ +use unshell::hash; + +macro_rules! hashtest { + ($input:tt) => { + ($input, hash($input)) + }; +} + +const MAP: [(&'static str, u32); 6] = [ + hashtest!("abc123"), + hashtest!("abc124"), + hashtest!("abc125"), + hashtest!("abc122"), + hashtest!("somethingelse"), + hashtest!("org.io.abc1234"), +]; + +pub fn main() { + for (a, b) in MAP { + println!("unshell::hash(\"{}\") = {}", a, b) + } +} diff --git a/src/hash.rs b/src/hash.rs new file mode 100644 index 0000000..70e8d28 --- /dev/null +++ b/src/hash.rs @@ -0,0 +1,57 @@ +//! Temporary hash function + +const fn hash_recursive<'a>(state: &mut [u8; 4], input: &'a [u8]) { + match input.len() { + 3 => { + state[0] ^= input[0]; + state[1] ^= input[1]; + state[2] ^= input[2]; + } + 2 => { + state[0] ^= input[0]; + state[1] ^= input[1]; + } + 1 => { + state[0] ^= input[0]; + } + 0 => {} + _ => { + state[0] ^= input[0]; + state[1] ^= input[1]; + state[2] ^= input[2]; + state[3] ^= input[3]; + + // Mess with the state quite a bit + state[0] = u8::reverse_bits(state[0]) ^ state[2]; + state[2] = state[0].wrapping_add(state[2]).wrapping_add(state[3]) ^ state[0]; + state[3] = state[2].wrapping_add(state[3] << 2) ^ state[1]; + state[1] = state[3] ^ 0xa3; + + hash_recursive(state, &input[1..]); + } + } +} + +pub const fn hash(input: &'static str) -> u32 { + let mut data = [0xDE, 0xED, 0xBE, 0xEF]; + hash_recursive(&mut data, input.as_bytes()); + + // throw the data back into itself because why not + let input2 = [ + u8::reverse_bits(data[1]), + data[2], + data[2], + data[1], + u8::reverse_bits(data[0]), + data[2], + u8::reverse_bits(data[3]), + u8::reverse_bits(data[2]), + data[3], + u8::reverse_bits(data[3]), + u8::reverse_bits(data[2]), + data[0], + ]; + hash_recursive(&mut data, &input2); + + u32::from_be_bytes(data) +} diff --git a/src/lib.rs b/src/lib.rs index 365f64c..3dbfd0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,9 +10,12 @@ //! The library requires `alloc` for path and payload management. #![no_std] +#![feature(const_index)] +#![feature(const_trait_impl)] pub extern crate alloc; +mod hash; pub mod logger; pub mod protocol { @@ -20,3 +23,5 @@ pub mod protocol { pub use unshell_macros::unshell_leaf; } + +pub use hash::hash; diff --git a/unshell-leaves/leaf-pty/src/constants.rs b/unshell-leaves/leaf-pty/src/constants.rs index a73be52..4341c4d 100644 --- a/unshell-leaves/leaf-pty/src/constants.rs +++ b/unshell-leaves/leaf-pty/src/constants.rs @@ -1,8 +1,8 @@ /// Leaf id used by the generated fake PTY wrapper. -pub const LEAF_FAKE_PTY: u32 = 300; +pub const LEAF_FAKE_PTY: u32 = unshell::hash("dev.unshell.v1.pty"); /// Outer procedure id used by all fake PTY session packets. -pub const PROC_PTY: u32 = 30; +pub const PROC_PTY: u32 = unshell::hash("dev.unshell.v1.pty.pty"); /// Downward opcode that opens one PTY session. pub const OP_OPEN: u8 = 0;