Fixed package group
[platform/upstream/nettle.git] / sha-example.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include <nettle/sha1.h>
5
6 #define BUF_SIZE 1000
7
8 static void
9 display_hex(unsigned length, uint8_t *data)
10 {
11   unsigned i;
12
13   for (i = 0; i<length; i++)
14     printf("%02x ", data[i]);
15
16   printf("\n");
17 }
18
19 int
20 main(int argc, char **argv)
21 {
22   struct sha1_ctx ctx;
23   uint8_t buffer[BUF_SIZE];
24   uint8_t digest[SHA1_DIGEST_SIZE];
25   
26   sha1_init(&ctx);
27   for (;;)
28   {
29     int done = fread(buffer, 1, sizeof(buffer), stdin);
30     sha1_update(&ctx, done, buffer);
31     if (done < sizeof(buffer))
32       break;
33   }
34   if (ferror(stdin))
35     return EXIT_FAILURE;
36
37   sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest);
38
39   display_hex(SHA1_DIGEST_SIZE, digest);
40   return EXIT_SUCCESS;  
41 }