Implement working unpack for bink1998
This commit is contained in:
parent
0208c863aa
commit
3993cafa35
7 changed files with 273 additions and 44 deletions
46
src/key.rs
Normal file
46
src/key.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
use openssl::bn::BigNum;
|
||||
|
||||
const P_CHARSET: [char; 24] = [
|
||||
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 'T', 'V', 'W', 'X', 'Y', '2', '3',
|
||||
'4', '6', '7', '8', '9',
|
||||
];
|
||||
|
||||
const PK_LENGTH: usize = 25;
|
||||
|
||||
pub fn unbase24(cd_key: &str) -> Vec<u8> {
|
||||
let p_decoded_key: Vec<u8> = cd_key
|
||||
.chars()
|
||||
.filter_map(|c| P_CHARSET.iter().position(|&x| x == c).map(|i| i as u8))
|
||||
.collect();
|
||||
|
||||
let mut y = BigNum::from_u32(0).unwrap();
|
||||
|
||||
for i in p_decoded_key {
|
||||
y.mul_word((PK_LENGTH - 1) as u32).unwrap();
|
||||
y.add_word(i.into()).unwrap();
|
||||
}
|
||||
|
||||
y.to_vec()
|
||||
}
|
||||
|
||||
pub fn base24(byte_seq: &[u8]) -> String {
|
||||
let mut z = BigNum::from_slice(byte_seq).unwrap();
|
||||
let mut out: VecDeque<char> = VecDeque::new();
|
||||
(0..=24).for_each(|_| out.push_front(P_CHARSET[z.div_word(24).unwrap() as usize]));
|
||||
out.iter().collect()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn test_base24() {
|
||||
let input = "JTW3TJ7PFJ7V9CCMX84V9PFT8";
|
||||
let unbase24 = super::unbase24(input);
|
||||
println!("{:?}", unbase24);
|
||||
let base24 = super::base24(&unbase24);
|
||||
println!("{}", base24);
|
||||
assert_eq!(input, base24);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue