use ostrya_gvariant::{GvEncode, GvType};
use crate::error::{Error, Result};
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Checksum([u8; 32]);
const STD_ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const MOD_ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+_";
impl Checksum {
pub fn from_bytes(b: [u8; 32]) -> Checksum {
Checksum(b)
}
pub fn sha256(data: &[u8]) -> Checksum {
use sha2::{Digest, Sha256};
Checksum(Sha256::digest(data).into())
}
pub fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
pub fn from_hex(s: &str) -> Result<Checksum> {
let bytes = s.as_bytes();
if bytes.len() != 64 {
return Err(Error::InvalidChecksum("hex checksum is not 64 characters"));
}
let mut out = [0u8; 32];
for (i, pair) in bytes.chunks_exact(2).enumerate() {
let hi = hex_val(pair[0])?;
let lo = hex_val(pair[1])?;
out[i] = (hi << 4) | lo;
}
Ok(Checksum(out))
}
pub fn to_hex(&self) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut s = String::with_capacity(64);
for &b in &self.0 {
s.push(HEX[(b >> 4) as usize] as char);
s.push(HEX[(b & 0x0f) as usize] as char);
}
s
}
pub fn from_ay(b: &[u8]) -> Result<Checksum> {
let arr: [u8; 32] = b
.try_into()
.map_err(|_| Error::InvalidChecksum("ay checksum is not 32 bytes"))?;
Ok(Checksum(arr))
}
pub fn to_base64(&self) -> String {
base64_encode(&self.0, STD_ALPHABET, true)
}
pub fn from_base64(s: &str) -> Result<Checksum> {
Ok(Checksum(base64_decode_checksum(s, STD_ALPHABET, true)?))
}
pub fn to_base64_modified(&self) -> String {
base64_encode(&self.0, MOD_ALPHABET, false)
}
pub fn from_base64_modified(s: &str) -> Result<Checksum> {
Ok(Checksum(base64_decode_checksum(s, MOD_ALPHABET, false)?))
}
}
fn hex_val(c: u8) -> Result<u8> {
match c {
b'0'..=b'9' => Ok(c - b'0'),
b'a'..=b'f' => Ok(c - b'a' + 10),
b'A'..=b'F' => Ok(c - b'A' + 10),
_ => Err(Error::InvalidChecksum(
"hex checksum has a non-hex character",
)),
}
}
fn base64_encode(input: &[u8], alphabet: &[u8; 64], pad: bool) -> String {
let mut out = String::with_capacity(input.len().div_ceil(3) * 4);
for chunk in input.chunks(3) {
let b0 = chunk[0];
out.push(alphabet[(b0 >> 2) as usize] as char);
match chunk.len() {
1 => {
out.push(alphabet[((b0 & 0x03) << 4) as usize] as char);
if pad {
out.push_str("==");
}
}
2 => {
let b1 = chunk[1];
out.push(alphabet[(((b0 & 0x03) << 4) | (b1 >> 4)) as usize] as char);
out.push(alphabet[((b1 & 0x0f) << 2) as usize] as char);
if pad {
out.push('=');
}
}
_ => {
let (b1, b2) = (chunk[1], chunk[2]);
out.push(alphabet[(((b0 & 0x03) << 4) | (b1 >> 4)) as usize] as char);
out.push(alphabet[(((b1 & 0x0f) << 2) | (b2 >> 6)) as usize] as char);
out.push(alphabet[(b2 & 0x3f) as usize] as char);
}
}
}
out
}
fn base64_decode_checksum(s: &str, alphabet: &[u8; 64], padded: bool) -> Result<[u8; 32]> {
let bytes = s.as_bytes();
let expected = if padded { 44 } else { 43 };
if bytes.len() != expected {
return Err(Error::InvalidChecksum(
"base64 checksum has the wrong length",
));
}
if padded && bytes[43] != b'=' {
return Err(Error::InvalidChecksum("base64 checksum is missing its pad"));
}
let data = &bytes[..43];
let mut out = [0u8; 32];
for (group, chunk) in data[..40].chunks_exact(4).enumerate() {
let s0 = base64_val(chunk[0], alphabet)?;
let s1 = base64_val(chunk[1], alphabet)?;
let s2 = base64_val(chunk[2], alphabet)?;
let s3 = base64_val(chunk[3], alphabet)?;
out[group * 3] = (s0 << 2) | (s1 >> 4);
out[group * 3 + 1] = (s1 << 4) | (s2 >> 2);
out[group * 3 + 2] = (s2 << 6) | s3;
}
let s0 = base64_val(data[40], alphabet)?;
let s1 = base64_val(data[41], alphabet)?;
let s2 = base64_val(data[42], alphabet)?;
if s2 & 0x03 != 0 {
return Err(Error::InvalidChecksum(
"base64 checksum has nonzero trailing bits",
));
}
out[30] = (s0 << 2) | (s1 >> 4);
out[31] = (s1 << 4) | (s2 >> 2);
Ok(out)
}
fn base64_val(c: u8, alphabet: &[u8; 64]) -> Result<u8> {
match c {
b'A'..=b'Z' => Ok(c - b'A'),
b'a'..=b'z' => Ok(c - b'a' + 26),
b'0'..=b'9' => Ok(c - b'0' + 52),
b'+' => Ok(62),
_ if c == alphabet[63] => Ok(63),
_ => Err(Error::InvalidChecksum("base64 has an invalid character")),
}
}
impl std::fmt::Display for Checksum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.to_hex())
}
}
impl std::fmt::Debug for Checksum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Checksum({})", self.to_hex())
}
}
impl std::str::FromStr for Checksum {
type Err = Error;
fn from_str(s: &str) -> Result<Checksum> {
Checksum::from_hex(s)
}
}
impl GvType for Checksum {
const SIGNATURE: &'static str = "ay";
const ALIGNMENT: usize = 1;
const FIXED_SIZE: Option<usize> = None;
}
impl GvEncode for Checksum {
fn encode(&self, out: &mut Vec<u8>) -> ostrya_gvariant::Result<()> {
out.extend_from_slice(&self.0);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
const FIXTURE_COMMIT: &str = "b3c8e8525e8a5c3409bf6e6db5f5d656da77ae76d08cbc4f8b75b71879757a89";
#[test]
fn hex_round_trips_and_is_lowercase() {
let c = Checksum::from_hex(FIXTURE_COMMIT).unwrap();
assert_eq!(c.to_hex(), FIXTURE_COMMIT);
let upper = Checksum::from_hex(&FIXTURE_COMMIT.to_uppercase()).unwrap();
assert_eq!(upper, c);
}
#[test]
fn hex_rejects_bad_length_and_characters() {
assert!(Checksum::from_hex("abcd").is_err());
assert!(Checksum::from_hex(&"g".repeat(64)).is_err());
}
#[test]
fn ay_conversion_validates_length() {
let c = Checksum::from_hex(FIXTURE_COMMIT).unwrap();
assert_eq!(Checksum::from_ay(c.as_bytes()).unwrap(), c);
assert!(Checksum::from_ay(&[0u8; 31]).is_err());
assert!(Checksum::from_ay(&[0u8; 33]).is_err());
}
#[test]
fn base64_reference_vectors() {
let zero = Checksum::from_bytes([0u8; 32]);
assert_eq!(zero.to_base64_modified(), "A".repeat(43));
assert_eq!(zero.to_base64(), format!("{}=", "A".repeat(43)));
let ones = Checksum::from_bytes([0xffu8; 32]);
assert_eq!(ones.to_base64_modified(), format!("{}8", "_".repeat(42)));
assert_eq!(ones.to_base64(), format!("{}8=", "/".repeat(42)));
assert_eq!(
ones.to_base64_modified(),
ones.to_base64().trim_end_matches('=').replace('/', "_")
);
}
#[test]
fn base64_round_trips_both_alphabets() {
let c = Checksum::from_hex(FIXTURE_COMMIT).unwrap();
assert_eq!(Checksum::from_base64(&c.to_base64()).unwrap(), c);
assert_eq!(
Checksum::from_base64_modified(&c.to_base64_modified()).unwrap(),
c
);
assert_eq!(c.to_base64_modified().len(), 43);
assert_eq!(c.to_base64().len(), 44);
}
fn sample_checksums() -> Vec<Checksum> {
let mut v = vec![
Checksum::from_bytes([0u8; 32]),
Checksum::from_bytes([0xffu8; 32]),
Checksum::from_hex(FIXTURE_COMMIT).unwrap(),
];
for seed in [1u8, 7, 31, 63, 128, 200, 254] {
let mut b = [0u8; 32];
for (i, x) in b.iter_mut().enumerate() {
*x = seed.wrapping_mul(i as u8).wrapping_add(seed);
}
v.push(Checksum::from_bytes(b));
}
v
}
#[test]
fn base64_parse_then_render_is_identity_for_accepted_strings() {
for c in sample_checksums() {
let std = c.to_base64();
assert_eq!(Checksum::from_base64(&std).unwrap().to_base64(), std);
let modified = c.to_base64_modified();
assert_eq!(
Checksum::from_base64_modified(&modified)
.unwrap()
.to_base64_modified(),
modified
);
}
}
#[test]
fn base64_rejects_cross_alphabet_glyphs() {
let modified = Checksum::from_bytes([0xffu8; 32]).to_base64_modified();
assert!(modified.contains('_'));
assert!(Checksum::from_base64(&modified).is_err());
let std = Checksum::from_bytes([0xffu8; 32]).to_base64();
assert!(std.contains('/'));
assert!(Checksum::from_base64_modified(std.trim_end_matches('=')).is_err());
}
#[test]
fn base64_rejects_nonzero_trailing_bits() {
let mut modified = Checksum::from_bytes([0xffu8; 32]).to_base64_modified();
modified.pop();
modified.push('9');
assert!(Checksum::from_base64_modified(&modified).is_err());
let mut std = Checksum::from_bytes([0u8; 32]).to_base64();
std.replace_range(42..43, "9");
assert!(Checksum::from_base64(&std).is_err());
}
#[test]
fn base64_rejects_wrong_length_and_stray_padding() {
let c = Checksum::from_hex(FIXTURE_COMMIT).unwrap();
assert!(Checksum::from_base64(c.to_base64().trim_end_matches('=')).is_err());
assert!(Checksum::from_base64(&format!("{}=", c.to_base64())).is_err());
assert!(Checksum::from_base64_modified(&format!("{}=", c.to_base64_modified())).is_err());
assert!(Checksum::from_base64_modified(&c.to_base64()).is_err());
}
#[test]
fn sha256_matches_the_empty_input_vector() {
assert_eq!(
Checksum::sha256(b"").to_hex(),
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
);
}
#[test]
fn ordering_matches_hex_ordering() {
let a = Checksum::from_hex(&"00".repeat(32)).unwrap();
let b = Checksum::from_hex(&"ff".repeat(32)).unwrap();
assert!(a < b);
assert!(a.to_hex() < b.to_hex());
}
}