Blame


1 53bf0b54 2023-02-23 op /*
2 53bf0b54 2023-02-23 op * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 53bf0b54 2023-02-23 op *
4 53bf0b54 2023-02-23 op * Permission to use, copy, modify, and distribute this software for any
5 53bf0b54 2023-02-23 op * purpose with or without fee is hereby granted, provided that the above
6 53bf0b54 2023-02-23 op * copyright notice and this permission notice appear in all copies.
7 53bf0b54 2023-02-23 op *
8 53bf0b54 2023-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 53bf0b54 2023-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 53bf0b54 2023-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 53bf0b54 2023-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 53bf0b54 2023-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 53bf0b54 2023-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 53bf0b54 2023-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 53bf0b54 2023-02-23 op */
16 53bf0b54 2023-02-23 op
17 53bf0b54 2023-02-23 op #define GOT_SHA1_STRING_ZERO "0000000000000000000000000000000000000000"
18 87a3ab84 2023-02-23 op #define GOT_SHA256_STRING_ZERO "0000000000000000000000000000000000000000000000000000000000000000"
19 53bf0b54 2023-02-23 op
20 ae25a666 2023-02-23 op #define GOT_HASH_DIGEST_MAXLEN SHA256_DIGEST_LENGTH
21 ae25a666 2023-02-23 op
22 53bf0b54 2023-02-23 op int got_parse_xdigit(uint8_t *, const char *);
23 87a3ab84 2023-02-23 op
24 53bf0b54 2023-02-23 op char *got_sha1_digest_to_str(const uint8_t *, char *, size_t);
25 87a3ab84 2023-02-23 op char *got_sha256_digest_to_str(const uint8_t *, char *, size_t);
26 87a3ab84 2023-02-23 op
27 87a3ab84 2023-02-23 op int got_parse_hash_digest(uint8_t *, const char *, enum got_hash_algorithm);
28 2f43cd69 2023-04-14 stsp
29 2f43cd69 2023-04-14 stsp /*
30 2f43cd69 2023-04-14 stsp * Write the string representation fo an object ID in the given buffer.
31 2f43cd69 2023-04-14 stsp * This buffer must be at least GOT_OBJECT_ID_HEX_MAXLEN bytes in size.
32 2f43cd69 2023-04-14 stsp * The output depneds on the hash function used by the repository format.
33 2f43cd69 2023-04-14 stsp */
34 2f43cd69 2023-04-14 stsp char *got_object_id_hex(struct got_object_id *, char *, size_t);
35 2f43cd69 2023-04-14 stsp
36 87a3ab84 2023-02-23 op int got_parse_object_id(struct got_object_id *, const char *,
37 87a3ab84 2023-02-23 op enum got_hash_algorithm);
38 ae25a666 2023-02-23 op
39 ae25a666 2023-02-23 op struct got_hash {
40 ae25a666 2023-02-23 op SHA1_CTX sha1_ctx;
41 ae25a666 2023-02-23 op SHA2_CTX sha256_ctx;
42 ae25a666 2023-02-23 op enum got_hash_algorithm algo;
43 ae25a666 2023-02-23 op };
44 ae25a666 2023-02-23 op
45 ae25a666 2023-02-23 op /*
46 ae25a666 2023-02-23 op * These functions allow to compute and check hashes.
47 ae25a666 2023-02-23 op * The hash function used is specified during got_hash_init.
48 ae25a666 2023-02-23 op * Data can be added with got_hash_update and, once done, the checksum
49 ae25a666 2023-02-23 op * saved in a buffer long at least GOT_HASH_DIGEST_MAXLEN bytes with
50 ae25a666 2023-02-23 op * got_hash_final or in an got_object_id with got_hash_final_object_id.
51 ae25a666 2023-02-23 op */
52 ae25a666 2023-02-23 op void got_hash_init(struct got_hash *, enum got_hash_algorithm);
53 ae25a666 2023-02-23 op void got_hash_update(struct got_hash *, const void *, size_t);
54 ae25a666 2023-02-23 op void got_hash_final(struct got_hash *, uint8_t *);
55 ae25a666 2023-02-23 op void got_hash_final_object_id(struct got_hash *, struct got_object_id *);
56 ae25a666 2023-02-23 op
57 ae25a666 2023-02-23 op /*
58 ae25a666 2023-02-23 op * Compare two hash digest; similar to memcmp().
59 ae25a666 2023-02-23 op */
60 ae25a666 2023-02-23 op int got_hash_cmp(enum got_hash_algorithm, uint8_t *, uint8_t *);