summaryrefslogtreecommitdiffstats
path: root/src/tools/cargo/crates/cargo-util/src/sha256.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/cargo/crates/cargo-util/src/sha256.rs')
-rw-r--r--src/tools/cargo/crates/cargo-util/src/sha256.rs53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/tools/cargo/crates/cargo-util/src/sha256.rs b/src/tools/cargo/crates/cargo-util/src/sha256.rs
new file mode 100644
index 000000000..8906fe93d
--- /dev/null
+++ b/src/tools/cargo/crates/cargo-util/src/sha256.rs
@@ -0,0 +1,53 @@
+use super::paths;
+use anyhow::{Context, Result};
+use sha2::{Digest, Sha256 as Sha2_sha256};
+use std::fs::File;
+use std::io::{self, Read};
+use std::path::Path;
+
+pub struct Sha256(Sha2_sha256);
+
+impl Sha256 {
+ pub fn new() -> Sha256 {
+ let hasher = Sha2_sha256::new();
+ Sha256(hasher)
+ }
+
+ pub fn update(&mut self, bytes: &[u8]) -> &mut Sha256 {
+ let _ = self.0.update(bytes);
+ self
+ }
+
+ pub fn update_file(&mut self, mut file: &File) -> io::Result<&mut Sha256> {
+ let mut buf = [0; 64 * 1024];
+ loop {
+ let n = file.read(&mut buf)?;
+ if n == 0 {
+ break Ok(self);
+ }
+ self.update(&buf[..n]);
+ }
+ }
+
+ pub fn update_path<P: AsRef<Path>>(&mut self, path: P) -> Result<&mut Sha256> {
+ let path = path.as_ref();
+ let file = paths::open(path)?;
+ self.update_file(&file)
+ .with_context(|| format!("failed to read `{}`", path.display()))?;
+ Ok(self)
+ }
+
+ pub fn finish(&mut self) -> [u8; 32] {
+ self.0.finalize_reset().into()
+ }
+
+ pub fn finish_hex(&mut self) -> String {
+ hex::encode(self.finish())
+ }
+}
+
+impl Default for Sha256 {
+ fn default() -> Self {
+ Self::new()
+ }
+}