1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
use crate::sha01; /// Compute the SHA-1 padding for the given input length. /// /// # Arguments /// /// * `input_length` - The length of the input length. This value is /// needed to determine the padding length, and to embed the /// length in the last 8 bytes of padding. /// /// # Returns /// /// This function returns SHA-1 padding for the given input size. /// This padding has a length you can determine by calling /// `sha1::padding_length_for_input_length`. /// /// # Example /// /// ``` /// # use extendhash::sha1; /// let data = "This string will be hashed."; /// let padding = sha1::padding_for_length(data.len()); /// assert_eq!(data.len() + padding.len(), 64); /// for (i, p) in padding.iter().enumerate() { /// match i { /// 0 => 0b1000_0000, /// 1..=28 => 0b0000_0000, /// 29 => data.len() as u8 * 8, /// 30..=36 => 0b0000_0000, /// _ => unreachable!("Invalid padding length") /// }; /// } /// ``` pub fn padding_for_length(input_length: usize) -> Vec<u8> { sha01::padding_for_length(input_length) } /// Compute the SHA-1 padding length (in bytes) for the given /// input length. /// /// The result is always between 9 and 72 (inclusive). /// /// # Arguments /// /// * `input_length` - The length of the input length. This value /// is used because the amount of padding is always such that /// the total padded string is a multiple of 64 bytes. /// /// # Returns /// /// This function returns the amount of padding required for the /// given input length. /// /// # Example /// /// ``` /// # use extendhash::sha1; /// let data = "This string will be hashed."; /// let padding_length = /// sha1::padding_length_for_input_length(data.len()); /// assert_eq!(data.len() + padding_length, 64); /// ``` pub fn padding_length_for_input_length(input_length: usize) -> usize { sha01::padding_length_for_input_length(input_length) } /// Compute the SHA-1 hash of the input data /// /// # Arguments /// /// * `input` - The input data to be hashed - this could be a /// UTF-8 string or any other binary data. /// /// # Returns /// /// This function returns the computed SHA-1 hash. /// /// # Example /// /// ``` /// # use extendhash::sha1; /// let secret_data = "input string".as_bytes(); /// let hash = sha1::compute_hash(secret_data); /// assert_eq!(hash, [ /// 0xb1, 0xa3, 0x9a, 0x26, 0xea, 0x62, 0xa5, 0xc0, 0x75, 0xcd, /// 0x3c, 0xb5, 0xaa, 0x46, 0x49, 0x2c, 0x8e, 0x11, 0x34, 0xb7]); /// ``` pub fn compute_hash(input: &[u8]) -> [u8; 20] { sha01::compute_hash(input, sha01::HashType::SHA1) } /// Calculate a SHA-1 hash extension. /// /// # Arguments /// /// * `hash` - The SHA-1 hash of some previous (unknown) data /// * `length` - The length of the unknown data (without /// any added padding) /// * `additional_input` - Additional input to be /// included in the new hash. /// /// # Returns /// /// This function returns the SHA-1 hash of the /// concatenation of the original /// unknown data, its padding, and the `additional_input`. /// You can see the included (intermediate) padding by /// calling `sha1::padding_for_length`. /// /// # Example /// /// ``` /// # use extendhash::sha1; /// let secret_data = "This is a secret!".as_bytes(); /// let hash = sha1::compute_hash(secret_data); /// let secret_data_length = secret_data.len(); /// /// // Now we try computing a hash extension, assuming that /// // `secret_data` is not available. We only need `hash` /// // and `secret_data_length`. /// let appended_message = "Appended message.".as_bytes(); /// let combined_hash = sha1::extend_hash( /// hash, secret_data_length, appended_message); /// /// // Now we verify that `combined_hash` matches the /// // concatenation (note the intermediate padding): /// let mut combined_data = Vec::<u8>::new(); /// combined_data.extend_from_slice(secret_data); /// let padding = sha1::padding_for_length(secret_data_length); /// combined_data.extend_from_slice(padding.as_slice()); /// combined_data.extend_from_slice(appended_message); /// assert_eq!( /// combined_hash, /// sha1::compute_hash(combined_data.as_slice())); /// ``` pub fn extend_hash( hash: [u8; 20], length: usize, additional_input: &[u8], ) -> [u8; 20] { sha01::extend_hash( hash, length, additional_input, sha01::HashType::SHA1, ) } #[cfg(test)] mod tests { use crate::sha1; #[test] fn empty_hash() { assert_eq!( sha1::compute_hash(&[]), [ 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 ] ); } #[test] fn a_test() { assert_eq!( sha1::compute_hash("a".as_bytes()), [ 0x86, 0xf7, 0xe4, 0x37, 0xfa, 0xa5, 0xa7, 0xfc, 0xe1, 0x5d, 0x1d, 0xdc, 0xb9, 0xea, 0xea, 0xea, 0x37, 0x76, 0x67, 0xb8 ] ); } #[test] fn quick_brown_fox_test() { let s = "The quick brown fox jumps over the lazy dog"; assert_eq!( sha1::compute_hash(s.as_bytes()), [ 0x2f, 0xd4, 0xe1, 0xc6, 0x7a, 0x2d, 0x28, 0xfc, 0xed, 0x84, 0x9e, 0xe1, 0xbb, 0x76, 0xe7, 0x39, 0x1b, 0x93, 0xeb, 0x12 ] ); } #[test] fn quick_brown_fox_test_2() { let s = "The quick brown fox jumps over the lazy cog"; assert_eq!( sha1::compute_hash(s.as_bytes()), [ 0xde, 0x9f, 0x2c, 0x7f, 0xd2, 0x5e, 0x1b, 0x3a, 0xfa, 0xd3, 0xe8, 0x5a, 0x0b, 0xd1, 0x7d, 0x9b, 0x10, 0x0d, 0xb4, 0xb3 ] ); } #[test] fn abc_test() { let s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\ abcdefghijklmnopqrstuvwxyz0123456789"; assert_eq!( sha1::compute_hash(s.as_bytes()), [ 0x76, 0x1c, 0x45, 0x7b, 0xf7, 0x3b, 0x14, 0xd2, 0x7e, 0x9e, 0x92, 0x65, 0xc4, 0x6f, 0x4b, 0x4d, 0xda, 0x11, 0xf9, 0x40 ] ); } #[test] fn long_test() { let mut input = String::new(); for _ in 0..40000 { input.push_str("aaaaaaaaaaaaaaaaaaaaaaaaa"); } assert_eq!(input.len(), 1_000_000); assert_eq!( sha1::compute_hash(input.as_bytes()), [ 0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4, 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6f ] ); } #[test] fn padding_length_tests() { assert_eq!(sha1::padding_length_for_input_length(0), 64); assert_eq!(sha1::padding_length_for_input_length(1), 63); assert_eq!(sha1::padding_length_for_input_length(2), 62); assert_eq!(sha1::padding_length_for_input_length(3), 61); assert_eq!(sha1::padding_length_for_input_length(4), 60); assert_eq!(sha1::padding_length_for_input_length(50), 14); assert_eq!(sha1::padding_length_for_input_length(54), 10); assert_eq!(sha1::padding_length_for_input_length(55), 9); assert_eq!(sha1::padding_length_for_input_length(56), 64 + 8); assert_eq!(sha1::padding_length_for_input_length(57), 64 + 7); assert_eq!(sha1::padding_length_for_input_length(62), 64 + 2); assert_eq!(sha1::padding_length_for_input_length(63), 64 + 1); assert_eq!(sha1::padding_length_for_input_length(64), 64); assert_eq!(sha1::padding_length_for_input_length(128), 64); assert_eq!( sha1::padding_length_for_input_length(64 * 100000), 64 ); } #[test] fn test_hash_ext() { let secret = "count=10&lat=37.351&user_id=1&\ long=-119.827&waffle=eggo" .as_bytes(); let hash = sha1::compute_hash(secret); let appended_str = "&waffle=liege".as_bytes(); let combined_hash = sha1::extend_hash(hash, secret.len(), appended_str); let mut concatenation = Vec::<u8>::new(); concatenation.extend_from_slice(secret); let padding = sha1::padding_for_length(secret.len()); concatenation.extend_from_slice(padding.as_slice()); concatenation.extend_from_slice(appended_str); assert_eq!( combined_hash, sha1::compute_hash(concatenation.as_slice()) ); } }