Compute SHA-1 checksums of regular files to be stored with index data.
[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 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 typedef uint32_t md5_uint32;
27
28 /* Structure to save state of computation between the single steps.  */
29 struct sha1_ctx
30 {
31   md5_uint32 A;
32   md5_uint32 B;
33   md5_uint32 C;
34   md5_uint32 D;
35   md5_uint32 E;
36
37   md5_uint32 total[2];
38   md5_uint32 buflen;
39   char buffer[128] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
40 };
41
42
43 /* Initialize structure containing state of computation. */
44 extern void sha1_init_ctx (struct sha1_ctx *ctx);
45
46 /* Starting with the result of former calls of this function (or the
47    initialization function update the context for the next LEN bytes
48    starting at BUFFER.
49    It is necessary that LEN is a multiple of 64!!! */
50 extern void sha1_process_block (const void *buffer, size_t len,
51                                 struct sha1_ctx *ctx);
52
53 /* Starting with the result of former calls of this function (or the
54    initialization function update the context for the next LEN bytes
55    starting at BUFFER.
56    It is NOT required that LEN is a multiple of 64.  */
57 extern void sha1_process_bytes (const void *buffer, size_t len,
58                                 struct sha1_ctx *ctx);
59
60 /* Process the remaining bytes in the buffer and put result from CTX
61    in first 20 bytes following RESBUF.  The result is always in little
62    endian byte order, so that a byte-wise output yields to the wanted
63    ASCII representation of the message digest.
64
65    IMPORTANT: On some systems it is required that RESBUF be correctly
66    aligned for a 32 bits value.  */
67 extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf);
68
69
70 /* Put result from CTX in first 20 bytes following RESBUF.  The result is
71    always in little endian byte order, so that a byte-wise output yields
72    to the wanted ASCII representation of the message digest.
73
74    IMPORTANT: On some systems it is required that RESBUF is correctly
75    aligned for a 32 bits value.  */
76 extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf);
77
78 /* An object-oriented wrapper around checksumming functionality. */
79 class SHA1Checksum {
80 private:
81     struct sha1_ctx ctx;
82     char resbuf[20] __attribute__ ((__aligned__ (__alignof__ (md5_uint32))));
83
84 public:
85     SHA1Checksum();
86     ~SHA1Checksum();
87
88     void process(void *data, size_t len);
89     const uint8_t *checksum();
90     size_t checksum_size() const { return 20; }
91 };
92
93 #endif