diff options
| author | Ludovic Courtès <ludo@gnu.org> | 2020-06-23 11:46:05 +0200 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2020-06-27 23:42:20 +0200 |
| commit | 3fb6b8f30444d41963ba5bdd441123a6d2df17bd (patch) | |
| tree | 0159dc3f3ceefdbe75e42587afc6d711a7c460dc /nix/libutil | |
| parent | 4b4f890cb03d7c5b958fe08553dfad2f39a1d4f2 (diff) | |
daemon: Map directly to gcrypt hash functions.
* nix/libutil/hash.hh (HashType): Map directly to GCRY_MD_ values.
(md5HashSize, sha1HashSize, sha256HashSize, sha512HashSize): Remove.
* nix/libutil/hash.cc (Hash::Hash): Use 'gcry_md_get_algo_dlen'.
Diffstat (limited to 'nix/libutil')
| -rw-r--r-- | nix/libutil/hash.cc | 8 | ||||
| -rw-r--r-- | nix/libutil/hash.hh | 17 |
2 files changed, 12 insertions, 13 deletions
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc index ea69aa64f91..251f18f60e8 100644 --- a/nix/libutil/hash.cc +++ b/nix/libutil/hash.cc | |||
| @@ -38,11 +38,9 @@ Hash::Hash() | |||
| 38 | Hash::Hash(HashType type) | 38 | Hash::Hash(HashType type) |
| 39 | { | 39 | { |
| 40 | this->type = type; | 40 | this->type = type; |
| 41 | if (type == htMD5) hashSize = md5HashSize; | 41 | hashSize = gcry_md_get_algo_dlen(type); |
| 42 | else if (type == htSHA1) hashSize = sha1HashSize; | 42 | |
| 43 | else if (type == htSHA256) hashSize = sha256HashSize; | 43 | if (hashSize == 0) throw Error("unknown hash type"); |
| 44 | else if (type == htSHA512) hashSize = sha512HashSize; | ||
| 45 | else throw Error("unknown hash type"); | ||
| 46 | assert(hashSize <= maxHashSize); | 44 | assert(hashSize <= maxHashSize); |
| 47 | memset(hash, 0, maxHashSize); | 45 | memset(hash, 0, maxHashSize); |
| 48 | } | 46 | } |
diff --git a/nix/libutil/hash.hh b/nix/libutil/hash.hh index 6b5e47cd8a2..7357a34e1dd 100644 --- a/nix/libutil/hash.hh +++ b/nix/libutil/hash.hh | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include <gcrypt.h> | ||
| 4 | |||
| 3 | #include "types.hh" | 5 | #include "types.hh" |
| 4 | #include "serialise.hh" | 6 | #include "serialise.hh" |
| 5 | 7 | ||
| @@ -7,16 +9,15 @@ | |||
| 7 | namespace nix { | 9 | namespace nix { |
| 8 | 10 | ||
| 9 | 11 | ||
| 10 | typedef enum { htUnknown, htMD5, htSHA1, htSHA256, htSHA512 } HashType; | ||
| 11 | |||
| 12 | |||
| 13 | const int md5HashSize = 16; | ||
| 14 | const int sha1HashSize = 20; | ||
| 15 | const int sha256HashSize = 32; | ||
| 16 | const int sha512HashSize = 64; | ||
| 17 | |||
| 18 | extern const string base32Chars; | 12 | extern const string base32Chars; |
| 19 | 13 | ||
| 14 | typedef enum { | ||
| 15 | htUnknown = 0, | ||
| 16 | htMD5 = GCRY_MD_MD5, | ||
| 17 | htSHA1 = GCRY_MD_SHA1, | ||
| 18 | htSHA256 = GCRY_MD_SHA256, | ||
| 19 | htSHA512 = GCRY_MD_SHA512 | ||
| 20 | } HashType; | ||
| 20 | 21 | ||
| 21 | struct Hash | 22 | struct Hash |
| 22 | { | 23 | { |
