Replace boost::scoped_ptr with std::unique_ptr.
[cumulus.git] / hash.h
1 /* Cumulus: Smart Filesystem Backup to Dumb Servers
2  *
3  * Copyright (C) 2012  Michael Vrable <vrable@cs.hmc.edu>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 /* A generic interface for computing digests of data, used for both
21  * content-based deduplication and for data integrity verification. */
22
23 #ifndef CUMULUS_HASH_H
24 #define CUMULUS_HASH_H 1
25
26 #include <stdint.h>
27 #include <string>
28
29 /* An object-oriented wrapper around checksumming functionality. */
30 class Hash {
31 public:
32     Hash() : digest_bytes(NULL) { }
33     virtual ~Hash() { }
34
35     // 
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;
41
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();
47
48     // Returns the digest in text form: "<digest name>=<hex digits>".
49     std::string digest_str();
50
51     //typedef Hash *(*HashConstructor)();
52     static void Register(const std::string& name, Hash *(*constructor)());
53     static Hash *New();
54     static Hash *New(const std::string& name);
55
56     // Computes and returns the hash of a file on disk.
57     static std::string hash_file(const char *filename);
58
59 protected:
60     virtual const uint8_t *finalize() = 0;
61
62 private:
63     const uint8_t *digest_bytes;
64 };
65
66 void hash_init();
67
68 #endif