diff options
author | Werner Koch <[email protected]> | 2020-04-09 10:13:49 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2020-04-09 10:20:19 +0000 |
commit | 5d5b70ae0f515290a3d64daa1d687fe8c8477f76 (patch) | |
tree | aa378b37478aee2afc6d03ae3165d36fc44c4743 /common/sexputil.c | |
parent | scd:p15: Return a display S/N via Assuan. (diff) | |
download | gnupg-5d5b70ae0f515290a3d64daa1d687fe8c8477f76.tar.gz gnupg-5d5b70ae0f515290a3d64daa1d687fe8c8477f76.zip |
common: New function to map hash algo names.
* common/sexputil.c (hash_algo_to_string): New.
--
Libgcrypt expects lowercase names and it is cumbersome to downcase
those retrieved via gcry_md_algo_name. It is easier and also faster
to use a dedicated map function.
Diffstat (limited to 'common/sexputil.c')
-rw-r--r-- | common/sexputil.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/common/sexputil.c b/common/sexputil.c index 3ed95e43b..d6860f814 100644 --- a/common/sexputil.c +++ b/common/sexputil.c @@ -640,3 +640,44 @@ pubkey_algo_string (gcry_sexp_t s_pkey, enum gcry_pk_algos *r_algoid) xfree (algoname); return result; } + + +/* Map a hash algo id from gcrypt to a string. This is the same as + * gcry_md_algo_name but the returned string is lower case, as + * expected by libksba and it avoids some overhead. */ +const char * +hash_algo_to_string (int algo) +{ + static const struct + { + const char *name; + int algo; + } hashnames[] = + { + { "sha256", GCRY_MD_SHA256 }, + { "sha512", GCRY_MD_SHA512 }, + { "sha1", GCRY_MD_SHA1 }, + { "sha384", GCRY_MD_SHA384 }, + { "sha224", GCRY_MD_SHA224 }, + { "sha3-224", GCRY_MD_SHA3_224 }, + { "sha3-256", GCRY_MD_SHA3_256 }, + { "sha3-384", GCRY_MD_SHA3_384 }, + { "sha3-512", GCRY_MD_SHA3_512 }, + { "ripemd160", GCRY_MD_RMD160 }, + { "rmd160", GCRY_MD_RMD160 }, + { "md2", GCRY_MD_MD2 }, + { "md4", GCRY_MD_MD4 }, + { "tiger", GCRY_MD_TIGER }, + { "haval", GCRY_MD_HAVAL }, +#if GCRYPT_VERSION_NUMBER >= 0x010900 + { "sm3", GCRY_MD_SM3 }, +#endif + { "md5", GCRY_MD_MD5 } + }; + int i; + + for (i=0; i < DIM (hashnames); i++) + if (algo == hashnames[i].algo) + return hashnames[i].name; + return "?"; +} |