pub(crate) const ALPHABET: &[u8; 64] =
b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
pub(crate) const SALT_B64_LEN: usize = 22;
pub(crate) const HASH_B64_LEN: usize = 31;
pub(crate) const INVALID: u8 = 0xFF;
pub(crate) const DECODE: [u8; 256] = {
let mut table = [INVALID; 256];
let mut i = 0;
while i < 64 {
table[ALPHABET[i] as usize] = i as u8;
i += 1;
}
table
};
pub mod scalar;
#[cfg(target_arch = "aarch64")]
pub(crate) mod neon;
#[cfg(target_arch = "x86_64")]
pub(crate) mod x86;
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
pub(crate) mod wasm128;
pub(crate) fn encode_16(bytes: &[u8; 16]) -> [u8; SALT_B64_LEN] {
#[cfg(all(target_arch = "aarch64", not(miri)))]
return neon::encode_16(bytes);
#[cfg(target_arch = "x86_64")]
return x86::encode_16(bytes);
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
return wasm128::encode_16(bytes);
#[allow(unreachable_code)]
scalar::encode_16(bytes)
}
pub(crate) fn encode_23(bytes: &[u8; 23]) -> [u8; HASH_B64_LEN] {
#[cfg(all(target_arch = "aarch64", not(miri)))]
return neon::encode_23(bytes);
#[cfg(target_arch = "x86_64")]
return x86::encode_23(bytes);
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
return wasm128::encode_23(bytes);
#[allow(unreachable_code)]
scalar::encode_23(bytes)
}
pub(crate) fn decode_16(s: &[u8]) -> Result<[u8; 16], ()> {
#[cfg(all(target_arch = "aarch64", not(miri)))]
return neon::decode_16(s);
#[cfg(target_arch = "x86_64")]
return x86::decode_16(s);
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
return wasm128::decode_16(s);
#[allow(unreachable_code)]
scalar::decode_16(s)
}
pub(crate) fn decode_23(s: &[u8]) -> Result<[u8; 23], ()> {
#[cfg(all(target_arch = "aarch64", not(miri)))]
return neon::decode_23(s);
#[cfg(target_arch = "x86_64")]
return x86::decode_23(s);
#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
return wasm128::decode_23(s);
#[allow(unreachable_code)]
scalar::decode_23(s)
}
#[cfg(test)]
mod tests {
use super::*;
struct Rng(u64);
impl Rng {
fn fill(&mut self, buf: &mut [u8]) {
for b in buf {
let mut x = self.0;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.0 = x;
*b = (x.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 56) as u8;
}
}
}
#[test]
fn salt_cross_check() {
let bytes: [u8; 16] = [
38, 113, 212, 141, 108, 213, 195, 166, 201, 38, 20, 13, 47, 40, 104, 18,
];
assert_eq!(&encode_16(&bytes), b"HlFShUxTu4ZHHfOLJwfmCe");
assert_eq!(decode_16(b"HlFShUxTu4ZHHfOLJwfmCe"), Ok(bytes));
}
#[test]
fn round_trip_16_and_23() {
let mut rng = Rng(0x9E37_79B9_7F4A_7C15);
let mut b16 = [0u8; 16];
let mut b23 = [0u8; 23];
for _ in 0..256 {
rng.fill(&mut b16);
rng.fill(&mut b23);
assert_eq!(decode_16(&encode_16(&b16)), Ok(b16));
assert_eq!(decode_23(&encode_23(&b23)), Ok(b23));
}
}
#[test]
fn encode_16_last_char_is_structurally_constrained() {
let mut rng = Rng(0xDEAD_BEEF_CAFE_F00D);
let mut bytes = [0u8; 16];
for _ in 0..64 {
rng.fill(&mut bytes);
let last = encode_16(&bytes)[SALT_B64_LEN - 1];
assert!(matches!(last, b'.' | b'O' | b'e' | b'u'));
}
}
#[test]
fn encode_23_last_char_index_is_multiple_of_4() {
let mut rng = Rng(0x0123_4567_89AB_CDEF);
let mut bytes = [0u8; 23];
for _ in 0..64 {
rng.fill(&mut bytes);
let last = encode_23(&bytes)[HASH_B64_LEN - 1];
assert_eq!(DECODE[last as usize] % 4, 0);
}
}
#[test]
fn decode_rejects_bad_chars_and_wrong_lengths() {
assert!(decode_16(b"CCCCCCCCCCCCCCCCCCCCC!").is_err());
assert!(decode_23(b"7uG0VCzI2bS7j6ymqJi9CdcdxiRTWN*").is_err());
assert!(decode_16(b"CCCCCCCCCCCCCCCCCCCCC").is_err());
assert!(decode_16(b"CCCCCCCCCCCCCCCCCCCCC..").is_err());
assert!(decode_23(b"7uG0VCzI2bS7j6ymqJi9CdcdxiRTWN").is_err());
assert!(decode_23(&[b'C'; 32]).is_err());
assert!(decode_16(&[]).is_err());
}
#[cfg(not(miri))]
#[test]
fn dispatch_matches_scalar() {
let mut rng = Rng(0xB529_7A4D_1D2B_9F83);
let mut b16 = [0u8; 16];
let mut b23 = [0u8; 23];
for _ in 0..512 {
rng.fill(&mut b16);
rng.fill(&mut b23);
assert_eq!(encode_16(&b16), scalar::encode_16(&b16));
assert_eq!(encode_23(&b23), scalar::encode_23(&b23));
}
let base16 = *b"CCCCCCCCCCCCCCCCCCCCCC";
let base23 = *b"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";
for pos in 0..SALT_B64_LEN {
for c in 0..=u8::MAX {
let mut s = base16;
s[pos] = c;
assert_eq!(decode_16(&s), scalar::decode_16(&s), "16: pos {pos} c {c:#04x}");
}
}
for pos in 0..HASH_B64_LEN {
for c in 0..=u8::MAX {
let mut s = base23;
s[pos] = c;
assert_eq!(decode_23(&s), scalar::decode_23(&s), "23: pos {pos} c {c:#04x}");
}
}
}
}