]> wimlib.net Git - wimlib/blob - include/wimlib/sha1.h
v1.14.4
[wimlib] / include / wimlib / sha1.h
1 /*
2  * sha1.h
3  *
4  * The following copying information applies to this specific source code file:
5  *
6  * Written in 2013-2015 by Eric Biggers <ebiggers3@gmail.com>
7  *
8  * To the extent possible under law, the author(s) have dedicated all copyright
9  * and related and neighboring rights to this software to the public domain
10  * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
11  * Dedication (the "CC0").
12  *
13  * This software is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
16  *
17  * You should have received a copy of the CC0 along with this software; if not
18  * see <http://creativecommons.org/publicdomain/zero/1.0/>.
19  */
20
21 #ifndef _WIMLIB_SHA1_H
22 #define _WIMLIB_SHA1_H
23
24 #include <string.h>
25
26 #include "wimlib/types.h"
27 #include "wimlib/util.h"
28
29 #define SHA1_HASH_SIZE 20
30
31 extern const u8 zero_hash[SHA1_HASH_SIZE];
32
33 extern void
34 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1]);
35
36 static inline void
37 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
38 {
39         memcpy(dest, src, SHA1_HASH_SIZE);
40 }
41
42 static inline int
43 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
44 {
45         return memcmp(h1, h2, SHA1_HASH_SIZE);
46 }
47
48 static inline bool
49 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
50 {
51         return !hashes_cmp(h1, h2);
52 }
53
54 static inline bool
55 is_zero_hash(const u8 *hash)
56 {
57         return (hash == zero_hash || hashes_equal(hash, zero_hash));
58 }
59
60 #ifdef WITH_LIBCRYPTO
61
62 #include <openssl/sha.h>
63
64 #define sha1_init     SHA1_Init
65 #define sha1_update   SHA1_Update
66 #define sha1_final    SHA1_Final
67
68 static inline void
69 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE])
70 {
71         SHA1(buffer, len, hash);
72 }
73
74 #else /* WITH_LIBCRYPTO */
75
76 typedef struct {
77         u64 bytecount;
78         u32 state[5];
79         u8 buffer[64];
80 } SHA_CTX;
81
82 extern void
83 sha1_init(SHA_CTX *ctx);
84
85 extern void
86 sha1_update(SHA_CTX *ctx, const void *data, size_t len);
87
88 extern void
89 sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
90
91 extern void
92 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE]);
93
94 #endif /* !WITH_LIBCRYPTO */
95
96 #endif /* _WIMLIB_SHA1_H */