]> wimlib.net Git - wimlib/blob - include/wimlib/sha1.h
76e6d5e1d7082136e7614e7f6b5e5ed678868f6c
[wimlib] / include / wimlib / sha1.h
1 #ifndef _WIMLIB_SHA1_H
2 #define _WIMLIB_SHA1_H
3
4 #include "wimlib/types.h"
5 #include "wimlib/util.h"
6
7 #include <string.h>
8
9 #define SHA1_HASH_SIZE 20
10
11 extern const u8 zero_hash[SHA1_HASH_SIZE];
12
13 static inline void
14 copy_hash(u8 dest[SHA1_HASH_SIZE], const u8 src[SHA1_HASH_SIZE])
15 {
16         memcpy(dest, src, SHA1_HASH_SIZE);
17 }
18
19 static inline void
20 random_hash(u8 hash[SHA1_HASH_SIZE])
21 {
22         randomize_byte_array(hash, SHA1_HASH_SIZE);
23 }
24
25 static inline int
26 hashes_cmp(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
27 {
28         return memcmp(h1, h2, SHA1_HASH_SIZE);
29 }
30
31 static inline bool
32 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
33 {
34         return !hashes_cmp(h1, h2);
35 }
36
37 static inline void
38 print_hash(const u8 hash[SHA1_HASH_SIZE], FILE *out)
39 {
40         print_byte_field(hash, SHA1_HASH_SIZE, out);
41 }
42
43 static inline bool
44 is_zero_hash(const u8 *hash)
45 {
46         if (!hash)
47                 return true;
48         return hashes_equal(hash, zero_hash);
49 }
50
51 static inline void
52 zero_out_hash(u8 hash[SHA1_HASH_SIZE])
53 {
54         copy_hash(hash, zero_hash);
55 }
56
57 #ifdef WITH_LIBCRYPTO
58
59 #include <openssl/sha.h>
60 static inline void
61 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE])
62 {
63         SHA1(buffer, len, hash);
64 }
65 #define sha1_init     SHA1_Init
66 #define sha1_update   SHA1_Update
67 #define sha1_final    SHA1_Final
68
69 #else /* WITH_LIBCRYPTO */
70
71 typedef struct {
72         u32 state[5];
73         u32 count[2];
74         u8 buffer[64];
75 } SHA_CTX;
76
77 extern void
78 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE]);
79
80 extern void
81 sha1_init(SHA_CTX *ctx);
82
83 extern void
84 sha1_update(SHA_CTX *ctx, const void *data, size_t len);
85
86 extern void
87 sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
88
89 #endif /* !WITH_LIBCRYPTO */
90
91 #endif /* _WIMLIB_SHA1_H */