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
use crate::sha01;

/// Compute the SHA-0 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-0 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::sha0;
/// let data = "This string will be hashed.";
/// let padding = sha0::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-0 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::sha0;
/// let data = "This string will be hashed.";
/// let padding_length =
///     sha0::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-0 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-0 hash.
///
/// # Example
///
/// ```
/// # use extendhash::sha0;
/// let secret_data = "abc".as_bytes();
/// let hash = sha0::compute_hash(secret_data);
/// assert_eq!(hash, [
///     0x01, 0x64, 0xb8, 0xa9, 0x14, 0xcd, 0x2a, 0x5e, 0x74, 0xc4,
///     0xf7, 0xff, 0x08, 0x2c, 0x4d, 0x97, 0xf1, 0xed, 0xf8, 0x80]);
/// ```
pub fn compute_hash(input: &[u8]) -> [u8; 20] {
    sha01::compute_hash(input, sha01::HashType::SHA0)
}

/// 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::sha0;
/// let secret_data = "This is a secret!".as_bytes();
/// let hash = sha0::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 = sha0::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 = sha0::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,
///     sha0::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::SHA0,
    )
}

#[cfg(test)]
mod tests {
    use crate::sha0;

    #[test]
    fn abc_test() {
        assert_eq!(
            sha0::compute_hash("abc".as_bytes()),
            [
                0x01, 0x64, 0xb8, 0xa9, 0x14, 0xcd, 0x2a, 0x5e, 0x74,
                0xc4, 0xf7, 0xff, 0x08, 0x2c, 0x4d, 0x97, 0xf1, 0xed,
                0xf8, 0x80
            ]
        );
    }

    #[test]
    fn slightly_longer_test() {
        let input = "abcdbcdecdefdefgefghfghighi\
                     jhijkijkljklmklmnlmnomnopnopq";
        assert_eq!(
            sha0::compute_hash(input.as_bytes()),
            [
                0xd2, 0x51, 0x6e, 0xe1, 0xac, 0xfa, 0x5b, 0xaf, 0x33,
                0xdf, 0xc1, 0xc4, 0x71, 0xe4, 0x38, 0x44, 0x9e, 0xf1,
                0x34, 0xc8
            ]
        );
    }

    #[test]
    fn padding_length_tests() {
        assert_eq!(sha0::padding_length_for_input_length(0), 64);
        assert_eq!(sha0::padding_length_for_input_length(1), 63);
        assert_eq!(sha0::padding_length_for_input_length(2), 62);
        assert_eq!(sha0::padding_length_for_input_length(3), 61);
        assert_eq!(sha0::padding_length_for_input_length(4), 60);

        assert_eq!(sha0::padding_length_for_input_length(50), 14);
        assert_eq!(sha0::padding_length_for_input_length(54), 10);
        assert_eq!(sha0::padding_length_for_input_length(55), 9);
        assert_eq!(sha0::padding_length_for_input_length(56), 64 + 8);
        assert_eq!(sha0::padding_length_for_input_length(57), 64 + 7);
        assert_eq!(sha0::padding_length_for_input_length(62), 64 + 2);
        assert_eq!(sha0::padding_length_for_input_length(63), 64 + 1);
        assert_eq!(sha0::padding_length_for_input_length(64), 64);
        assert_eq!(sha0::padding_length_for_input_length(128), 64);
        assert_eq!(
            sha0::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 = sha0::compute_hash(secret);

        let appended_str = "&waffle=liege".as_bytes();
        let combined_hash =
            sha0::extend_hash(hash, secret.len(), appended_str);

        let mut concatenation = Vec::<u8>::new();
        concatenation.extend_from_slice(secret);
        let padding = sha0::padding_for_length(secret.len());
        concatenation.extend_from_slice(padding.as_slice());
        concatenation.extend_from_slice(appended_str);
        assert_eq!(
            combined_hash,
            sha0::compute_hash(concatenation.as_slice())
        );
    }
}