aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/hash.rs
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2021-03-18 09:28:50 +0100
committerNicholas Rodrigues Lordello <[email protected]>2021-03-18 09:28:50 +0100
commit789f4585504119de8e490aa12a8c02b649f24ee0 (patch)
treeef2df8f30af6f06102b49555a308d77d9eae36ef /src/cmd/hash.rs
parent1ce0c196c10dc92977114279180ea714056c587d (diff)
added hashing command
Diffstat (limited to 'src/cmd/hash.rs')
-rw-r--r--src/cmd/hash.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/cmd/hash.rs b/src/cmd/hash.rs
new file mode 100644
index 0000000..78f33cb
--- /dev/null
+++ b/src/cmd/hash.rs
@@ -0,0 +1,41 @@
+//! Module implementing the `hash` subcommand for `keccak256` hashing data.
+
+use crate::{cmd, hash};
+use anyhow::Result;
+use std::{
+ fs,
+ io::{self, Read},
+ path::PathBuf,
+ str,
+};
+use structopt::StructOpt;
+
+#[derive(Debug, StructOpt)]
+pub struct Options {
+ /// The data is hexidecimal encoded.
+ #[structopt(short, long)]
+ hex: bool,
+
+ /// Path to the data to hash. Use `-` for standard in.
+ #[structopt(name = "DATA", default_value = "-")]
+ data: PathBuf,
+}
+
+pub fn run(options: Options) -> Result<()> {
+ let data = match options.data.to_str() {
+ Some("-") => {
+ let mut buf = Vec::new();
+ io::stdin().read_to_end(&mut buf)?;
+ buf
+ }
+ _ => fs::read(&options.data)?,
+ };
+ let bytes = if options.hex {
+ cmd::permissive_hex(str::from_utf8(&data)?)?
+ } else {
+ data.into_boxed_slice()
+ };
+
+ println!("0x{}", hex::encode(hash::keccak256(bytes)));
+ Ok(())
+}