2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 d1cda826 2017-11-06 stsp * Permission to use, copy, modify, and distribute this software for any
5 d1cda826 2017-11-06 stsp * purpose with or without fee is hereby granted, provided that the above
6 d1cda826 2017-11-06 stsp * copyright notice and this permission notice appear in all copies.
8 d1cda826 2017-11-06 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d1cda826 2017-11-06 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d1cda826 2017-11-06 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d1cda826 2017-11-06 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d1cda826 2017-11-06 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d1cda826 2017-11-06 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d1cda826 2017-11-06 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 d1cda826 2017-11-06 stsp #include <sys/types.h>
18 d1cda826 2017-11-06 stsp #include <sha1.h>
19 d1cda826 2017-11-06 stsp #include <errno.h>
20 a1fd68d8 2018-01-12 stsp #include <stdio.h>
21 d1cda826 2017-11-06 stsp #include <stdlib.h>
22 d1cda826 2017-11-06 stsp #include <limits.h>
24 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
27 4277420a 2019-06-29 stsp got_parse_xdigit(uint8_t *val, const char *hex)
33 d1cda826 2017-11-06 stsp lval = strtol(hex, &ep, 16);
34 d1cda826 2017-11-06 stsp if (hex[0] == '\0' || *ep != '\0')
36 d1cda826 2017-11-06 stsp if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))
39 d1cda826 2017-11-06 stsp *val = (uint8_t)lval;
44 d1cda826 2017-11-06 stsp got_parse_sha1_digest(uint8_t *digest, const char *line)
46 d1cda826 2017-11-06 stsp uint8_t b = 0;
47 d1cda826 2017-11-06 stsp char hex[3] = {'\0', '\0', '\0'};
50 d1cda826 2017-11-06 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
51 d1cda826 2017-11-06 stsp if (line[0] == '\0' || line[1] == '\0')
53 d1cda826 2017-11-06 stsp for (j = 0; j < 2; j++) {
54 d1cda826 2017-11-06 stsp hex[j] = *line;
57 4277420a 2019-06-29 stsp if (!got_parse_xdigit(&b, hex))
59 d1cda826 2017-11-06 stsp digest[i] = b;
66 a1fd68d8 2018-01-12 stsp got_sha1_digest_to_str(const uint8_t *digest, char *buf, size_t size)
68 a1fd68d8 2018-01-12 stsp char *p = buf;
69 a1fd68d8 2018-01-12 stsp char hex[3];
72 a1fd68d8 2018-01-12 stsp if (size < SHA1_DIGEST_STRING_LENGTH)
73 a1fd68d8 2018-01-12 stsp return NULL;
75 a1fd68d8 2018-01-12 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
76 a1fd68d8 2018-01-12 stsp snprintf(hex, sizeof(hex), "%.2x", digest[i]);
77 a1fd68d8 2018-01-12 stsp p[0] = hex[0];
78 a1fd68d8 2018-01-12 stsp p[1] = hex[1];
81 a1fd68d8 2018-01-12 stsp p[0] = '\0';