1 use std
::borrow
::Borrow
;
3 use endian_trait
::Endian
;
6 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
12 #[derive(Endian, Clone, Debug, Eq, Hash, PartialEq)]
14 pub struct FixedChunk(pub [u8; 32]);
17 pub fn new(hash
: [u8; 32]) -> Self {
21 pub fn from_hex
<T
: AsRef
<[u8]>>(hex
: T
) -> Result
<Self, Error
> {
22 Ok(Self::new(crate::tools
::parse_hex_digest(hex
.as_ref())?
))
25 pub fn from_data(data
: &[u8]) -> Self {
26 let mut hasher
= openssl
::sha
::Sha256
::new();
28 Self::new(hasher
.finish())
31 pub fn digest_to_hex(&self) -> String
{
32 crate::tools
::digest_to_hex(&self.0)
36 #[derive(Endian, Clone, Copy, Debug, Hash)]
38 pub struct ChunkEntry
{
44 pub fn new(hash
: [u8; 32], size
: u64) -> Self {
48 pub fn from_hex
<T
: AsRef
<[u8]>>(hex
: T
, size
: u64) -> Result
<Self, Error
> {
50 crate::tools
::parse_hex_digest(hex
.as_ref())?
,
55 pub fn len(&self) -> u64 {
59 pub fn from_data(data
: &[u8]) -> Self {
60 let mut hasher
= openssl
::sha
::Sha256
::new();
62 Self::new(hasher
.finish(), data
.len() as u64)
65 pub fn digest_to_hex(&self) -> String
{
66 crate::tools
::digest_to_hex(&self.hash
)
69 pub fn to_fixed(&self) -> FixedChunk
{
74 impl PartialEq
for ChunkEntry
{
75 fn eq(&self, other
: &Self) -> bool
{
76 self.size
== other
.size
&& self.hash
== other
.hash
80 impl Eq
for ChunkEntry {}
82 impl Into
<FixedChunk
> for ChunkEntry
{
83 fn into(self) -> FixedChunk
{
88 impl Borrow
<FixedChunk
> for ChunkEntry
{
89 fn borrow(&self) -> &FixedChunk
{
90 unsafe { std::mem::transmute(&self.hash) }
94 impl Borrow
<FixedChunk
> for [u8; 32] {
95 fn borrow(&self) -> &FixedChunk
{
96 unsafe { std::mem::transmute(self) }