1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use crate::*;
use std::{
io::{
Read,
},
};
#[derive(Clone,Debug,PartialEq,Eq,Hash)]
pub enum Algorithm
{
#[cfg(feature="crc")]
Crc32,
#[cfg(feature="crc")]
Crc64,
Sha256,
Sha256Truncated,
}
impl Default for Algorithm
{
#[inline] fn default() -> Self
{
Self::Sha256Truncated
}
}
#[derive(Clone,Debug,PartialEq,Eq,Hash)]
pub struct Context
{
algo: Algorithm,
salt: salt::Salt,
}
impl Context
{
pub fn new(algo: Algorithm, salt: impl Into<salt::Salt>) -> Self
{
Self {
algo,
salt: salt.into(),
}
}
pub fn get_algorithm(&self) -> &Algorithm
{
&self.algo
}
pub fn get_salt(&self) -> &salt::Salt
{
&self.salt
}
pub(crate) fn compute<R: Read>(&self, mut from: R) -> Result<(usize, Box<[u8]>), error::Error>
{
fn provide<P,R>(input: &mut R, salt: &salt::Salt, output: &mut usize) -> Result<Vec<u8>, error::Error>
where P: provider::ByteProvider,
R: Read + ?Sized
{
let this = P::compute(input, &salt, output)?;
Ok(Vec::from(this.bytes()))
}
let mut output = 0usize;
let bytes = match self.algo
{
#[cfg(feature="crc")] Algorithm::Crc32 => provide::<hash::Crc32Checksum, _>(&mut from, &self.salt, &mut output)?,
#[cfg(feature="crc")] Algorithm::Crc64 => provide::<hash::Crc64Checksum, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Sha256 => provide::<hash::Sha256Hash, _>(&mut from, &self.salt, &mut output)?,
Algorithm::Sha256Truncated => provide::<hash::Sha256Truncated, _>(&mut from, &self.salt, &mut output)?,
}.into_boxed_slice();
Ok((output, bytes))
}
#[cfg(feature="ffi")]
pub(crate) unsafe fn into_raw(self) -> CContext
{
CContext{
algo: u8::from(self.algo),
salt: salt::into_raw(self.salt),
flags: Default::default(),
}
}
#[cfg(feature="ffi")]
pub(crate) unsafe fn clone_from_raw(from: *const CContext) -> Self
{
let from = &*from;
Self {
algo: from.algo.into(),
salt: salt::clone_from_raw(&from.salt as *const salt::FFI),
}
}
#[cfg(feature="ffi")]
pub(crate) unsafe fn from_raw(from: *mut CContext) -> Self
{
let from = &mut *from;
let output = Self{
algo: from.algo.into(),
salt: salt::from_raw(&mut from.salt as *mut salt::FFI),
};
from.algo = 0;
output
}
}
impl Default for Context
{
fn default() -> Self
{
Self {
algo: Default::default(),
salt: Default::default(),
}
}
}
pub(crate) const ALGO_DEFAULT: u8 = 0;
pub(crate) const ALGO_CRC32: u8 = 1;
pub(crate) const ALGO_CRC64: u8 = 2;
pub(crate) const ALGO_SHA256: u8 = 3;
pub(crate) const ALGO_SHA256_TRUNCATED: u8 = 4;
#[derive(Debug)]
#[repr(C)]
pub(crate) struct CContext
{
algo: u8,
flags: u64,
salt: salt::FFI,
}
impl From<Algorithm> for u8
{
fn from(al: Algorithm) -> Self
{
match al {
#[cfg(feature="crc")] Algorithm::Crc32 => ALGO_CRC32,
#[cfg(feature="crc")] Algorithm::Crc64 => ALGO_CRC64,
Algorithm::Sha256 => ALGO_SHA256,
Algorithm::Sha256Truncated => ALGO_SHA256_TRUNCATED,
}
}
}
impl From<u8> for Algorithm
{
fn from(al: u8) -> Self
{
match al {
#[cfg(feature="crc")] ALGO_CRC32 => Algorithm::Crc32,
#[cfg(feature="crc")] ALGO_CRC64 => Algorithm::Crc64,
ALGO_SHA256 => Algorithm::Sha256,
ALGO_SHA256_TRUNCATED => Algorithm::Sha256Truncated,
_ => Self::default(),
}
}
}