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
//! # SHA-1 Hash Function Implementation
//!
//! This module provides a complete implementation of SHA-1 (Secure Hash Algorithm 1),
//! developed by the NSA and published as a federal standard in 1995. SHA-1 was widely
//! used but is now considered cryptographically weak due to collision vulnerabilities.
//!
//! ⚠️ **Security Warning**: SHA-1 is **deprecated** for cryptographic use due to
//! practical collision attacks demonstrated in 2017. Use SHA-256 or newer algorithms
//! for security-critical applications.
//!
//! ## Algorithm Overview
//!
//! - **Output Size**: 160 bits (20 bytes, 40 hex characters)
//! - **Block Size**: 512 bits (64 bytes)
//! - **Structure**: Merkle-Damgård construction with 80 rounds
//! - **Operations**: Bitwise functions, addition, left rotation
//!
//! ## Security Status
//!
//! SHA-1 vulnerabilities include:
//! - **SHAttered Attack (2017)**: Practical collision attack
//! - **Theoretical Attacks**: Various cryptanalytic techniques
//! - **Deprecation**: Removed from TLS, code signing, and other protocols
//!
//! ## Historical Significance
//!
//! SHA-1 was the foundation for many security protocols and served as a
//! stepping stone to more secure hash functions like SHA-256. Understanding
//! SHA-1 helps in learning modern cryptographic hash design.
//!
//! ## Examples
//!
//! ```rust
//! use ruscrypt::hash::sha1;
//!
//! // Basic SHA-1 hashing
//! let hash = sha1::hash("Hello, World!").unwrap();
//! println!("SHA-1: {}", hash); // Outputs 40 hex characters
//!
//! // Well-known test vectors
//! let abc_hash = sha1::hash("abc").unwrap();
//! assert_eq!(abc_hash, "a9993e364706816aba3e25717850c26c9cd0d89d");
//! ```
use Result;
/// Computes the SHA-1 hash of the input text.
///
/// This function processes the input through the SHA-1 algorithm and returns
/// the resulting 160-bit hash as a 40-character hexadecimal string.
///
/// # Arguments
///
/// * `input` - The text to hash
///
/// # Returns
///
/// Returns a 40-character hexadecimal string representing the 160-bit SHA-1 hash.
///
/// # Algorithm Overview
///
/// 1. Convert input to bytes
/// 2. Apply SHA-1 algorithm (padding, length appending, compression)
/// 3. Convert 20-byte result to hexadecimal representation
///
/// # Examples
///
/// ```rust
/// use ruscrypt::hash::sha1;
///
/// // Hash a message
/// let hash = sha1::hash("The quick brown fox").unwrap();
/// println!("SHA-1: {}", hash);
///
/// // Test with known vectors
/// let empty = sha1::hash("").unwrap();
/// assert_eq!(empty, "da39a3ee5e6b4b0d3255bfef95601890afd80709");
///
/// let abc = sha1::hash("abc").unwrap();
/// assert_eq!(abc, "a9993e364706816aba3e25717850c26c9cd0d89d");
/// ```
///
/// # Output Format
///
/// The returned string is exactly 40 characters long, containing only
/// lowercase hexadecimal digits (0-9, a-f).
///
/// # Security Consideration
///
/// While this function correctly implements SHA-1, the algorithm itself
/// is deprecated. Consider using SHA-256 for new applications requiring
/// cryptographic security.
///
/// # Errors
///
/// Currently infallible, but returns `Result` for consistency and
/// potential future error conditions.
/// Core SHA-1 implementation following FIPS PUB 180-1 specification.
///
/// This function implements the complete SHA-1 algorithm including all
/// preprocessing steps and the 80-round compression function.
///
/// # Arguments
///
/// * `input` - Raw bytes to hash
///
/// # Returns
///
/// Returns a 20-byte array containing the SHA-1 hash.
///
/// # Algorithm Structure
///
/// ## Hash Value Initialization
/// Five 32-bit words initialized to specific constants:
/// - H₀ = 0x67452301
/// - H₁ = 0xEFCDAB89
/// - H₂ = 0x98BADCFE
/// - H₃ = 0x10325476
/// - H₄ = 0xC3D2E1F0
///
/// ## Preprocessing
/// 1. **Padding**: Append '1' bit, then '0' bits until length ≡ 448 (mod 512)
/// 2. **Length**: Append original bit length as 64-bit big-endian integer
/// 3. **Result**: Padded message is multiple of 512 bits
///
/// ## Message Processing
/// - Process in 512-bit chunks
/// - Expand each chunk into 80 32-bit words
/// - Apply compression function with 80 rounds
///
/// # Round Functions
///
/// SHA-1 uses four different functions across its rounds:
/// - **f₀₋₁₉(B,C,D) = (B ∧ C) ∨ (¬B ∧ D)**: Conditional function
/// - **f₂₀₋₃₉(B,C,D) = B ⊕ C ⊕ D**: Parity function
/// - **f₄₀₋₅₉(B,C,D) = (B ∧ C) ∨ (B ∧ D) ∨ (C ∧ D)**: Majority function
/// - **f₆₀₋₇₉(B,C,D) = B ⊕ C ⊕ D**: Parity function (repeated)
///
/// # Constants
///
/// Four 32-bit constants used in different round ranges:
/// - K₀₋₁₉ = 0x5A827999
/// - K₂₀₋₃₉ = 0x6ED9EBA1
/// - K₄₀₋₅₉ = 0x8F1BBCDC
/// - K₆₀₋₇₉ = 0xCA62C1D6
///
/// # Word Expansion
///
/// The 16 input words are expanded to 80 words using:
/// W[i] = (W[i-3] ⊕ W[i-8] ⊕ W[i-14] ⊕ W[i-16]) <<<< 1
///
/// This provides better diffusion than the original SHA-0 design.
///
/// # Security Implementation Notes
///
/// This educational implementation prioritizes clarity over performance
/// and includes all specified operations for learning purposes.