1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
3 * Copyright (C) 2012 Michael Vrable <vrable@cs.hmc.edu>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 /* A generic interface for computing digests of data, used for both
21 * content-based deduplication and for data integrity verification. */
23 #ifndef CUMULUS_HASH_H
24 #define CUMULUS_HASH_H 1
29 /* An object-oriented wrapper around checksumming functionality. */
32 Hash() : digest_bytes(NULL) { }
36 virtual void update(const void *data, size_t len) = 0;
37 // Returns the size of the buffer returned by digest, in bytes.
38 virtual size_t digest_size() const = 0;
39 // Returns the name of the hash algorithm.
40 virtual std::string name() const = 0;
42 // Calls update with the contents of the data found in the specified file.
43 bool update_from_file(const char *filename);
44 // Finalizes the digest and returns a pointer to a raw byte array
45 // containing the hash.
46 const uint8_t *digest();
48 // Returns the digest in text form: "<digest name>=<hex digits>".
49 std::string digest_str();
51 //typedef Hash *(*HashConstructor)();
52 static void Register(const std::string& name, Hash *(*constructor)());
54 static Hash *New(const std::string& name);
56 // Computes and returns the hash of a file on disk.
57 static std::string hash_file(const char *filename);
60 virtual const uint8_t *finalize() = 0;
63 const uint8_t *digest_bytes;