-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
178 additions
and
151 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
mod main; | ||
pub use main::Main; | ||
|
||
mod sha256; | ||
mod sha512; | ||
|
||
pub use sha256::Sha256; | ||
pub use sha512::Sha512; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; | ||
use nu_protocol::{Category, LabeledError, Signature, Value}; | ||
|
||
use crate::HmacPlugin; | ||
|
||
pub struct Main; | ||
|
||
impl SimplePluginCommand for Main { | ||
type Plugin = HmacPlugin; | ||
|
||
fn name(&self) -> &str { | ||
"hmac" | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"HMAC commands implementing various hash functions" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
// TODO: choose better category | ||
Signature::build(self.name()).category(Category::Experimental) | ||
} | ||
|
||
fn run( | ||
&self, | ||
_plugin: &Self::Plugin, | ||
engine: &EngineInterface, | ||
call: &EvaluatedCall, | ||
_input: &Value, | ||
) -> Result<Value, LabeledError> { | ||
Ok(Value::string(engine.get_help()?, call.head)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use hmac::{Hmac, Mac}; | ||
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; | ||
use nu_protocol::{Category, Example, LabeledError, Signature, SyntaxShape, Type, Value}; | ||
|
||
use crate::HmacPlugin; | ||
|
||
pub struct Sha256; | ||
|
||
impl SimplePluginCommand for Sha256 { | ||
type Plugin = HmacPlugin; | ||
|
||
fn name(&self) -> &str { | ||
"hmac sha256" | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"HMAC-SHA256 sealing" | ||
} | ||
|
||
fn signature(&self) -> nu_protocol::Signature { | ||
Signature::build(self.name()) | ||
// TODO: choose better category | ||
.category(Category::Experimental) | ||
.input_output_type(Type::String, Type::String) | ||
.required("secret", SyntaxShape::String, "Secret key to use") | ||
} | ||
|
||
fn search_terms(&self) -> Vec<&str> { | ||
vec!["hmac", "sha", "sha-2", "sha256"] | ||
} | ||
|
||
fn examples(&self) -> Vec<nu_protocol::Example> { | ||
vec![Example { | ||
example: "\"foobar\" | hmac sha256 \"my_secret\"", | ||
description: "seal “foobar” message using “my_secret” key", | ||
result: Some(Value::test_string( | ||
"c95f4062da9dd9474896abd3a0577f7e4493a09fe033a7393e539481062dca07", | ||
)), | ||
}] | ||
} | ||
|
||
fn run( | ||
&self, | ||
_plugin: &Self::Plugin, | ||
_engine: &EngineInterface, | ||
call: &EvaluatedCall, | ||
input: &Value, | ||
) -> Result<Value, LabeledError> { | ||
let message = input.as_str()?; | ||
let secret = call.req::<Vec<u8>>(0)?; | ||
|
||
let mut mac = Hmac::<sha2::Sha256>::new_from_slice(&secret) | ||
.map_err(|_| LabeledError::new("Invalid key length"))?; | ||
mac.update(message.as_bytes()); | ||
|
||
let result = mac.finalize().into_bytes(); | ||
Ok(Value::string(hex::encode(result), call.head)) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_examples() -> Result<(), nu_protocol::ShellError> { | ||
use nu_plugin_test_support::PluginTest; | ||
PluginTest::new("sha256", HmacPlugin.into())?.test_command_examples(&Sha256) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use hmac::{Hmac, Mac}; | ||
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; | ||
use nu_protocol::{Category, Example, LabeledError, Signature, SyntaxShape, Type, Value}; | ||
|
||
use crate::HmacPlugin; | ||
|
||
pub struct Sha512; | ||
|
||
impl SimplePluginCommand for Sha512 { | ||
type Plugin = HmacPlugin; | ||
|
||
fn name(&self) -> &str { | ||
"hmac sha512" | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"HMAC-SHA512 sealing" | ||
} | ||
|
||
fn signature(&self) -> nu_protocol::Signature { | ||
Signature::build(self.name()) | ||
// TODO: choose better category | ||
.category(Category::Experimental) | ||
.input_output_type(Type::String, Type::String) | ||
.required("secret", SyntaxShape::String, "Secret key to use") | ||
} | ||
|
||
fn search_terms(&self) -> Vec<&str> { | ||
vec!["hmac", "sha", "sha-2", "sha512"] | ||
} | ||
|
||
fn examples(&self) -> Vec<nu_protocol::Example> { | ||
vec![Example { | ||
example: "\"foobar\" | hmac sha512 \"my_secret\"", | ||
description: "seal “foobar” message using “my_secret” key", | ||
result: Some(Value::test_string( | ||
"6b46bb83bd0f2f721c7b7b8c9ea4904ca43bc792ea2991ac11c3d33d1e44381c2a60df3776e965d9fdc9761b901d2ea7cb3d407a0e3ecb650088127743314ee5", | ||
)), | ||
}] | ||
} | ||
|
||
fn run( | ||
&self, | ||
_plugin: &Self::Plugin, | ||
_engine: &EngineInterface, | ||
call: &EvaluatedCall, | ||
input: &Value, | ||
) -> Result<Value, LabeledError> { | ||
let message = input.as_str()?; | ||
let secret = call.req::<Vec<u8>>(0)?; | ||
|
||
let mut mac = Hmac::<sha2::Sha512>::new_from_slice(&secret) | ||
.map_err(|_| LabeledError::new("Invalid key length"))?; | ||
mac.update(message.as_bytes()); | ||
|
||
let result = mac.finalize().into_bytes(); | ||
Ok(Value::string(hex::encode(result), call.head)) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_examples() -> Result<(), nu_protocol::ShellError> { | ||
use nu_plugin_test_support::PluginTest; | ||
PluginTest::new("sha512", HmacPlugin.into())?.test_command_examples(&Sha512) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters