Skip to content

Commit

Permalink
Sha256 hashing for SliceFile added
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeceHumphreys committed May 30, 2024
1 parent 71f62d8 commit 636384a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ lalrpop-util = "0.20.2"
# derive feature allows structs to derive Serialize automatically
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.115"
sha2 = "0.10.8"

[build-dependencies]
# The default features enable a built-in lexer. We supply our own lexer so we don't need these.
Expand Down
14 changes: 14 additions & 0 deletions src/slice_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::grammar::*;
use crate::utils::ptr_util::WeakPtr;
use console::style;
use serde::Serialize;
use sha2::{Digest, Sha256};
use std::cmp::{max, min, Ordering};
use std::fmt::{Display, Write};

Expand Down Expand Up @@ -166,6 +167,19 @@ impl SliceFile {

formatted_snippet + &line_prefix
}

/// Hash the combination of the filename, relative path, and raw text using a SHA-256 hash.
///
/// # Returns
/// The SHA-256 hash as a vector of bytes.
pub fn compute_sha256_hash(&self) -> Vec<u8> {
Sha256::new()
.chain_update(self.filename.as_bytes())
.chain_update(self.relative_path.as_bytes())
.chain_update(self.raw_text.as_bytes())
.finalize()
.to_vec()
}
}

implement_Attributable_for!(SliceFile);
Expand Down

0 comments on commit 636384a

Please sign in to comment.