The map::at method does not always exist, so instead use map::find.
[cumulus.git] / sha1.h
1 /* Declarations of functions and data types used for SHA1 sum library
2  * functions.
3  * part of Cumulus: Smart Filesystem Backup to Dumb Servers
4  *
5  * Copyright (C) 2000, 2001, 2003, 2005 Free Software Foundation, Inc.
6  * Copyright (C) 2006-2007  The Regents of the University of California
7  *
8  * Original code (in C) is taken from GNU coreutils (Debian package 5.97-5).
9  * Modifications by Michael Vrable <mvrable@cs.ucsd.edu> to integrate into
10  * Cumulus.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  */
26
27 #ifndef SHA1_H
28 # define SHA1_H 1
29
30 # include <stdio.h>
31 # include <stdint.h>
32
33 #include <string>
34
35 typedef uint32_t md5_uint32;
36
37 /* Structure to save state of computation between the single steps.  */
38 struct sha1_ctx
39 {
40   md5_uint32 A;
41   md5_uint32 B;
42   md5_uint32 C;
43   md5_uint32 D;
44   md5_uint32 E;
45
46   md5_uint32 total[2];
47   md5_uint32 buflen;
48   char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
49 };
50
51
52 /* Initialize structure containing state of computation. */
53 extern void sha1_init_ctx (struct sha1_ctx *ctx);
54
55 /* Starting with the result of former calls of this function (or the
56    initialization function update the context for the next LEN bytes
57    starting at BUFFER.
58    It is necessary that LEN is a multiple of 64!!! */
59 extern void sha1_process_block (const void *buffer, size_t len,
60                                 struct sha1_ctx *ctx);
61
62 /* Starting with the result of former calls of this function (or the
63    initialization function update the context for the next LEN bytes
64    starting at BUFFER.
65    It is NOT required that LEN is a multiple of 64.  */
66 extern void sha1_process_bytes (const void *buffer, size_t len,
67                                 struct sha1_ctx *ctx);
68
69 /* Process the remaining bytes in the buffer and put result from CTX
70    in first 20 bytes following RESBUF.  The result is always in little
71    endian byte order, so that a byte-wise output yields to the wanted
72    ASCII representation of the message digest.
73
74    IMPORTANT: On some systems it is required that RESBUF be correctly
75    aligned for a 32 bits value.  */
76 extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
77
78
79 /* Put result from CTX in first 20 bytes following RESBUF.  The result is
80    always in little endian byte order, so that a byte-wise output yields
81    to the wanted ASCII representation of the message digest.
82
83    IMPORTANT: On some systems it is required that RESBUF is correctly
84    aligned for a 32 bits value.  */
85 extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
86
87 /* An object-oriented wrapper around checksumming functionality. */
88 class SHA1Checksum {
89 private:
90     struct sha1_ctx ctx;
91     char resbuf[20] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
92
93 public:
94     SHA1Checksum();
95     ~SHA1Checksum();
96
97     void process(const void *data, size_t len);
98     bool process_file(const char *filename);
99     const uint8_t *checksum();
100     size_t checksum_size() const { return 20; }
101     std::string checksum_str();
102 };
103
104 #endif