]> wimlib.net Git - wimlib/blob - include/wimlib/sha1.h
Use MIT license instead of CC0
[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 extern void
41 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1]);
42
43 static inline void
44 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
45 {
46         memcpy(dest, src, SHA1_HASH_SIZE);
47 }
48
49 static inline int
50 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
51 {
52         return memcmp(h1, h2, SHA1_HASH_SIZE);
53 }
54
55 static inline bool
56 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
57 {
58         return !hashes_cmp(h1, h2);
59 }
60
61 static inline bool
62 is_zero_hash(const u8 *hash)
63 {
64         return (hash == zero_hash || hashes_equal(hash, zero_hash));
65 }
66
67 #ifdef WITH_LIBCRYPTO
68
69 #include <openssl/sha.h>
70
71 #define sha1_init     SHA1_Init
72 #define sha1_update   SHA1_Update
73 #define sha1_final    SHA1_Final
74
75 static inline void
76 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE])
77 {
78         SHA1(buffer, len, hash);
79 }
80
81 #else /* WITH_LIBCRYPTO */
82
83 typedef struct {
84         u64 bytecount;
85         u32 state[5];
86         u8 buffer[64];
87 } SHA_CTX;
88
89 extern void
90 sha1_init(SHA_CTX *ctx);
91
92 extern void
93 sha1_update(SHA_CTX *ctx, const void *data, size_t len);
94
95 extern void
96 sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
97
98 extern void
99 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE]);
100
101 #endif /* !WITH_LIBCRYPTO */
102
103 #endif /* _WIMLIB_SHA1_H */