Skip to content

Commit

Permalink
Eldritch file hash function. (#8)
Browse files Browse the repository at this point in the history
* Added functions, docs, and testing.

* Update cmd/implants/eldritch/src/file/hash_impl.rs

Co-authored-by: Nicholas O'Brien <[email protected]>

Co-authored-by: KCarretto <[email protected]>
Co-authored-by: Nicholas O'Brien <[email protected]>
  • Loading branch information
3 people authored Mar 6, 2022
1 parent c6518a6 commit a87a3d0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/implants/eldritch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ edition = "2021"
starlark = "0.6.0"
anyhow = "1.0.55"
derive_more = "0.99.17"
sha256 = "1.0.3"
32 changes: 30 additions & 2 deletions cmd/implants/eldritch/src/file/hash_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
use anyhow::Result;
use sha256::digest_file;

pub fn hash(_path: String) -> Result<String> {
unimplemented!("Method unimplemented")
pub fn hash(path: String) -> Result<String> {
let val = digest_file(path)?;
Ok(val)
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use std::io::prelude::*;
use std::fs::remove_file;

#[test]
fn test_hash() -> anyhow::Result<()>{
let winfile = "/tmp/win_test_exists_does";
let _ = remove_file(String::from(winfile));
// Create file
let mut file = File::create(winfile)?;
// Write to file
file.write_all(b"aoeu")?;

// Run our code
let res = hash(String::from(winfile))?;

assert_eq!(res, "bc4c24181ed3ce6666444deeb95e1f61940bffee70dd13972beb331f5d111e9b");

remove_file(String::from(winfile))?;
Ok(())
}
}
2 changes: 1 addition & 1 deletion docs/_data/eldritch_nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- name: "file.hash"
signature: "file.hash(path: str) -> str"
link: stdlib_file_hash
content: The <b>file.hash</b> method is very cool, and will be even cooler when Nick documents it.
content: The <b>file.hash</b> takes a sha256 hash of the file specified in path.
- name: "file.is_dir"
signature: "file.is_dir(path: str) -> bool"
link: stdlib_file_is_dir
Expand Down

0 comments on commit a87a3d0

Please sign in to comment.