nettle: Example

1 
1 4 Example
1 *********
1 
1 A simple example program that reads a file from standard input and
1 writes its SHA1 check-sum on standard output should give the flavor of
1 Nettle.
1 
1      #include <stdio.h>
1      #include <stdlib.h>
1 
1      #include <nettle/sha1.h>
1 
1      #define BUF_SIZE 1000
1 
1      static void
1      display_hex(unsigned length, uint8_t *data)
1      {
1        unsigned i;
1 
1        for (i = 0; i<length; i++)
1          printf("%02x ", data[i]);
1 
1        printf("\n");
1      }
1 
1      int
1      main(int argc, char **argv)
1      {
1        struct sha1_ctx ctx;
1        uint8_t buffer[BUF_SIZE];
1        uint8_t digest[SHA1_DIGEST_SIZE];
1 
1        sha1_init(&ctx);
1        for (;;)
1        {
1          int done = fread(buffer, 1, sizeof(buffer), stdin);
1          sha1_update(&ctx, done, buffer);
1          if (done < sizeof(buffer))
1            break;
1        }
1        if (ferror(stdin))
1          return EXIT_FAILURE;
1 
1        sha1_digest(&ctx, SHA1_DIGEST_SIZE, digest);
1 
1        display_hex(SHA1_DIGEST_SIZE, digest);
1        return EXIT_SUCCESS;
1      }
1 
1    On a typical Unix system, this program can be compiled and linked
1 with the command line
1      gcc sha-example.c -o sha-example -lnettle
1