1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 Copyright 2012 Lennart Poettering
10 #include "gcrypt-util.h"
11 #include "hexdecoct.h"
13 void initialize_libgcrypt(bool secmem
) {
15 if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P
))
18 p
= gcry_check_version("1.4.5");
21 /* Turn off "secmem". Clients which wish to make use of this
22 * feature should initialize the library manually */
24 gcry_control(GCRYCTL_DISABLE_SECMEM
);
25 gcry_control(GCRYCTL_INITIALIZATION_FINISHED
, 0);
28 int string_hashsum(const char *s
, size_t len
, int md_algorithm
, char **out
) {
29 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
34 initialize_libgcrypt(false);
36 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
37 assert(hash_size
> 0);
39 gcry_md_open(&md
, md_algorithm
, 0);
43 gcry_md_write(md
, s
, len
);
45 hash
= gcry_md_read(md
, 0);
49 enc
= hexmem(hash
, hash_size
);