use std::fmt;
use std::io;
use std::str::FromStr;
use crate::cbor::{self, FromCbor, ToCbor, Value};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ChangeId(Hash);
impl ChangeId {
#[must_use]
pub const fn hash(&self) -> &Hash {
&self.0
}
#[must_use]
pub const fn from_hash(hash: Hash) -> Self {
Self(hash)
}
}
impl fmt::Display for ChangeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl FromStr for ChangeId {
type Err = <Hash as std::str::FromStr>::Err;
fn from_str(text: &str) -> Result<Self, Self::Err> {
text.parse().map(Self)
}
}
impl ToCbor for ChangeId {
fn to_cbor(&self) -> Value {
self.0.to_cbor()
}
}
impl FromCbor for ChangeId {
fn from_cbor(value: &Value) -> Result<Self, cbor::Error> {
Hash::from_cbor(value).map(Self)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum HashAlgorithm {
Blake3,
}
impl HashAlgorithm {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Blake3 => "blake3",
}
}
const fn multihash_code(self) -> u64 {
match self {
Self::Blake3 => 0x1e,
}
}
const fn digest_len(self) -> usize {
match self {
Self::Blake3 => 32,
}
}
fn from_name(name: &str) -> Option<Self> {
(name == Self::Blake3.name()).then_some(Self::Blake3)
}
fn from_multihash_code(code: u64) -> Option<Self> {
(code == Self::Blake3.multihash_code()).then_some(Self::Blake3)
}
}
const MAX_DIGEST_LEN: usize = 64;
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Hash {
algorithm: HashAlgorithm,
digest: [u8; MAX_DIGEST_LEN],
}
impl Hash {
#[must_use]
pub fn of(data: &[u8]) -> Self {
Self::from_blake3(&blake3::hash(data))
}
#[must_use]
pub fn algorithm(&self) -> HashAlgorithm {
self.algorithm
}
#[must_use]
pub fn digest(&self) -> &[u8] {
&self.digest[..self.algorithm.digest_len()]
}
#[must_use]
pub fn to_multihash(&self) -> Vec<u8> {
let digest = self.digest();
let mut out = Vec::with_capacity(2 + digest.len());
write_varint(&mut out, self.algorithm.multihash_code());
write_varint(&mut out, digest.len() as u64);
out.extend_from_slice(digest);
out
}
pub fn from_multihash(bytes: &[u8]) -> Result<Self, ParseHashError> {
let mut rest = bytes;
let code = read_varint(&mut rest)?;
let len = read_varint(&mut rest)?;
let algorithm =
HashAlgorithm::from_multihash_code(code).ok_or(ParseHashError::UnknownAlgorithm)?;
if usize::try_from(len).ok() != Some(algorithm.digest_len()) {
return Err(ParseHashError::WrongLength);
}
if rest.len() != algorithm.digest_len() {
return Err(ParseHashError::Malformed);
}
Ok(Self::new(algorithm, rest))
}
fn from_blake3(hash: &blake3::Hash) -> Self {
Self::new(HashAlgorithm::Blake3, hash.as_bytes())
}
fn new(algorithm: HashAlgorithm, digest: &[u8]) -> Self {
debug_assert_eq!(digest.len(), algorithm.digest_len());
let mut padded = [0; MAX_DIGEST_LEN];
padded[..digest.len()].copy_from_slice(digest);
Self {
algorithm,
digest: padded,
}
}
}
impl fmt::Display for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:", self.algorithm.name())?;
self.digest()
.iter()
.try_for_each(|byte| write!(f, "{byte:02x}"))
}
}
impl fmt::Debug for Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Hash({self})")
}
}
impl FromStr for Hash {
type Err = ParseHashError;
fn from_str(text: &str) -> Result<Self, Self::Err> {
let (name, hex) = text.split_once(':').ok_or(ParseHashError::Malformed)?;
let algorithm = HashAlgorithm::from_name(name).ok_or(ParseHashError::UnknownAlgorithm)?;
if hex.len() != 2 * algorithm.digest_len() {
return Err(ParseHashError::WrongLength);
}
let mut digest = [0; MAX_DIGEST_LEN];
for (byte, pair) in digest.iter_mut().zip(hex.as_bytes().chunks(2)) {
*byte = (hex_digit(pair[0])? << 4) | hex_digit(pair[1])?;
}
Ok(Self { algorithm, digest })
}
}
impl ToCbor for Hash {
fn to_cbor(&self) -> Value {
Value::Bytes(self.to_multihash())
}
}
impl FromCbor for Hash {
fn from_cbor(value: &Value) -> Result<Self, cbor::Error> {
match value {
Value::Bytes(bytes) => {
Self::from_multihash(bytes).map_err(|_| cbor::Error::Expected("a multihash"))
}
_ => Err(cbor::Error::Expected("a multihash")),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum ParseHashError {
#[error("unknown hash algorithm")]
UnknownAlgorithm,
#[error("digest has the wrong length for its algorithm")]
WrongLength,
#[error("not a hash in the expected form")]
Malformed,
}
#[derive(Clone, Debug, Default)]
pub struct Hasher(blake3::Hasher);
impl Hasher {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn update(&mut self, data: &[u8]) -> &mut Self {
self.0.update(data);
self
}
#[must_use]
pub fn finalize(&self) -> Hash {
Hash::from_blake3(&self.0.finalize())
}
}
impl io::Write for Hasher {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.update(data);
Ok(data.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
fn hex_digit(digit: u8) -> Result<u8, ParseHashError> {
match digit {
b'0'..=b'9' => Ok(digit - b'0'),
b'a'..=b'f' => Ok(digit - b'a' + 10),
_ => Err(ParseHashError::Malformed),
}
}
fn write_varint(out: &mut Vec<u8>, mut value: u64) {
loop {
let low = u8::try_from(value & 0x7f).expect("seven bits");
value >>= 7;
if value == 0 {
out.push(low);
return;
}
out.push(low | 0x80);
}
}
fn read_varint(bytes: &mut &[u8]) -> Result<u64, ParseHashError> {
let mut value = 0u64;
for (index, &byte) in bytes.iter().enumerate().take(9) {
value |= u64::from(byte & 0x7f) << (7 * index);
if byte & 0x80 == 0 {
if byte == 0 && index > 0 {
return Err(ParseHashError::Malformed);
}
*bytes = &bytes[index + 1..];
return Ok(value);
}
}
Err(ParseHashError::Malformed)
}