version 1.1, 2004/05/16 15:02:39 |
version 1.2, 2004/06/20 10:59:01 |
|
|
/* $OpenXM$ */ |
/* $OpenXM: OpenXM/src/ox_ntl/crypt/sha1/sha1test.c,v 1.1 2004/05/16 15:02:39 iwane Exp $ */ |
|
|
/* openssl sha1 ${FILE} */ |
/* openssl sha1 ${FILE} */ |
|
|
#include <stdio.h> |
#include <stdio.h> |
|
#include <unistd.h> |
|
#include <fcntl.h> |
|
|
#include "sha1.h" |
#include "sha1.h" |
|
|
void |
void |
Usage(char *cmd) |
Usage(char *cmd) |
{ |
{ |
printf("Usage: %s file\n", cmd); |
printf("Usage: %s [-f][-s] file\n", cmd); |
exit (-1); |
exit (-1); |
} |
} |
|
|
Line 22 main(int argc, char *argv[]) |
|
Line 24 main(int argc, char *argv[]) |
|
int ret; |
int ret; |
int i; |
int i; |
unsigned char sha[100]; |
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]); |
Usage(argv[0]); |
|
|
fp = fopen(argv[1], "r"); |
argv += i - 1; |
if (fp == NULL) { |
|
fprintf(stderr, "file not found: %s\n", argv[1]); |
if (flag & SHA) { |
exit (-1); |
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]); |
printf("SHA1(%s)= ", argv[1]); |
for (i = 0; i < 20; i++) { |
for (i = 0; i < 20; i++) { |
printf("%02x", sha[i] & 0xff); |
printf("%02x", sha[i] & 0xff); |
|
} |
|
printf("\n"); |
} |
} |
printf("\n"); |
|
|
|
return (0); |
return (0); |
} |
} |