CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_hash.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
6 #include "cmd_hash.h"
7 
8 #ifndef __STDC_FORMAT_MACROS
9 #define __STDC_FORMAT_MACROS
10 #endif
11 
12 #include <inttypes.h>
13 #include <stdint.h>
14 
15 #include <cstdio>
16 #include <cstring>
17 
18 #include "crypto/hash.h"
19 #include "publish/except.h"
20 #include "util/logging.h"
21 
22 int publish::CmdHash::Main(const Options &options) {
23  std::string algorithm = options.GetString("algorithm");
24  shash::Any hash(shash::ParseHashAlgorithm(algorithm));
25  // MD5 is not a content hash algorithm but we deal with it in this utility
26  // nevertheless
27  if (algorithm == "md5")
28  hash.algorithm = shash::kMd5;
29  if (hash.algorithm == shash::kAny)
30  throw EPublish("unknown hash algorithm: " + algorithm);
31 
32  if (options.Has("input")) {
33  shash::HashString(options.GetString("input"), &hash);
34  } else {
35  shash::HashFd(fileno(stdin), &hash);
36  }
38  options.Has("fingerprint") ? hash.ToFingerprint().c_str()
39  : hash.ToString().c_str());
40  if (options.Has("split")) {
41  if (hash.algorithm != shash::kMd5)
42  throw EPublish("split int representation only supported for MD5");
43 
44  uint64_t high, low;
45  shash::Md5 md5_hash;
46  memcpy(md5_hash.digest, hash.digest, shash::kDigestSizes[shash::kMd5]);
47  md5_hash.ToIntPair(&high, &low);
48  // SQLite uses int64, not uint64
50  " [%" PRId64 " %" PRId64 "]",
51  static_cast<int64_t>(high), static_cast<int64_t>(low));
52  }
54 
55  return 0;
56 }
void HashString(const std::string &content, Any *any_digest)
Definition: hash.cc:268
bool Has(const std::string &key) const
Definition: command.h:114
void ToIntPair(uint64_t *lo, uint64_t *hi) const
Definition: hash.cc:385
std::string GetString(const std::string &key) const
Definition: command.h:123
std::string ToString(const bool with_suffix=false) const
Definition: hash.h:249
Algorithms algorithm
Definition: hash.h:125
unsigned char digest[digest_size_]
Definition: hash.h:124
char algorithm
std::string ToFingerprint(const bool with_suffix=false) const
Definition: hash.h:275
Algorithms ParseHashAlgorithm(const string &algorithm_option)
Definition: hash.cc:72
bool HashFd(int fd, Any *any_digest)
Definition: hash.cc:321
virtual int Main(const Options &options)
Definition: cmd_hash.cc:22
const unsigned kDigestSizes[]
Definition: hash.h:69
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)
Definition: logging.cc:528