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