#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct BigBits {
limbs: Vec<u64>,
}
impl BigBits {
fn zero() -> Self {
BigBits { limbs: Vec::new() }
}
fn or_shifted(mut self, value: u64, shift: u32) -> Self {
if value == 0 {
return self;
}
let limb_shift = (shift / 64) as usize;
let bit_shift = shift % 64;
let needed = limb_shift + 2;
if self.limbs.len() < needed {
self.limbs.resize(needed, 0);
}
let low = value << bit_shift;
let high = if bit_shift == 0 {
0
} else {
value >> (64 - bit_shift)
};
self.limbs[limb_shift] |= low;
if high != 0 {
self.limbs[limb_shift + 1] |= high;
}
self
}
fn or_assign(&mut self, other: &BigBits) {
if self.limbs.len() < other.limbs.len() {
self.limbs.resize(other.limbs.len(), 0);
}
for (index, limb) in other.limbs.iter().enumerate() {
self.limbs[index] |= limb;
}
}
fn xor_assign(&mut self, other: &BigBits) {
if self.limbs.len() < other.limbs.len() {
self.limbs.resize(other.limbs.len(), 0);
}
for (index, limb) in other.limbs.iter().enumerate() {
self.limbs[index] ^= limb;
}
}
fn and_small(mut self, mask: u64) -> Self {
if self.limbs.is_empty() {
self.limbs.push(0);
}
self.limbs[0] &= mask;
for limb in self.limbs.iter_mut().skip(1) {
*limb = 0;
}
self
}
fn xor_small(&mut self, value: u64) {
if value == 0 {
return;
}
if self.limbs.is_empty() {
self.limbs.push(0);
}
self.limbs[0] ^= value;
}
fn is_zero(&self) -> bool {
self.limbs.iter().all(|limb| *limb == 0)
}
fn to_hex(&self) -> String {
if self.is_zero() {
return "0".to_string();
}
let significant = self
.limbs
.iter()
.rposition(|limb| *limb != 0)
.expect("non-zero accumulator");
let mut digits = Vec::new();
for (position, limb) in self.limbs[..=significant].iter().enumerate().rev() {
let is_top = position == significant;
if is_top {
digits.push(format!("{limb:X}"));
} else {
digits.push(format!("{limb:016X}"));
}
}
digits.concat()
}
}
pub fn hash_password(plaintext_password: &str) -> String {
let mut password = BigBits::zero();
for (shift, ch) in (1_u32..).zip(plaintext_password.chars()) {
let code = ch as u64;
let mut masked = BigBits::zero().or_shifted(code, shift).and_small(0x7fff);
let rotated = if shift >= 15 {
BigBits::zero().or_shifted(code, shift - 15)
} else {
BigBits::zero().or_shifted(code >> (15 - shift), 0)
};
masked.or_assign(&rotated);
password.xor_assign(&masked);
}
password.xor_small(plaintext_password.chars().count() as u64);
password.xor_small(0xCE4B);
password.to_hex()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn matches_python_reference_values() {
for (password, expected) in [
("", "CE4B"),
("a", "CE88"),
("b", "CE8E"),
("password", "83AF"),
("test", "CBEB"),
("secret", "DAA7"),
("aa", "CF0F"),
("aaaaa", "C630"),
("a".repeat(10).as_str(), "C9FD"),
("a".repeat(20).as_str(), "B9DE"),
("a".repeat(50).as_str(), "20FFFFF4E38"),
] {
assert_eq!(hash_password(password), expected, "password {password:?}");
}
}
#[test]
fn unbounded_precision_matches_python() {
assert_eq!(hash_password(&"a".repeat(100)), "83FFFFFFFFFFFFFFFFF4E6E");
}
#[test]
fn hash_is_stable_and_upper_hex() {
let h = hash_password("test");
assert!(h.chars().all(|c| c.is_ascii_hexdigit()));
assert!(h.chars().all(|c| !c.is_ascii_lowercase()));
assert!(!h.starts_with("0x"));
}
#[test]
fn different_passwords_differ() {
assert_ne!(hash_password("a"), hash_password("b"));
assert_ne!(hash_password("password"), hash_password("password "));
}
}