use ffi::crypto_generichash_BYTES_MAX;
use std::cmp::{Eq, Ordering, PartialEq, PartialOrd};
use std::fmt::{Debug, Formatter};
use std::hash::{Hash, Hasher};
use std::ops::{Index, Range, RangeFrom, RangeFull, RangeTo};
#[derive(Clone)]
pub struct Digest {
pub(super) len: usize,
pub(super) data: [u8; crypto_generichash_BYTES_MAX as usize],
}
impl Digest {
pub(super) fn new(len: usize) -> Self {
Self {
len,
data: [0u8; crypto_generichash_BYTES_MAX as usize],
}
}
}
impl Debug for Digest {
fn fmt(&self, formatter: &mut Formatter) -> ::std::fmt::Result {
write!(formatter, "Digest({:?})", &self[..])
}
}
impl PartialEq for Digest {
fn eq(&self, other: &Digest) -> bool {
use utils::memcmp;
if other.len != self.len {
return false;
}
memcmp(self.as_ref(), other.as_ref())
}
}
impl Eq for Digest {}
impl AsRef<[u8]> for Digest {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.data[0..self.len]
}
}
impl PartialOrd for Digest {
#[inline]
fn partial_cmp(&self, other: &Digest) -> Option<Ordering> {
PartialOrd::partial_cmp(self.as_ref(), other.as_ref())
}
#[inline]
fn lt(&self, other: &Digest) -> bool {
PartialOrd::lt(self.as_ref(), other.as_ref())
}
#[inline]
fn le(&self, other: &Digest) -> bool {
PartialOrd::le(self.as_ref(), other.as_ref())
}
#[inline]
fn ge(&self, other: &Digest) -> bool {
PartialOrd::ge(self.as_ref(), other.as_ref())
}
#[inline]
fn gt(&self, other: &Digest) -> bool {
PartialOrd::gt(self.as_ref(), other.as_ref())
}
}
impl Ord for Digest {
#[inline]
fn cmp(&self, other: &Digest) -> Ordering {
Ord::cmp(self.as_ref(), other.as_ref())
}
}
impl Hash for Digest {
fn hash<H: Hasher>(&self, state: &mut H) {
Hash::hash(self.as_ref(), state)
}
}
impl Index<Range<usize>> for Digest {
type Output = [u8];
fn index(&self, index: Range<usize>) -> &[u8] {
self.as_ref().index(index)
}
}
impl Index<RangeTo<usize>> for Digest {
type Output = [u8];
fn index(&self, index: RangeTo<usize>) -> &[u8] {
self.as_ref().index(index)
}
}
impl Index<RangeFrom<usize>> for Digest {
type Output = [u8];
fn index(&self, index: RangeFrom<usize>) -> &[u8] {
self.as_ref().index(index)
}
}
impl Index<RangeFull> for Digest {
type Output = [u8];
fn index(&self, index: RangeFull) -> &[u8] {
self.as_ref().index(index)
}
}