=================================================================== RCS file: /home/cvs/OpenXM/src/ox_ntl/crypt/sha1/sha1test.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -p -r1.1 -r1.2 --- OpenXM/src/ox_ntl/crypt/sha1/sha1test.c 2004/05/16 15:02:39 1.1 +++ OpenXM/src/ox_ntl/crypt/sha1/sha1test.c 2004/06/20 10:59:01 1.2 @@ -1,15 +1,17 @@ -/* $OpenXM$ */ +/* $OpenXM: OpenXM/src/ox_ntl/crypt/sha1/sha1test.c,v 1.1 2004/05/16 15:02:39 iwane Exp $ */ /* openssl sha1 ${FILE} */ #include +#include +#include #include "sha1.h" void Usage(char *cmd) { - printf("Usage: %s file\n", cmd); + printf("Usage: %s [-f][-s] file\n", cmd); exit (-1); } @@ -22,27 +24,71 @@ main(int argc, char *argv[]) int ret; int i; unsigned char sha[100]; + int flag = 0; - if (argc != 2) + enum { + SHA = 1, + F_SHA = 2 + }; + + for (i = 1; i < argc; i++) { + if (strcmp(argv[i], "-f") == 0) { + flag |= F_SHA; + } else if (strcmp(argv[i], "-s") == 0) { + flag |= SHA; + } else { + break; + } + } + + if (flag == 0) + flag |= SHA; + argc -= i; + + if (argc != 1) Usage(argv[0]); - fp = fopen(argv[1], "r"); - if (fp == NULL) { - fprintf(stderr, "file not found: %s\n", argv[1]); - exit (-1); + argv += i - 1; + + if (flag & SHA) { + fp = fopen(argv[1], "r"); + if (fp == NULL) { + fprintf(stderr, "file not found: %s\n", argv[1]); + exit (-1); + } + + ret = fread(buf, 1, sizeof(buf), fp); + + sha1(sha, (unsigned char *)buf, ret); + + fclose(fp); + + printf("SHA1(%s)= ", argv[1]); + for (i = 0; i < 20; i++) { + printf("%02x", sha[i] & 0xff); + } + printf("\n"); } - ret = fread(buf, 1, sizeof(buf), fp); + if (flag & F_SHA) { + int fd; - sha1(sha, (unsigned char *)buf, ret); + fd = open(argv[1], O_RDONLY, NULL); + if (fd == -1) { + fprintf(stderr, "file not found: %s\n", argv[1]); + exit (-1); + } - fclose(fp); + fsha1(sha, fd); + close(fd); - printf("SHA1(%s)= ", argv[1]); - for (i = 0; i < 20; i++) { - printf("%02x", sha[i] & 0xff); + printf("SHA1(%s)= ", argv[1]); + for (i = 0; i < 20; i++) { + printf("%02x", sha[i] & 0xff); + } + printf("\n"); } - printf("\n"); + return (0); }