mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 14:36:01 -06:00
Add temporary hash function.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
+57
@@ -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)
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user