12 #ifndef CVMFS_CRYPTO_HASH_H_
13 #define CVMFS_CRYPTO_HASH_H_
15 #include <arpa/inet.h>
29 #ifdef CVMFS_NAMESPACE_GUARD
30 namespace CVMFS_NAMESPACE_GUARD {
102 explicit HexPtr(
const std::string &s) { str = &s; }
103 bool IsValid()
const;
108 explicit AsciiPtr(
const std::string &s) { str = &s; }
119 template<
unsigned digest_size_, Algorithms algorithm_>
132 unsigned int length()
const {
return hash_length_ + algo_id_length_; }
135 assert(position < length());
136 return (position < hash_length_) ? GetHashChar(position)
137 : GetAlgorithmIdentifierChar(position);
142 assert(position < hash_length_);
143 const char digit = (position % 2 == 0)
144 ? digest_.digest[position / 2] / 16
145 : digest_.digest[position / 2] % 16;
150 assert(position >= hash_length_);
151 return kAlgorithmIds[digest_.algorithm][position - hash_length_];
155 return static_cast<char>(c + ((c <= 9) ?
'0' :
'a' - 10));
173 assert((algorithm_ ==
kAny) || (a == algorithm_));
176 const std::string *str = hex.
str;
177 const unsigned length = str->length();
178 assert(length >= char_size);
180 for (
unsigned i = 0; i < char_size; i += 2) {
181 this->
digest[i / 2] = ((*str)[i] <=
'9' ? (*str)[i] -
'0'
182 : (*str)[i] -
'a' + 10)
184 + ((*str)[i + 1] <=
'9' ? (*str)[i + 1] -
'0'
185 : (*str)[i + 1] -
'a' + 10);
190 const unsigned char *digest_buffer,
225 const unsigned bytes = GetDigestSize();
226 for (
unsigned i = 0; i < bytes; ++i) {
241 std::string
ToString(
const bool with_suffix =
false)
const {
243 const bool use_suffix = with_suffix &&
HasSuffix();
244 const unsigned string_length = hex.length() + use_suffix;
245 std::string result(string_length, 0);
247 for (
unsigned int i = 0; i < hex.length(); ++i) {
252 result[string_length - 1] = suffix;
255 assert(result.length() == string_length);
269 const bool use_suffix = with_suffix &&
HasSuffix();
272 std::string result(string_length, 0);
274 unsigned l = hex.length();
275 for (
unsigned int hex_i = 0, result_i = 0; hex_i < l; ++hex_i, ++result_i) {
276 result[result_i] = toupper(hex[hex_i]);
278 result[++result_i] =
':';
283 result[string_length - 1] = suffix;
286 assert(result.length() == string_length);
306 std::string
MakePath()
const {
return MakePathExplicit(1, 2, suffix); }
314 return ".cvmfsalt-" + ToStringWithSuffix();
340 const unsigned digits_per_level,
345 const bool use_suffix = (hash_suffix !=
kSuffixNone);
346 const unsigned string_length = hex.length() + dir_levels + use_suffix;
348 result.resize(string_length);
353 for (; i < hex.length(); ++i) {
354 if (i > 0 && (i % digits_per_level == 0)
355 && (i / digits_per_level <= dir_levels)) {
358 result[pos++] = hex[i];
363 result[pos++] = hash_suffix;
366 assert(i == hex.length());
367 assert(pos == string_length);
383 const uint32_t *partial = (
const uint32_t *)
digest;
384 return ntohl(*partial);
402 return !(*
this == other);
405 bool operator<(const Digest<digest_size_, algorithm_> &other)
const {
407 return (this->
algorithm < other.algorithm);
409 if (this->
digest[i] > other.digest[i])
411 if (this->
digest[i] < other.digest[i])
435 Md5(
const char *chars,
const unsigned length);
440 Md5(
const uint64_t lo,
const uint64_t hi);
441 void ToIntPair(uint64_t *lo, uint64_t *hi)
const;
463 const unsigned char *digest_buffer,
502 const unsigned buffer_size,
508 const unsigned buffer_size,
512 const unsigned char *buffer,
513 const unsigned buffer_size,
515 inline void HmacString(
const std::string &key,
const std::string &content,
518 reinterpret_cast<const unsigned char *>(content.data()),
531 const std::string &content,
532 bool raw_output =
false);
535 const unsigned buffer_size);
546 #ifdef CVMFS_NAMESPACE_GUARD
550 #endif // CVMFS_CRYPTO_HASH_H_
void HashString(const std::string &content, Any *any_digest)
Hex(const Digest< digest_size_, algorithm_ > *digest)
bool operator==(const Digest< digest_size_, algorithm_ > &other) const
const unsigned kMaxContextSize
void set_suffix(const Suffix s)
const unsigned int hash_length_
std::string MakeAlternativePath() const
bool HashFile(const std::string &filename, Any *any_digest)
string Sha256String(const string &content)
std::string ToString(const bool with_suffix=false) const
void Randomize(const uint64_t seed)
std::string ToStringWithSuffix() const
const unsigned kAlgorithmIdSizes[]
char GetAlgorithmIdentifierChar(const unsigned int position) const
const unsigned kMaxDigestSize
void Hmac(const string &key, const unsigned char *buffer, const unsigned buffer_size, Any *any_digest)
assert((mem||(size==0))&&"Out Of Memory")
string Sha256File(const string &filename)
char ToHex(const char c) const
Digest(const Algorithms a, const unsigned char *digest_buffer, const Suffix s=kSuffixNone)
void InitSeed(const uint64_t seed)
unsigned char digest[digest_size_]
ContextPtr(const Algorithms a)
HexPtr(const std::string &s)
const Digest< digest_size_, algorithm_ > & digest_
const unsigned kBlockSizes[]
unsigned GetDigestSize() const
const unsigned kDigestSizes[]
std::string ToFingerprint(const bool with_suffix=false) const
string Sha256Mem(const unsigned char *buffer, const unsigned buffer_size)
const unsigned kMaxAlgorithmIdentifierSize
const char kSuffixHistory
bool HasSuffix(const std::string &str, const std::string &suffix, const bool ignore_case)
bool operator!=(const Digest< digest_size_, algorithm_ > &other) const
ContextPtr(const Algorithms a, void *b)
char GetHashChar(const unsigned int position) const
AsciiPtr(const std::string &s)
static int Init(const loader::LoaderExports *loader_exports)
void Final(ContextPtr context, Any *any_digest)
const char kSuffixMicroCatalog
const char kSuffixMetainfo
Any(const Algorithms a, const unsigned char *digest_buffer, const Suffix suffix=kSuffixNone)
const char * kAlgorithmIds[]
uint32_t Partial32() const
void HashMem(const unsigned char *buffer, const unsigned buffer_size, Any *any_digest)
unsigned GetContextSize(const Algorithms algorithm)
std::string MakePathWithoutSuffix() const
Any(const Algorithms a, const HexPtr hex, const char suffix=kSuffixNone)
void Update(const unsigned char *buffer, const unsigned buffer_length, ContextPtr context)
unsigned GetHexSize() const
unsigned int length() const
std::string MakePathExplicit(const unsigned dir_levels, const unsigned digits_per_level, const Suffix hash_suffix=kSuffixNone) const
void HmacString(const std::string &key, const std::string &content, Any *any_digest)
Any MkFromSuffixedHexPtr(const HexPtr hex)
Digest(const Algorithms a, const HexPtr hex, const char s=0)
Algorithms ParseHashAlgorithm(const string &algorithm_option)
const char kSuffixPartial
char operator[](const unsigned int position) const
bool HashFd(int fd, Any *any_digest)
Any MkFromHexPtr(const HexPtr hex, const char suffix)
std::string Hmac256(const std::string &key, const std::string &content, bool raw_output)
std::string MakePath() const
bool operator>(const Digest< digest_size_, algorithm_ > &other) const
const char kSuffixCatalog
const char kSuffixTemporary
void Randomize(Prng *prng)
const char kSuffixCertificate
uint32_t Next(const uint64_t boundary)
const unsigned int algo_id_length_
Any(const Algorithms a, const char s=kSuffixNone)