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 <lib_internal/crypto-wrapper.h>
30 #define ENSC_TESTSUITE
31 #include "lib_internal/coreassert.h"
33 #define HASH_BLOCKSIZE 0x10000000u
36 convertDigest(char res[], ensc_hash_context * h_ctx)
38 static char const HEX_DIGIT[] = "0123456789abcdef";
39 size_t d_size = ensc_crypto_hashctx_get_digestsize(h_ctx);
41 unsigned char digest[d_size];
44 if (ensc_crypto_hashctx_get_digest(h_ctx, digest, NULL, d_size)==-1)
47 for (size_t in=0; in<d_size; ++in) {
48 res[out++] = HEX_DIGIT[digest[in] >> 4];
49 res[out++] = HEX_DIGIT[digest[in] & 0x0f];
56 int main(int UNUSED argc, char *argv[])
58 int fd = open(argv[1], O_NOFOLLOW|O_NONBLOCK|O_RDONLY|O_NOCTTY);
59 ensc_hash_context hash_context;
60 ensc_hash_method const *method;
67 assert((method = ensc_crypto_hash_find(argv[2]))!=0);
68 assert(ensc_crypto_hashctx_init(&hash_context, method)!=-1);
70 assert(fstat(fd, &st)!=-1);
71 assert(ensc_crypto_hashctx_reset(&hash_context)!=-1);
75 while (offset < size) {
76 loff_t volatile buf_size = size-offset;
78 if (buf_size>HASH_BLOCKSIZE) buf_size = HASH_BLOCKSIZE;
80 assert((buf=mmap(0, buf_size, PROT_READ, MAP_SHARED, fd, offset))!=MAP_FAILED);
82 assert(ensc_crypto_hashctx_update(&hash_context, buf, buf_size)!=-1);
83 munmap((void *)(buf), buf_size);
86 assert(convertDigest(digest, &hash_context));
88 Vwrite(1, digest, strlen(digest));
91 ensc_crypto_hashctx_free(&hash_context);