slight code simplification, seeing that clear_meta(NULL) is fine
[got-portable.git] / lib / got_lib_hash.h
blob37b902e172faee0d19cafb57ef7739a0e7153dbd
1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #define GOT_SHA1_STRING_ZERO "0000000000000000000000000000000000000000"
18 #define GOT_SHA256_STRING_ZERO "0000000000000000000000000000000000000000000000000000000000000000"
20 #define GOT_HASH_DIGEST_MAXLEN SHA256_DIGEST_LENGTH
21 #define GOT_HASH_DIGEST_STRING_MAXLEN SHA256_DIGEST_STRING_LENGTH
23 int got_parse_xdigit(uint8_t *, const char *);
25 char *got_sha1_digest_to_str(const uint8_t *, char *, size_t);
26 char *got_sha256_digest_to_str(const uint8_t *, char *, size_t);
27 char *got_hash_digest_to_str(const uint8_t *, char *, size_t,
28 enum got_hash_algorithm);
30 int got_parse_hash_digest(uint8_t *, const char *, enum got_hash_algorithm);
33 * Write the string representation fo an object ID in the given buffer.
34 * This buffer must be at least GOT_OBJECT_ID_HEX_MAXLEN bytes in size.
35 * The output depends on the hash function used by the repository format.
37 char *got_object_id_hex(struct got_object_id *, char *, size_t);
39 int got_parse_object_id(struct got_object_id *, const char *,
40 enum got_hash_algorithm);
42 static inline int
43 got_hash_digest_length(enum got_hash_algorithm algo)
45 switch (algo) {
46 case GOT_HASH_SHA1:
47 return SHA1_DIGEST_LENGTH;
48 case GOT_HASH_SHA256:
49 return SHA256_DIGEST_LENGTH;
50 default:
51 return 0;
55 static inline int
56 got_hash_digest_string_length(enum got_hash_algorithm algo)
58 switch (algo) {
59 case GOT_HASH_SHA1:
60 return SHA1_DIGEST_STRING_LENGTH;
61 case GOT_HASH_SHA256:
62 return SHA256_DIGEST_STRING_LENGTH;
63 default:
64 return 0;
68 struct got_hash {
69 SHA1_CTX sha1_ctx;
70 SHA2_CTX sha256_ctx;
71 enum got_hash_algorithm algo;
75 * These functions allow to compute and check hashes.
76 * The hash function used is specified during got_hash_init.
77 * Data can be added with got_hash_update and, once done, the checksum
78 * saved in a buffer long at least GOT_HASH_DIGEST_MAXLEN bytes with
79 * got_hash_final or in an got_object_id with got_hash_final_object_id.
81 void got_hash_init(struct got_hash *, enum got_hash_algorithm);
82 void got_hash_update(struct got_hash *, const void *, size_t);
83 void got_hash_final(struct got_hash *, uint8_t *);
84 void got_hash_final_object_id(struct got_hash *, struct got_object_id *);
87 * Compare two hash digest; similar to memcmp().
89 int got_hash_cmp(enum got_hash_algorithm, uint8_t *, uint8_t *);