]> wimlib.net Git - wimlib/blob - include/wimlib/sha1.h
e1def3d9f2cb274c9bd282fe47bba9fdfc55447e
[wimlib] / include / wimlib / sha1.h
1 /*
2  * sha1.h
3  *
4  * Copyright 2022 Eric Biggers
5  *
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
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
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.
26  */
27
28 #ifndef _WIMLIB_SHA1_H
29 #define _WIMLIB_SHA1_H
30
31 #include <string.h>
32
33 #include "wimlib/types.h"
34 #include "wimlib/util.h"
35
36 #define SHA1_HASH_SIZE 20
37
38 extern const u8 zero_hash[SHA1_HASH_SIZE];
39
40 #define SHA1_HASH_STRING_LEN    (2 * SHA1_HASH_SIZE + 1)
41 extern void
42 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_STRING_LEN]);
43
44 static inline void
45 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
46 {
47         memcpy(dest, src, SHA1_HASH_SIZE);
48 }
49
50 static inline int
51 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
52 {
53         return memcmp(h1, h2, SHA1_HASH_SIZE);
54 }
55
56 static inline bool
57 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
58 {
59         return !hashes_cmp(h1, h2);
60 }
61
62 static inline bool
63 is_zero_hash(const u8 *hash)
64 {
65         return (hash == zero_hash || hashes_equal(hash, zero_hash));
66 }
67
68 #ifdef WITH_LIBCRYPTO
69
70 #include <openssl/sha.h>
71
72 #define sha1_init     SHA1_Init
73 #define sha1_update   SHA1_Update
74 #define sha1_final    SHA1_Final
75
76 static inline void
77 sha1(const void *data, size_t len, u8 hash[SHA1_HASH_SIZE])
78 {
79         SHA1(data, len, hash);
80 }
81
82 #else /* WITH_LIBCRYPTO */
83
84 typedef struct {
85         u64 bytecount;
86         u32 state[5];
87         u8 buffer[64];
88 } SHA_CTX;
89
90 extern void
91 sha1_init(SHA_CTX *ctx);
92
93 extern void
94 sha1_update(SHA_CTX *ctx, const void *data, size_t len);
95
96 extern void
97 sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
98
99 extern void
100 sha1(const void *data, size_t len, u8 hash[SHA1_HASH_SIZE]);
101
102 #endif /* !WITH_LIBCRYPTO */
103
104 #endif /* _WIMLIB_SHA1_H */