sha1sumを試す

戻る

■コンパイル
$ gcc -o sha1sum-test sha1sum-test.c $(libgcrypt-config --cflags --libs)


■実行
$ ls -l | ./sha1sum-test.exe
c2e8bbd858663e36a1f1cbab31b25045c621abf5

$ ls -l | sha1sum
c2e8bbd858663e36a1f1cbab31b25045c621abf5 *-

以下ソースコードです。


/**
* $Id: sha1sum-test.html,v 1.1 2009/06/22 16:12:26 kishi Exp kishi $
*/

#define LENGTH 1024

#include <stdio.h>
#include <gcrypt.h>

int
main ()
{
  gcry_md_hd_t g;
  int i, size;
  char buf[LENGTH];
  unsigned char *hash;

  gcry_md_open (&g, GCRY_MD_SHA1, 0);

  while ((size = read (0, buf, LENGTH)) > 0) {
    gcry_md_write (g, buf, size);
  }

  gcry_md_final (g);

  hash = gcry_md_read (g, 0);

  for (i = 0; i < 20; i++) {
    printf ("%.2x", (int) hash[i]);
  }
  printf ("\n");

  return 0;
}

戻る

inserted by FC2 system