于是我找到了这个名为 libsha1 的库,并编写了一个简单的程序来测试它。但是,当我用 编译它时gcc -lsha1 -o sha1sum sha1sum.c
,它给了我以下错误:
sha1sum.c: In function 'main':
sha1sum.c:30: error: 'byte' undeclared (first use in this function)
sha1sum.c:30: error: (Each undeclared identifier is reported only once
sha1sum.c:30: error: for each function it appears in.)
我究竟做错了什么?这是代码:
#include <stdio.h>
#include <string.h>
#include <libsha1.h>
void printByte(unsigned char);
int main(int argc, char *argv[])
{
unsigned char hash[20];
int i;
#ifndef LIBSHA1_NO_CTX
sha1_ctx ctx;
#endif
if (argc != 2)
{
fprintf(stderr, "Usage: %s string\n", argv[0]);
return 1;
}
#ifdef LIBSHA1_NO_CTX
sha1(hash, argv[1], strlen(argv[1]));
#else
sha1_begin(&ctx);
sha1_hash(argv[1], strlen(argv[1]), &ctx);
sha1_end(hash, &ctx);
#endif
for (i=0; i<5; i++) printByte(byte); printf("\n");
return 0;
}
void printByte(unsigned char byte)
{
const char *digits = "0123456789abcdef";
printf("%c%c", digits[byte / 16], digits[byte % 16]);
}