]> wimlib.net Git - wimlib/blob - include/wimlib/sha1.h
1f7c50f70b35112457eda3b8b6bed01897b3a2d6
[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 bool
26 hashes_equal(const u8 h1[SHA1_HASH_SIZE], const u8 h2[SHA1_HASH_SIZE])
27 {
28         return memcmp(h1, h2, SHA1_HASH_SIZE) == 0;
29 }
30
31 static inline int
32 hashes_cmp(const u8 h1[SHA1_HASH_SIZE],
33                              const u8 h2[SHA1_HASH_SIZE])
34 {
35         return memcmp(h1, h2, SHA1_HASH_SIZE);
36 }
37
38 static inline void
39 print_hash(const u8 hash[SHA1_HASH_SIZE], FILE *out)
40 {
41         print_byte_field(hash, SHA1_HASH_SIZE, out);
42 }
43
44 static inline bool
45 is_zero_hash(const u8 hash[SHA1_HASH_SIZE])
46 {
47         if (hash)
48                 for (u8 i = 0; i < SHA1_HASH_SIZE / 4; i++)
49                         if (((const u32*)hash)[i])
50                                 return false;
51         return true;
52 }
53
54 static inline void
55 zero_out_hash(u8 hash[SHA1_HASH_SIZE])
56 {
57         memset(hash, 0, SHA1_HASH_SIZE);
58 }
59
60
61 #ifdef WITH_LIBCRYPTO
62
63 #include <openssl/sha.h>
64 static inline void
65 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE])
66 {
67         SHA1(buffer, len, hash);
68 }
69 #define sha1_init     SHA1_Init
70 #define sha1_update   SHA1_Update
71 #define sha1_final    SHA1_Final
72
73 #else /* WITH_LIBCRYPTO */
74
75 typedef struct {
76         u32 state[5];
77         u32 count[2];
78         u8 buffer[64];
79 } SHA_CTX;
80
81 extern void
82 sha1_buffer(const void *buffer, size_t len, u8 hash[SHA1_HASH_SIZE]);
83
84 extern void
85 sha1_init(SHA_CTX *ctx);
86
87 extern void
88 sha1_update(SHA_CTX *ctx, const void *data, size_t len);
89
90 extern void
91 sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
92
93 #endif /* !WITH_LIBCRYPTO */
94
95 #endif /* _WIMLIB_SHA1_H */