4 * Copyright 2022 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
38 extern const u8 zero_hash[SHA1_HASH_SIZE];
41 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1]);
44 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
46 memcpy(dest, src, SHA1_HASH_SIZE);
50 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
52 return memcmp(h1, h2, SHA1_HASH_SIZE);
56 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
58 return !hashes_cmp(h1, h2);
62 is_zero_hash(const u8 *hash)
64 return (hash == zero_hash || hashes_equal(hash, zero_hash));
69 #include <openssl/sha.h>
71 #define sha1_init SHA1_Init
72 #define sha1_update SHA1_Update
73 #define sha1_final SHA1_Final
76 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE])
78 SHA1(buffer, len, hash);
81 #else /* WITH_LIBCRYPTO */
90 sha1_init(SHA_CTX *ctx);
93 sha1_update(SHA_CTX *ctx, const void *data, size_t len);
96 sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
99 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE]);
101 #endif /* !WITH_LIBCRYPTO */
103 #endif /* _WIMLIB_SHA1_H */