Blame


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