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 {
105 explicit HexPtr(
const std::string &s) { str = &s; }
106 bool IsValid()
const;
111 explicit AsciiPtr(
const std::string &s) { str = &s; }
122 template<
unsigned digest_size_, Algorithms algorithm_>
135 unsigned int length()
const {
return hash_length_ + algo_id_length_; }
138 assert(position < length());
139 return (position < hash_length_)
140 ? GetHashChar(position)
141 : GetAlgorithmIdentifierChar(position);
146 assert(position < hash_length_);
147 const char digit = (position % 2 == 0)
148 ? digest_.digest[position / 2] / 16
149 : digest_.digest[position / 2] % 16;
154 assert(position >= hash_length_);
155 return kAlgorithmIds[digest_.algorithm][position - hash_length_];
159 return static_cast<char>(c + ((c <= 9) ?
'0' :
'a' - 10));
182 assert((algorithm_ ==
kAny) || (a == algorithm_));
185 const std::string *str = hex.
str;
186 const unsigned length = str->length();
187 assert(length >= char_size);
189 for (
unsigned i = 0; i < char_size; i += 2) {
191 ((*str)[i] <=
'9' ? (*str)[i] -
'0' : (*str)[i] -
'a' + 10)*16 +
192 ((*str)[i+1] <=
'9' ? (*str)[i+1] -
'0' : (*str)[i+1] -
'a' + 10);
197 const unsigned char *digest_buffer,
233 const unsigned bytes = GetDigestSize();
234 for (
unsigned i = 0; i < bytes; ++i) {
249 std::string
ToString(
const bool with_suffix =
false)
const {
251 const bool use_suffix = with_suffix &&
HasSuffix();
252 const unsigned string_length = hex.length() + use_suffix;
253 std::string result(string_length, 0);
255 for (
unsigned int i = 0; i < hex.length(); ++i) {
260 result[string_length - 1] = suffix;
263 assert(result.length() == string_length);
277 const bool use_suffix = with_suffix &&
HasSuffix();
278 const unsigned string_length =
280 std::string result(string_length, 0);
282 unsigned l = hex.length();
283 for (
unsigned int hex_i = 0, result_i = 0; hex_i < l; ++hex_i, ++result_i) {
284 result[result_i] = toupper(hex[hex_i]);
286 result[++result_i] =
':';
291 result[string_length - 1] = suffix;
294 assert(result.length() == string_length);
305 return ToString(
true);
317 return MakePathExplicit(1, 2, suffix);
326 return ".cvmfsalt-" + ToStringWithSuffix();
352 const unsigned digits_per_level,
357 const bool use_suffix = (hash_suffix !=
kSuffixNone);
358 const unsigned string_length = hex.length() + dir_levels + use_suffix;
360 result.resize(string_length);
365 for (; i < hex.length(); ++i) {
366 if (i > 0 && (i % digits_per_level == 0)
367 && (i / digits_per_level <= dir_levels)) {
370 result[pos++] = hex[i];
375 result[pos++] = hash_suffix;
378 assert(i == hex.length());
379 assert(pos == string_length);
395 const uint32_t *partial = (
const uint32_t *)
digest;
396 return ntohl(*partial);
401 memset(
digest, 0, digest_size_);
416 return !(*
this == other);
419 bool operator <(const Digest<digest_size_, algorithm_> &other)
const {
421 return (this->
algorithm < other.algorithm);
423 if (this->
digest[i] > other.digest[i])
425 if (this->
digest[i] < other.digest[i])
449 Md5(
const char *chars,
const unsigned length);
454 Md5(
const uint64_t lo,
const uint64_t hi);
455 void ToIntPair(uint64_t *lo, uint64_t *hi)
const;
475 const unsigned char *digest_buffer,
514 const unsigned buffer_size,
520 const unsigned buffer_size,
524 const unsigned char *buffer,
525 const unsigned buffer_size,
527 inline void HmacString(
const std::string &key,
const std::string &content,
531 reinterpret_cast<const unsigned char *>(content.data()),
544 const std::string &content,
545 bool raw_output =
false);
548 const unsigned buffer_size);
559 #ifdef CVMFS_NAMESPACE_GUARD
563 #endif // CVMFS_CRYPTO_HASH_H_
void HashString(const std::string &content, Any *any_digest)
Hex(const Digest< digest_size_, algorithm_ > *digest)
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 cvmcache_hash &a, const cvmcache_hash &b)
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)
bool operator==(const cvmcache_hash &a, const cvmcache_hash &b)
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
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)