StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StHyperHashSha256.h
1 /* Declarations of functions and data types used for SHA256 and SHA224 sum
2  library functions.
3  Copyright (C) 2005, 2006, 2008 Free Software Foundation, Inc.
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 3 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
16  along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 
18 #ifndef __ST_HYPERHASH_SHA256_H
19 #define __ST_HYPERHASH_SHA256_H
20 
21 #include <cstdio>
22 #include <stdint.h>
23 #include <string>
24 
25 namespace StHyperHash
26 {
27 
28 /* Structure to save state of computation between the single steps. */
29 struct st_sha256_ctx {
30  uint32_t state[8];
31 
32  uint32_t total[2];
33  size_t buflen;
34  uint32_t buffer[32];
35 };
36 
37 enum { ST_SHA224_DIGEST_SIZE = 224 / 8 };
38 enum { ST_SHA256_DIGEST_SIZE = 256 / 8 };
39 
40 /* Initialize structure containing state of computation. */
41 extern void st_sha256_init_ctx (struct st_sha256_ctx *ctx);
42 extern void st_sha224_init_ctx (struct st_sha256_ctx *ctx);
43 
44 /* Starting with the result of former calls of this function (or the
45  initialization function update the context for the next LEN bytes
46  starting at BUFFER.
47  It is necessary that LEN is a multiple of 64!!! */
48 extern void st_sha256_process_block (const void *buffer, size_t len,
49  struct st_sha256_ctx *ctx);
50 
51 /* Starting with the result of former calls of this function (or the
52  initialization function update the context for the next LEN bytes
53  starting at BUFFER.
54  It is NOT required that LEN is a multiple of 64. */
55 extern void st_sha256_process_bytes (const void *buffer, size_t len,
56  struct st_sha256_ctx *ctx);
57 
58 /* Process the remaining bytes in the buffer and put result from CTX
59  in first 32 (28) bytes following RESBUF. The result is always in little
60  endian byte order, so that a byte-wise output yields to the wanted
61  ASCII representation of the message digest. */
62 extern void *st_sha256_finish_ctx (struct st_sha256_ctx *ctx, void *resbuf);
63 extern void *st_sha224_finish_ctx (struct st_sha256_ctx *ctx, void *resbuf);
64 
65 
66 /* Put result from CTX in first 32 (28) bytes following RESBUF. The result is
67  always in little endian byte order, so that a byte-wise output yields
68  to the wanted ASCII representation of the message digest. */
69 extern void *st_sha256_read_ctx (const struct st_sha256_ctx *ctx, void *resbuf);
70 extern void *st_sha224_read_ctx (const struct st_sha256_ctx *ctx, void *resbuf);
71 
72 
73 /* Compute SHA256 (SHA224) message digest for bytes read from STREAM. The
74  resulting message digest number will be written into the 32 (28) bytes
75  beginning at RESBLOCK. */
76 extern int st_sha256_stream (FILE *stream, void *resblock);
77 extern int st_sha224_stream (FILE *stream, void *resblock);
78 
79 /* Compute SHA256 (SHA224) message digest for LEN bytes beginning at BUFFER. The
80  result is always in little endian byte order, so that a byte-wise
81  output yields to the wanted ASCII representation of the message
82  digest. */
83 extern void *st_sha256_buffer (const char *buffer, size_t len, void *resblock);
84 extern void *st_sha224_buffer (const char *buffer, size_t len, void *resblock);
85 
86 std::string sha256sum(const std::string& str);
87 
88 } // namespace StHyperHash
89 
90 #endif // __ST_SHA256_H