/src/wget2/libwget/hash_printf.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2012 Tim Ruehsen |
3 | | * Copyright (c) 2015-2024 Free Software Foundation, Inc. |
4 | | * |
5 | | * This file is part of libwget. |
6 | | * |
7 | | * Libwget is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as published by |
9 | | * the Free Software Foundation, either version 3 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * Libwget is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libwget. If not, see <https://www.gnu.org/licenses/>. |
19 | | * |
20 | | * |
21 | | * Digest/hash helper routines |
22 | | * |
23 | | * 21.12.2012 Tim Ruehsen created |
24 | | * |
25 | | */ |
26 | | |
27 | | #include <config.h> |
28 | | |
29 | | #include <stddef.h> |
30 | | |
31 | | #include <wget.h> |
32 | | #include "private.h" |
33 | | |
34 | | /** |
35 | | * \file |
36 | | * \brief Hash convenience functions |
37 | | * \defgroup libwget-hash Hash convenience functions |
38 | | * @{ |
39 | | * |
40 | | * Provides Hash helper functions |
41 | | */ |
42 | | |
43 | | /** |
44 | | * \param[in] algorithm The hash algorithm to use |
45 | | * \param[out] out Output string buffer |
46 | | * \param[in] outsize Size of output string buffer |
47 | | * \param[in] fmt Printf-like format specifier |
48 | | * \param[in] ... List of arguments |
49 | | * |
50 | | * Calculate the hash from the string generated via the |
51 | | * printf-style \p fmt and the following arguments and place it as hexadecimal string |
52 | | * into \p out. |
53 | | * |
54 | | * The ideal length of \p out would be wget_hash_get_len(type) * 2 + 1. |
55 | | */ |
56 | | void wget_hash_printf_hex(wget_digest_algorithm algorithm, char *out, size_t outsize, const char *fmt, ...) |
57 | 0 | { |
58 | 0 | char *plaintext = NULL; |
59 | 0 | va_list args; |
60 | 0 | size_t len; |
61 | |
|
62 | 0 | va_start(args, fmt); |
63 | 0 | len = wget_vasprintf(&plaintext, fmt, args); |
64 | 0 | va_end(args); |
65 | |
|
66 | 0 | if (!plaintext) |
67 | 0 | return; |
68 | | |
69 | 0 | unsigned char tmp[256], *digest = tmp; |
70 | 0 | size_t digestlen = wget_hash_get_len(algorithm); |
71 | |
|
72 | 0 | if (digestlen > sizeof(tmp)) { |
73 | 0 | digest = wget_malloc(digestlen); |
74 | 0 | } |
75 | |
|
76 | 0 | if (digest) { |
77 | 0 | int rc; |
78 | |
|
79 | 0 | if ((rc = wget_hash_fast(algorithm, plaintext, len, digest)) == 0) { |
80 | 0 | wget_memtohex(digest, digestlen, out, outsize); |
81 | 0 | } else { |
82 | 0 | *out = 0; |
83 | 0 | error_printf(_("Failed to hash (%d)\n"), rc); |
84 | 0 | } |
85 | |
|
86 | 0 | if (digest != tmp) |
87 | 0 | xfree(digest); |
88 | 0 | } else { |
89 | 0 | error_printf(_("%s: Failed to malloc %zu bytes\n"), __func__, digestlen); |
90 | 0 | } |
91 | |
|
92 | | xfree(plaintext); |
93 | 0 | } |
94 | | |
95 | | /**@}*/ |