From cdac8b5f5b21b2c66dae1f943353d5cb9cd45e2f Mon Sep 17 00:00:00 2001 From: Greg Leonard Date: Fri, 22 Oct 2021 10:52:20 +0100 Subject: [PATCH] Replace 'crypto' crate with 'sha2' (close #131) --- Cargo.toml | 2 +- src/factotum/webhook/jobcontext/mod.rs | 19 ++++++++-------- src/factotum/webhook/jobcontext/tests.rs | 28 +++++++++++------------- src/factotum/webhook/jobupdate/mod.rs | 2 +- src/main.rs | 2 +- 5 files changed, 25 insertions(+), 28 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d3c5505..3a6b1ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,10 +14,10 @@ chrono = "0.2" colored = "1.2" mustache = "0.7.0" rand = "0.3" -rust-crypto = "^0.2" uuid = { version = "0.2", features = ["v4"] } hyper = "^0.10" hyper-native-tls = "0.3.0" libc = "0.2.17" ifaces = "0.0.3" dns-lookup = "0.2.1" +sha2 = "0.9.8" diff --git a/src/factotum/webhook/jobcontext/mod.rs b/src/factotum/webhook/jobcontext/mod.rs index 6d18a0d..68f6e55 100644 --- a/src/factotum/webhook/jobcontext/mod.rs +++ b/src/factotum/webhook/jobcontext/mod.rs @@ -17,9 +17,8 @@ mod tests; use chrono::DateTime; use chrono::UTC; -use crypto::digest::Digest; -use crypto::sha2::Sha256; use rustc_serialize::base64::{ToBase64, MIME}; +use sha2::{Digest, Sha256}; use std::collections::HashMap; use uuid::Uuid; @@ -43,22 +42,22 @@ impl JobContext { let ff = factfile; let mut job_digest = Sha256::new(); - job_digest.input_str(&ff); + job_digest.update(&ff); if let Some(ref tags_map) = tags { let mut sorted_keys: Vec<_> = tags_map.keys().collect(); sorted_keys.sort(); for key in sorted_keys { - job_digest.input_str(key); - job_digest.input_str(&tags_map[key]); + job_digest.update(key); + job_digest.update(&tags_map[key]); } } - let job_ref = job_digest.result_str(); + let job_ref = job_digest.finalize(); let mut run_digest = Sha256::new(); - run_digest.input_str(&format!("{}", Uuid::new_v4())); - let run_ref = run_digest.result_str(); + run_digest.update(&format!("{}", Uuid::new_v4())); + let run_ref = run_digest.finalize(); let mut config = MIME; config.line_length = None; @@ -72,8 +71,8 @@ impl JobContext { JobContext { job_name: job_name.into(), - job_reference: job_ref, - run_reference: run_ref, + job_reference: format!("{:x}", job_ref), + run_reference: format!("{:x}", run_ref), factfile: b64_ff, factotum_version: env!("CARGO_PKG_VERSION").to_string(), start_time: UTC::now(), diff --git a/src/factotum/webhook/jobcontext/tests.rs b/src/factotum/webhook/jobcontext/tests.rs index 6428a1a..05ab852 100644 --- a/src/factotum/webhook/jobcontext/tests.rs +++ b/src/factotum/webhook/jobcontext/tests.rs @@ -15,9 +15,8 @@ use super::*; use chrono::UTC; -use crypto::digest::Digest; -use crypto::sha2::Sha256; use rustc_serialize::base64::{ToBase64, MIME}; +use sha2::{Digest, Sha256}; use std::collections::HashMap; #[test] @@ -41,12 +40,12 @@ fn new_sets_job_ref_to_hash_of_factfile() { let job_name = "hello"; let factfile_sim = "{Blabla}"; let mut digest = Sha256::new(); - digest.input_str(factfile_sim); - let expected = digest.result_str(); + digest.update(factfile_sim); + let expected = digest.finalize(); let context = JobContext::new(job_name, factfile_sim, None); - assert_eq!(context.job_reference, expected); + assert_eq!(context.job_reference, format!("{:x}", expected)); } #[test] @@ -97,18 +96,17 @@ fn tags_are_used_in_job_hash() { tags.insert("a".into(), "b".into()); let mut digest = Sha256::new(); - digest.input_str(factfile_sim); + digest.update(factfile_sim); - digest.input_str("a"); // NB the keys are sorted so the output is reproducible! - digest.input_str("b"); - digest.input_str("hello"); - digest.input_str("world"); - digest.input_str("z"); - digest.input_str("sdfsdfs"); + digest.update("a"); // NB the keys are sorted so the output is reproducible! + digest.update("b"); + digest.update("hello"); + digest.update("world"); + digest.update("z"); + digest.update("sdfsdfs"); - let expected = digest.result_str(); + let expected = digest.finalize(); let context = JobContext::new(job_name, factfile_sim, Some(tags.clone())); - - assert_eq!(context.job_reference, expected); + assert_eq!(context.job_reference, format!("{:x}", expected)); } diff --git a/src/factotum/webhook/jobupdate/mod.rs b/src/factotum/webhook/jobupdate/mod.rs index b6be413..b4b2d73 100644 --- a/src/factotum/webhook/jobupdate/mod.rs +++ b/src/factotum/webhook/jobupdate/mod.rs @@ -28,7 +28,7 @@ use factotum::executor::{ }; use rustc_serialize; use rustc_serialize::json::{self, Json, ToJson}; -use rustc_serialize::Encodable; +use rustc_serialize::{Decodable, Encodable}; use std::collections::BTreeMap; use std::collections::HashMap; diff --git a/src/main.rs b/src/main.rs index e2b7519..b6d19db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ extern crate log; extern crate chrono; extern crate colored; -extern crate crypto; extern crate daggy; extern crate dns_lookup; extern crate docopt; @@ -27,6 +26,7 @@ extern crate libc; extern crate log4rs; extern crate rand; extern crate rustc_serialize; +extern crate sha2; extern crate uuid; extern crate valico;