|
| 1 | +use md5::{Digest}; |
| 2 | +use serde::{Serialize}; |
| 3 | +use argon2::{PasswordHasher}; |
| 4 | + |
| 5 | +// --- tests --- |
| 6 | + |
| 7 | +fn test_hash_algorithms( |
| 8 | + harmless: &str, credit_card_no: &str, password: &str, encrypted_password: &str, salt: &str |
| 9 | +) { |
| 10 | + // test hashing with different algorithms and data |
| 11 | + |
| 12 | + // MD5 |
| 13 | + _ = md5::Md5::digest(harmless); |
| 14 | + _ = md5::Md5::digest(credit_card_no); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 15 | + _ = md5::Md5::digest(password); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 16 | + _ = md5::Md5::digest(encrypted_password); |
| 17 | + |
| 18 | + // MD5 (alternative / older library) |
| 19 | + _ = md5_alt::compute(harmless); |
| 20 | + _ = md5_alt::compute(credit_card_no); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 21 | + _ = md5_alt::compute(password); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 22 | + _ = md5_alt::compute(encrypted_password); |
| 23 | + |
| 24 | + // SHA-1 |
| 25 | + _ = sha1::Sha1::digest(harmless); |
| 26 | + _ = sha1::Sha1::digest(credit_card_no); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 27 | + _ = sha1::Sha1::digest(password); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 28 | + _ = sha1::Sha1::digest(encrypted_password); |
| 29 | + |
| 30 | + // SHA-1 checked |
| 31 | + _ = sha1_checked::Sha1::digest(harmless); |
| 32 | + _ = sha1_checked::Sha1::digest(credit_card_no); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 33 | + _ = sha1_checked::Sha1::digest(password); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 34 | + _ = sha1_checked::Sha1::digest(encrypted_password); |
| 35 | + |
| 36 | + // SHA-256 (appropriate for sensitive data hashing) |
| 37 | + _ = sha3::Sha3_256::digest(harmless); |
| 38 | + _ = sha3::Sha3_256::digest(credit_card_no); |
| 39 | + _ = sha3::Sha3_256::digest(password); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 40 | + _ = sha3::Sha3_256::digest(encrypted_password); |
| 41 | + |
| 42 | + // Argon2 (appropriate for password hashing) |
| 43 | + let argon2_salt = argon2::password_hash::Salt::from_b64(salt).unwrap(); |
| 44 | + _ = argon2::Argon2::default().hash_password(harmless.as_bytes(), argon2_salt).unwrap().to_string(); |
| 45 | + _ = argon2::Argon2::default().hash_password(credit_card_no.as_bytes(), argon2_salt).unwrap().to_string(); |
| 46 | + _ = argon2::Argon2::default().hash_password(password.as_bytes(), argon2_salt).unwrap().to_string(); |
| 47 | + _ = argon2::Argon2::default().hash_password(encrypted_password.as_bytes(), argon2_salt).unwrap().to_string(); |
| 48 | +} |
| 49 | + |
| 50 | +fn test_hash_code_patterns( |
| 51 | + harmless: &str, password: &str, |
| 52 | + harmless_str: String, password_str: String, |
| 53 | + harmless_arr: &[u8], password_arr: &[u8], |
| 54 | + harmless_vec: Vec<u8>, password_vec: Vec<u8> |
| 55 | +) { |
| 56 | + // test hashing with different code patterns |
| 57 | + |
| 58 | + // hash different types of data |
| 59 | + _ = md5::Md5::digest(harmless_str); |
| 60 | + _ = md5::Md5::digest(password_str); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 61 | + _ = md5::Md5::digest(harmless_arr); |
| 62 | + _ = md5::Md5::digest(password_arr); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 63 | + _ = md5::Md5::digest(harmless_vec); |
| 64 | + _ = md5::Md5::digest(password_vec); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 65 | + |
| 66 | + // hash through a hasher object |
| 67 | + let mut md5_hasher = md5::Md5::new(); |
| 68 | + md5_hasher.update(b"abc"); |
| 69 | + md5_hasher.update(harmless); |
| 70 | + md5_hasher.update(password); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 71 | + _ = md5_hasher.finalize(); |
| 72 | + |
| 73 | + _ = md5::Md5::new().chain_update(harmless).chain_update(harmless).chain_update(harmless).finalize(); |
| 74 | + _ = md5::Md5::new().chain_update(harmless).chain_update(password).chain_update(harmless).finalize(); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 75 | + |
| 76 | + _ = md5::Md5::new_with_prefix(harmless).finalize(); |
| 77 | + _ = md5::Md5::new_with_prefix(password).finalize(); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 78 | + |
| 79 | + // hash transformed data |
| 80 | + _ = md5::Md5::digest(harmless.trim()); |
| 81 | + _ = md5::Md5::digest(password.trim()); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 82 | + _ = md5::Md5::digest(harmless.as_bytes()); |
| 83 | + _ = md5::Md5::digest(password.as_bytes()); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 84 | + _ = md5::Md5::digest(std::str::from_utf8(harmless_arr).unwrap()); |
| 85 | + _ = md5::Md5::digest(std::str::from_utf8(password_arr).unwrap()); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 86 | +} |
| 87 | + |
| 88 | +#[derive(Serialize)] |
| 89 | +struct MyStruct1 { |
| 90 | + id: u64, |
| 91 | + data: String, |
| 92 | +} |
| 93 | + |
| 94 | +#[derive(Serialize)] |
| 95 | +struct MyStruct2 { |
| 96 | + id: u64, |
| 97 | + credit_card_no: String, |
| 98 | +} |
| 99 | + |
| 100 | +#[derive(Serialize)] |
| 101 | +struct MyStruct3 { |
| 102 | + id: u64, |
| 103 | + password: String, |
| 104 | +} |
| 105 | + |
| 106 | +fn test_hash_structs() { |
| 107 | + // test hashing with data in a struct |
| 108 | + let s1 = MyStruct1 { |
| 109 | + id: 1, |
| 110 | + data: "0123456789".to_string(), |
| 111 | + }; |
| 112 | + let s2 = MyStruct2 { |
| 113 | + id: 2, |
| 114 | + credit_card_no: "0123456789".to_string(), |
| 115 | + }; |
| 116 | + let s3 = MyStruct3 { |
| 117 | + id: 3, |
| 118 | + password: "0123456789".to_string(), |
| 119 | + }; |
| 120 | + |
| 121 | + // serialize with serde |
| 122 | + let str1a = serde_json::to_string(&s1).unwrap(); |
| 123 | + let str2a = serde_json::to_string(&s2).unwrap(); |
| 124 | + let str3a = serde_json::to_string(&s3).unwrap(); |
| 125 | + let str1b = serde_json::to_vec(&s1).unwrap(); |
| 126 | + let str2b = serde_json::to_vec(&s2).unwrap(); |
| 127 | + let str3b = serde_json::to_vec(&s3).unwrap(); |
| 128 | + let str1c = serde_urlencoded::to_string(&s1).unwrap(); |
| 129 | + let str2c = serde_urlencoded::to_string(&s2).unwrap(); |
| 130 | + let str3c = serde_urlencoded::to_string(&s3).unwrap(); |
| 131 | + |
| 132 | + // hash with MD5 |
| 133 | + let mut md5_hasher = md5::Md5::new(); |
| 134 | + md5_hasher.update(s1.data); |
| 135 | + md5_hasher.update(s2.credit_card_no); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 136 | + md5_hasher.update(s3.password); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 137 | + md5_hasher.update(str1a); |
| 138 | + md5_hasher.update(str2a); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 139 | + md5_hasher.update(str3a); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 140 | + md5_hasher.update(str1b); |
| 141 | + md5_hasher.update(str2b); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 142 | + md5_hasher.update(str3b); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 143 | + md5_hasher.update(str1c); |
| 144 | + md5_hasher.update(str2c); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 145 | + md5_hasher.update(str3c); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 146 | + _ = md5_hasher.finalize(); |
| 147 | +} |
| 148 | + |
| 149 | +fn test_hash_file( |
| 150 | + harmless_filename: &str, password_filename: &str |
| 151 | +) { |
| 152 | + // test hashing files |
| 153 | + let mut harmless_file = std::fs::File::open(harmless_filename).unwrap(); |
| 154 | + let mut password_file = std::fs::File::open(password_filename).unwrap(); |
| 155 | + |
| 156 | + let mut md5_hasher = md5::Md5::new(); |
| 157 | + _ = std::io::copy(&mut harmless_file, &mut md5_hasher); |
| 158 | + _ = std::io::copy(&mut password_file, &mut md5_hasher); // $ MISSING: Alert[rust/weak-sensitive-data-hashing] |
| 159 | + _ = md5_hasher.finalize(); |
| 160 | +} |
0 commit comments