]> wimlib.net Git - wimlib/blob - include/wimlib/sha1.h
New helper macro: ALIGN()
[wimlib] / include / wimlib / sha1.h
1 /*
2  * sha1.h
3  *
4  * The author dedicates this file to the public domain.
5  * You can do whatever you want with this file.
6  */
7
8 #ifndef _WIMLIB_SHA1_H
9 #define _WIMLIB_SHA1_H
10
11 #include <string.h>
12
13 #include "wimlib/types.h"
14 #include "wimlib/util.h"
15
16 #define SHA1_HASH_SIZE 20
17
18 extern const u8 zero_hash[SHA1_HASH_SIZE];
19
20 extern void
21 sprint_hash(const u8 hash[SHA1_HASH_SIZE], tchar strbuf[SHA1_HASH_SIZE * 2 + 1]);
22
23 static inline void
24 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
25 {
26         memcpy(dest, src, SHA1_HASH_SIZE);
27 }
28
29 static inline int
30 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
31 {
32         return memcmp(h1, h2, SHA1_HASH_SIZE);
33 }
34
35 static inline bool
36 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
37 {
38         return !hashes_cmp(h1, h2);
39 }
40
41 static inline bool
42 is_zero_hash(const u8 *hash)
43 {
44         return (hash == zero_hash || hashes_equal(hash, zero_hash));
45 }
46
47 #ifdef WITH_LIBCRYPTO
48
49 #include <openssl/sha.h>
50
51 #define sha1_init     SHA1_Init
52 #define sha1_update   SHA1_Update
53 #define sha1_final    SHA1_Final
54
55 static inline void
56 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE])
57 {
58         SHA1(buffer, len, hash);
59 }
60
61 #else /* WITH_LIBCRYPTO */
62
63 typedef struct {
64         u64 bytecount;
65         u32 state[5];
66         u8 buffer[64];
67 } SHA_CTX;
68
69 extern void
70 sha1_init(SHA_CTX *ctx);
71
72 extern void
73 sha1_update(SHA_CTX *ctx, const void *data, size_t len);
74
75 extern void
76 sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
77
78 extern void
79 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE]);
80
81 #endif /* !WITH_LIBCRYPTO */
82
83 #endif /* _WIMLIB_SHA1_H */