4 * Copyright 2022-2023 Eric Biggers
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #ifndef _WIMLIB_SHA1_H
29 #define _WIMLIB_SHA1_H
33 #include "wimlib/types.h"
34 #include "wimlib/util.h"
36 #define SHA1_HASH_SIZE 20
37 #define SHA1_BLOCK_SIZE 64
42 u8 buffer[SHA1_BLOCK_SIZE];
46 sha1_init(struct sha1_ctx *ctx);
49 sha1_update(struct sha1_ctx *ctx, const void *data, size_t len);
52 sha1_final(struct sha1_ctx *ctx, u8 hash[SHA1_HASH_SIZE]);
55 sha1(const void *data, size_t len, u8 hash[SHA1_HASH_SIZE]);
57 extern const u8 zero_hash[SHA1_HASH_SIZE];
59 #define SHA1_HASH_STRING_LEN (2 * SHA1_HASH_SIZE + 1)
61 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_STRING_LEN]);
64 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
66 memcpy(dest, src, SHA1_HASH_SIZE);
70 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
72 return memcmp(h1, h2, SHA1_HASH_SIZE);
76 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
78 return !hashes_cmp(h1, h2);
82 is_zero_hash(const u8 *hash)
84 return hash == zero_hash || hashes_equal(hash, zero_hash);
87 #endif /* _WIMLIB_SHA1_H */