From 72a8969453cba70c028cc5d557e9ccf00997634f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 3 Apr 2022 22:30:16 +0800 Subject: [PATCH] feat: add hmac_sha1 --- __crypto/hmac_sha1/Cargo.lock | 117 +++++++++++++++++++++++++++++++++ __crypto/hmac_sha1/Cargo.toml | 11 ++++ __crypto/hmac_sha1/src/main.rs | 21 ++++++ 3 files changed, 149 insertions(+) create mode 100644 __crypto/hmac_sha1/Cargo.lock create mode 100644 __crypto/hmac_sha1/Cargo.toml create mode 100644 __crypto/hmac_sha1/src/main.rs diff --git a/__crypto/hmac_sha1/Cargo.lock b/__crypto/hmac_sha1/Cargo.lock new file mode 100644 index 0000000..09bdf2d --- /dev/null +++ b/__crypto/hmac_sha1/Cargo.lock @@ -0,0 +1,117 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "block-buffer" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "generic-array" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "hmac_sha1" +version = "0.1.0" +dependencies = [ + "hex", + "hmac", + "sha-1", +] + +[[package]] +name = "libc" +version = "0.2.121" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f" + +[[package]] +name = "sha-1" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] +name = "typenum" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/__crypto/hmac_sha1/Cargo.toml b/__crypto/hmac_sha1/Cargo.toml new file mode 100644 index 0000000..6a41a7e --- /dev/null +++ b/__crypto/hmac_sha1/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "hmac_sha1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +sha-1 = "*" +hmac = "*" +hex = "*" \ No newline at end of file diff --git a/__crypto/hmac_sha1/src/main.rs b/__crypto/hmac_sha1/src/main.rs new file mode 100644 index 0000000..9d721a9 --- /dev/null +++ b/__crypto/hmac_sha1/src/main.rs @@ -0,0 +1,21 @@ +use hmac::{Hmac, Mac}; +use sha1::Sha1; + +const SHA1_DIGEST_SIZE: usize = 20; + +type HmacSha1 = Hmac; + +fn main() { + let args = std::env::args().into_iter().collect::>(); + println!("Args: {:?}", args); + let key = hex::decode(&args[1]).expect("Key hex parse failed"); + let data = hex::decode(&args[2]).expect("Data hex parse failed"); + let mut hmac_sha1 = HmacSha1::new_from_slice(&key).unwrap(); + hmac_sha1.update(&data); + let result = hmac_sha1.finalize(); + + let mut code = [0; SHA1_DIGEST_SIZE]; + code.copy_from_slice(result.into_bytes().as_slice()); + + println!("{}", hex::encode(&code)); +}