]> wimlib.net Git - wimlib/blob - src/sha1.h
Fix various issues
[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 static inline void copy_hash(u8 dest[SHA1_HASH_SIZE],
13                              const u8 src[SHA1_HASH_SIZE])
14 {
15         memcpy(dest, src, SHA1_HASH_SIZE);
16 }
17
18 static inline void random_hash(u8 hash[SHA1_HASH_SIZE])
19 {
20         randomize_byte_array(hash, SHA1_HASH_SIZE);
21 }
22
23 static inline bool hashes_equal(const u8 h1[SHA1_HASH_SIZE],
24                                 const u8 h2[SHA1_HASH_SIZE])
25 {
26         return memcmp(h1, h2, SHA1_HASH_SIZE) == 0;
27 }
28 static inline int hashes_cmp(const u8 h1[SHA1_HASH_SIZE],
29                              const u8 h2[SHA1_HASH_SIZE])
30 {
31         return memcmp(h1, h2, SHA1_HASH_SIZE);
32 }
33
34 /* Prints a hash code field. */
35 static inline void print_hash(const u8 hash[SHA1_HASH_SIZE])
36 {
37         print_byte_field(hash, SHA1_HASH_SIZE);
38 }
39
40 static inline bool is_zero_hash(const u8 hash[SHA1_HASH_SIZE])
41 {
42         if (hash)
43                 for (u8 i = 0; i < SHA1_HASH_SIZE / 4; i++)
44                         if (((u32*)hash)[i])
45                                 return false;
46         return true;
47 }
48
49 static inline void zero_hash(u8 hash[SHA1_HASH_SIZE])
50 {
51         memset(hash, 0, SHA1_HASH_SIZE);
52 }
53
54
55 #ifdef WITH_LIBCRYPTO
56
57 #include <openssl/sha.h>
58 #define sha1_buffer   SHA1
59 #define sha1_init     SHA1_Init
60 #define sha1_update   SHA1_Update
61 #define sha1_final    SHA1_Final
62
63 #else /* WITH_LIBCRYPTO */
64
65 typedef struct {
66     u32 state[5];
67     u32 count[2];
68     u8  buffer[64];
69 } SHA_CTX;
70
71 extern void sha1_buffer(const u8 buffer[], size_t len, u8 hash[SHA1_HASH_SIZE]);
72 extern void sha1_init(SHA_CTX *ctx);
73 extern void sha1_update(SHA_CTX *ctx, const u8 data[], size_t len);
74 extern void sha1_final(u8 hash[SHA1_HASH_SIZE], SHA_CTX *ctx);
75
76 #endif /* !WITH_LIBCRYPTO */
77
78 extern int sha1sum(const char *filename, u8 hash[SHA1_HASH_SIZE]);
79
80
81
82 #endif /* _WIMLIB_SHA1_H */