Struct git_features::hash::Sha1
source · pub struct Sha1(_);
Available on crate feature
fast-sha1
and (crate features rustsha1
or fast-sha1
) only.Expand description
A implementation of the Sha1 hash, which can be used once.
Implementations§
source§impl Sha1
impl Sha1
sourcepub fn update(&mut self, bytes: &[u8])
pub fn update(&mut self, bytes: &[u8])
Digest the given bytes
.
Examples found in repository?
src/hash.rs (line 136)
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
pub fn bytes(
mut read: impl std::io::Read,
num_bytes_from_start: usize,
kind: git_hash::Kind,
progress: &mut impl crate::progress::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> std::io::Result<git_hash::ObjectId> {
let mut hasher = hasher(kind);
let start = std::time::Instant::now();
// init progress before the possibility for failure, as convenience in case people want to recover
progress.init(Some(num_bytes_from_start), crate::progress::bytes());
const BUF_SIZE: usize = u16::MAX as usize;
let mut buf = [0u8; BUF_SIZE];
let mut bytes_left = num_bytes_from_start;
while bytes_left > 0 {
let out = &mut buf[..BUF_SIZE.min(bytes_left)];
read.read_exact(out)?;
bytes_left -= out.len();
progress.inc_by(out.len());
hasher.update(out);
if should_interrupt.load(std::sync::atomic::Ordering::SeqCst) {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Interrupted"));
}
}
let id = git_hash::ObjectId::from(hasher.digest());
progress.show_throughput(start);
Ok(id)
}
#[cfg(any(feature = "rustsha1", feature = "fast-sha1"))]
mod write {
use crate::hash::Sha1;
/// A utility to automatically generate a hash while writing into an inner writer.
pub struct Write<T> {
/// The hash implementation.
pub hash: Sha1,
/// The inner writer.
pub inner: T,
}
impl<T> std::io::Write for Write<T>
where
T: std::io::Write,
{
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
let written = self.inner.write(buf)?;
self.hash.update(&buf[..written]);
Ok(written)
}
sourcepub fn digest(self) -> Sha1Digest
pub fn digest(self) -> Sha1Digest
Finalize the hash and produce a digest.
Examples found in repository?
src/hash.rs (line 142)
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
pub fn bytes(
mut read: impl std::io::Read,
num_bytes_from_start: usize,
kind: git_hash::Kind,
progress: &mut impl crate::progress::Progress,
should_interrupt: &std::sync::atomic::AtomicBool,
) -> std::io::Result<git_hash::ObjectId> {
let mut hasher = hasher(kind);
let start = std::time::Instant::now();
// init progress before the possibility for failure, as convenience in case people want to recover
progress.init(Some(num_bytes_from_start), crate::progress::bytes());
const BUF_SIZE: usize = u16::MAX as usize;
let mut buf = [0u8; BUF_SIZE];
let mut bytes_left = num_bytes_from_start;
while bytes_left > 0 {
let out = &mut buf[..BUF_SIZE.min(bytes_left)];
read.read_exact(out)?;
bytes_left -= out.len();
progress.inc_by(out.len());
hasher.update(out);
if should_interrupt.load(std::sync::atomic::Ordering::SeqCst) {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Interrupted"));
}
}
let id = git_hash::ObjectId::from(hasher.digest());
progress.show_throughput(start);
Ok(id)
}