1 |
|
|
/** |
2 |
|
|
* This file is part of the CernVM File System. |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
#include "swissknife_hash.h" |
6 |
|
|
|
7 |
|
|
#include <alloca.h> |
8 |
|
|
#include <sys/types.h> |
9 |
|
|
#include <sys/uio.h> |
10 |
|
|
#include <unistd.h> |
11 |
|
|
|
12 |
|
|
#include <cstdio> |
13 |
|
|
|
14 |
|
|
#include "hash.h" |
15 |
|
|
#include "logging.h" |
16 |
|
|
#include "smalloc.h" |
17 |
|
|
#include "util/posix.h" |
18 |
|
|
|
19 |
|
|
// Hash stdin and print the digest to stdout |
20 |
|
|
int swissknife::CommandHash::Main(const swissknife::ArgumentList &args) { |
21 |
|
|
bool fingerprint = (args.find('f') != args.end()); |
22 |
|
|
shash::Any hash(shash::ParseHashAlgorithm(*args.find('a')->second)); |
23 |
|
|
|
24 |
|
|
shash::ContextPtr context_ptr(hash.algorithm); |
25 |
|
|
context_ptr.buffer = smalloc(context_ptr.size); |
26 |
|
|
|
27 |
|
|
unsigned char buf[kPageSize]; |
28 |
|
|
size_t n; |
29 |
|
|
shash::Init(context_ptr); |
30 |
|
|
while ((n = read(fileno(stdin), buf, kPageSize)) > 0) { |
31 |
|
|
shash::Update(buf, n, context_ptr); |
32 |
|
|
} |
33 |
|
|
shash::Final(context_ptr, &hash); |
34 |
|
|
LogCvmfs(kLogCvmfs, kLogStdout, "%s", fingerprint |
35 |
|
|
? hash.ToFingerprint().c_str() |
36 |
|
|
: hash.ToString().c_str()); |
37 |
|
|
|
38 |
|
|
return 0; |
39 |
|
|
} |