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