Port hash, hash md5 and hash sha256 commands (#464)

`hash` by itself is only printing the help message.

The other two are simply using the same generic implementation.
This commit is contained in:
Benoît Cortier
2021-12-10 18:14:28 -05:00
committed by GitHub
parent 95841e3489
commit e77c6bb284
10 changed files with 408 additions and 49 deletions

View File

@ -57,8 +57,12 @@ lazy_static = "1.4.0"
strip-ansi-escapes = "0.1.1"
crossterm = "0.22.1"
quick-xml = "0.22"
digest = "0.10.0"
md5 = { package = "md-5", version = "0.10.0" }
sha2 = "0.10.0"
base64 = "0.13.0"
num = {version="0.4.0", optional=true}
num = { version = "0.4.0", optional = true }
[dependencies.polars]
version = "0.18.0"

View File

@ -9,10 +9,7 @@ pub fn create_default_context() -> EngineState {
let mut working_set = StateWorkingSet::new(&engine_state);
macro_rules! bind_command {
( $command:expr ) => {
working_set.add_decl(Box::new($command));
};
( $( $command:expr ),* ) => {
( $( $command:expr ),* $(,)? ) => {
$( working_set.add_decl(Box::new($command)); )*
};
}
@ -175,7 +172,11 @@ pub fn create_default_context() -> EngineState {
Where,
WithEnv,
Wrap,
Zip
Zip,
// Hash
Hash,
HashMd5::default(),
HashSha256::default(),
);
#[cfg(feature = "plugin")]

View File

@ -0,0 +1,35 @@
use nu_engine::get_full_help;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, IntoPipelineData, PipelineData, ShellError, Signature, Value};
#[derive(Clone)]
pub struct Hash;
impl Command for Hash {
fn name(&self) -> &str {
"hash"
}
fn signature(&self) -> Signature {
Signature::build("hash").category(Category::Hash)
}
fn usage(&self) -> &str {
"Apply hash function."
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
Ok(Value::String {
val: get_full_help(&Self.signature(), &Self.examples(), engine_state),
span: call.head,
}
.into_pipeline_data())
}
}

View File

@ -0,0 +1,108 @@
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value};
use std::marker::PhantomData;
pub trait HashDigest: digest::Digest + Clone {
fn name() -> &'static str;
fn examples() -> Vec<Example>;
}
#[derive(Clone)]
pub struct GenericDigest<D: HashDigest> {
name: String,
usage: String,
phantom: PhantomData<D>,
}
impl<D: HashDigest> Default for GenericDigest<D> {
fn default() -> Self {
Self {
name: format!("hash {}", D::name()),
usage: format!("hash a value using the {} hash algorithm", D::name()),
phantom: PhantomData,
}
}
}
impl<D> Command for GenericDigest<D>
where
D: HashDigest + Send + Sync + 'static,
digest::Output<D>: core::fmt::LowerHex,
{
fn name(&self) -> &str {
&self.name
}
fn signature(&self) -> Signature {
Signature::build(self.name()).rest(
"rest",
SyntaxShape::CellPath,
format!("optionally {} hash data by cell path", D::name()),
)
}
fn usage(&self) -> &str {
&self.usage
}
fn examples(&self) -> Vec<Example> {
D::examples()
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
input.map(
move |v| {
if cell_paths.is_empty() {
action::<D>(&v)
} else {
let mut v = v;
for path in &cell_paths {
let ret = v
.update_cell_path(&path.members, Box::new(move |old| action::<D>(old)));
if let Err(error) = ret {
return Value::Error { error };
}
}
v
}
},
engine_state.ctrlc.clone(),
)
}
}
pub fn action<D>(input: &Value) -> Value
where
D: HashDigest,
digest::Output<D>: core::fmt::LowerHex,
{
let (bytes, span) = match input {
Value::String { val, span } => (val.as_bytes(), *span),
Value::Binary { val, span } => (val.as_slice(), *span),
other => {
return Value::Error {
error: ShellError::UnsupportedInput(
format!(
"Type `{}` is not supported for {} hashing input",
other.get_type(),
D::name()
),
input.span().unwrap_or_else(|_| Span::unknown()),
),
};
}
};
let val = format!("{:x}", D::digest(bytes));
Value::String { val, span }
}

View File

@ -0,0 +1,68 @@
use super::generic_digest::{GenericDigest, HashDigest};
use ::md5::Md5;
use nu_protocol::{Example, Span, Value};
pub type HashMd5 = GenericDigest<Md5>;
impl HashDigest for Md5 {
fn name() -> &'static str {
"md5"
}
fn examples() -> Vec<Example> {
vec![
Example {
description: "md5 encode a string",
example: "echo 'abcdefghijklmnopqrstuvwxyz' | hash md5",
result: Some(Value::String {
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
span: Span::unknown(),
}),
},
Example {
description: "md5 encode a file",
example: "open ./nu_0_24_1_windows.zip | hash md5",
result: None,
},
]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hash::generic_digest;
#[test]
fn test_examples() {
crate::test_examples(HashMd5::default())
}
#[test]
fn hash_string() {
let binary = Value::String {
val: "abcdefghijklmnopqrstuvwxyz".to_owned(),
span: Span::unknown(),
};
let expected = Value::String {
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
span: Span::unknown(),
};
let actual = generic_digest::action::<Md5>(&binary);
assert_eq!(actual, expected);
}
#[test]
fn hash_bytes() {
let binary = Value::Binary {
val: vec![0xC0, 0xFF, 0xEE],
span: Span::unknown(),
};
let expected = Value::String {
val: "5f80e231382769b0102b1164cf722d83".to_owned(),
span: Span::unknown(),
};
let actual = generic_digest::action::<Md5>(&binary);
assert_eq!(actual, expected);
}
}

View File

@ -0,0 +1,8 @@
mod command;
mod generic_digest;
mod md5;
mod sha256;
pub use self::command::Hash;
pub use self::md5::HashMd5;
pub use self::sha256::HashSha256;

View File

@ -0,0 +1,69 @@
use super::generic_digest::{GenericDigest, HashDigest};
use ::sha2::Sha256;
use nu_protocol::{Example, Span, Value};
pub type HashSha256 = GenericDigest<Sha256>;
impl HashDigest for Sha256 {
fn name() -> &'static str {
"sha256"
}
fn examples() -> Vec<Example> {
vec![
Example {
description: "sha256 encode a string",
example: "echo 'abcdefghijklmnopqrstuvwxyz' | hash sha256",
result: Some(Value::String {
val: "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73"
.to_owned(),
span: Span::unknown(),
}),
},
Example {
description: "sha256 encode a file",
example: "open ./nu_0_24_1_windows.zip | hash sha256",
result: None,
},
]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hash::generic_digest;
#[test]
fn test_examples() {
crate::test_examples(HashSha256::default())
}
#[test]
fn hash_string() {
let binary = Value::String {
val: "abcdefghijklmnopqrstuvwxyz".to_owned(),
span: Span::unknown(),
};
let expected = Value::String {
val: "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
span: Span::unknown(),
};
let actual = generic_digest::action::<Sha256>(&binary);
assert_eq!(actual, expected);
}
#[test]
fn hash_bytes() {
let binary = Value::Binary {
val: vec![0xC0, 0xFF, 0xEE],
span: Span::unknown(),
};
let expected = Value::String {
val: "c47a10dc272b1221f0380a2ae0f7d7fa830b3e378f2f5309bbf13f61ad211913".to_owned(),
span: Span::unknown(),
};
let actual = generic_digest::action::<Sha256>(&binary);
assert_eq!(actual, expected);
}
}

View File

@ -9,6 +9,7 @@ mod experimental;
mod filesystem;
mod filters;
mod formats;
mod hash;
mod math;
mod network;
mod platform;
@ -29,6 +30,7 @@ pub use experimental::*;
pub use filesystem::*;
pub use filters::*;
pub use formats::*;
pub use hash::*;
pub use math::*;
pub use network::*;
pub use platform::*;

View File

@ -49,6 +49,7 @@ pub enum Category {
Strings,
System,
Viewers,
Hash,
Custom(String),
}
@ -72,6 +73,7 @@ impl std::fmt::Display for Category {
Category::Strings => "strings",
Category::System => "system",
Category::Viewers => "viewers",
Category::Hash => "hash",
Category::Custom(name) => name,
};