3 // Copyright (C) 2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
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; version 2 of the License.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <beecrypt/beecrypt.h>
29 #define ENSC_TESTSUITE
30 #include "lib_internal/coreassert.h"
32 #define HASH_BLOCKSIZE 0x10000000u
35 convertDigest(char res[], hashFunctionContext * h_ctx)
37 static char const HEX_DIGIT[] = "0123456789abcdef";
38 size_t d_size = h_ctx->algo->digestsize;
40 unsigned char digest[d_size];
43 if (hashFunctionContextDigest(h_ctx, digest)==-1)
46 for (size_t in=0; in<d_size; ++in) {
47 res[out++] = HEX_DIGIT[digest[in] >> 4];
48 res[out++] = HEX_DIGIT[digest[in] & 0x0f];
55 int main(int UNUSED argc, char *argv[])
57 int fd = open(argv[1], O_NOFOLLOW|O_NONBLOCK|O_RDONLY|O_NOCTTY);
58 hashFunctionContext hash_context;
59 hashFunction const *method;
65 assert((method = hashFunctionFind(argv[2]))!=0);
66 assert(hashFunctionContextInit(&hash_context, method)!=-1);
68 assert(fstat(fd, &st)!=-1);
69 assert(hashFunctionContextReset(&hash_context)!=-1);
73 while (offset < size) {
74 loff_t volatile buf_size = size-offset;
76 if (buf_size>HASH_BLOCKSIZE) buf_size = HASH_BLOCKSIZE;
78 assert((buf=mmap(0, buf_size, PROT_READ, MAP_SHARED, fd, offset))!=0);
80 assert(hashFunctionContextUpdate(&hash_context, buf, buf_size)!=-1);
81 munmap((void *)(buf), buf_size);
84 assert(convertDigest(digest, &hash_context));
86 Vwrite(1, digest, strlen(digest));
89 hashFunctionContextFree(&hash_context);