|
@ -1,4 +1,4 @@ |
|
|
/* $OpenBSD: helper.c,v 1.1 2004/04/26 19:38:12 millert Exp $ */ |
|
|
|
|
|
|
|
|
/* $OpenBSD: helper.c,v 1.2 2004/04/29 02:39:32 millert Exp $ */ |
|
|
|
|
|
|
|
|
/* |
|
|
/* |
|
|
* ---------------------------------------------------------------------------- |
|
|
* ---------------------------------------------------------------------------- |
|
@ -10,7 +10,7 @@ |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint) |
|
|
#if defined(LIBC_SCCS) && !defined(lint) |
|
|
static const char rcsid[] = "$OpenBSD: helper.c,v 1.1 2004/04/26 19:38:12 millert Exp $"; |
|
|
|
|
|
|
|
|
static const char rcsid[] = "$OpenBSD: helper.c,v 1.2 2004/04/29 02:39:32 millert Exp $"; |
|
|
#endif /* LIBC_SCCS and not lint */ |
|
|
#endif /* LIBC_SCCS and not lint */ |
|
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
#include <sys/types.h> |
|
@ -28,21 +28,21 @@ static const char rcsid[] = "$OpenBSD: helper.c,v 1.1 2004/04/26 19:38:12 miller |
|
|
char * |
|
|
char * |
|
|
HASHEnd(HASH_CTX *ctx, char buf[HASH_DIGEST_STRING_LENGTH]) |
|
|
HASHEnd(HASH_CTX *ctx, char buf[HASH_DIGEST_STRING_LENGTH]) |
|
|
{ |
|
|
{ |
|
|
int i; |
|
|
|
|
|
u_char digest[HASH_DIGEST_LENGTH]; |
|
|
|
|
|
static const char hex[] = "0123456789abcdef"; |
|
|
|
|
|
|
|
|
int i; |
|
|
|
|
|
u_int8_t digest[HASH_DIGEST_LENGTH]; |
|
|
|
|
|
static const char hex[] = "0123456789abcdef"; |
|
|
|
|
|
|
|
|
if (buf == NULL && (buf = malloc(HASH_DIGEST_STRING_LENGTH)) == NULL) |
|
|
|
|
|
return(NULL); |
|
|
|
|
|
|
|
|
if (buf == NULL && (buf = malloc(HASH_DIGEST_STRING_LENGTH)) == NULL) |
|
|
|
|
|
return(NULL); |
|
|
|
|
|
|
|
|
HASHFinal(digest,ctx); |
|
|
|
|
|
for (i = 0; i < HASH_DIGEST_LENGTH; i++) { |
|
|
|
|
|
buf[i + i] = hex[digest[i] >> 4]; |
|
|
|
|
|
buf[i + i + 1] = hex[digest[i] & 0x0f]; |
|
|
|
|
|
} |
|
|
|
|
|
buf[i + i] = '\0'; |
|
|
|
|
|
memset(digest, 0, sizeof(digest)); |
|
|
|
|
|
return(buf); |
|
|
|
|
|
|
|
|
HASHFinal(digest,ctx); |
|
|
|
|
|
for (i = 0; i < HASH_DIGEST_LENGTH; i++) { |
|
|
|
|
|
buf[i + i] = hex[digest[i] >> 4]; |
|
|
|
|
|
buf[i + i + 1] = hex[digest[i] & 0x0f]; |
|
|
|
|
|
} |
|
|
|
|
|
buf[i + i] = '\0'; |
|
|
|
|
|
memset(digest, 0, sizeof(digest)); |
|
|
|
|
|
return (buf); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
char * |
|
|
char * |
|
@ -50,28 +50,28 @@ HASHFile(char *filename, char buf[HASH_DIGEST_STRING_LENGTH]) |
|
|
{ |
|
|
{ |
|
|
u_char buffer[BUFSIZ]; |
|
|
u_char buffer[BUFSIZ]; |
|
|
HASH_CTX ctx; |
|
|
HASH_CTX ctx; |
|
|
int fd, num, oerrno; |
|
|
|
|
|
|
|
|
int fd, num, save_errno; |
|
|
|
|
|
|
|
|
HASHInit(&ctx); |
|
|
HASHInit(&ctx); |
|
|
|
|
|
|
|
|
if ((fd = open(filename, O_RDONLY)) < 0) |
|
|
if ((fd = open(filename, O_RDONLY)) < 0) |
|
|
return(0); |
|
|
|
|
|
|
|
|
return(NULL); |
|
|
|
|
|
|
|
|
while ((num = read(fd, buffer, sizeof(buffer))) > 0) |
|
|
while ((num = read(fd, buffer, sizeof(buffer))) > 0) |
|
|
HASHUpdate(&ctx, buffer, num); |
|
|
|
|
|
|
|
|
HASHUpdate(&ctx, buffer, num); |
|
|
|
|
|
|
|
|
oerrno = errno; |
|
|
|
|
|
|
|
|
save_errno = errno; |
|
|
close(fd); |
|
|
close(fd); |
|
|
errno = oerrno; |
|
|
|
|
|
return(num < 0 ? 0 : HASHEnd(&ctx, buf)); |
|
|
|
|
|
|
|
|
errno = save_errno; |
|
|
|
|
|
return (num < 0 ? NULL : HASHEnd(&ctx, buf)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
char * |
|
|
char * |
|
|
HASHData(const u_char *data, size_t len, char buf[HASH_DIGEST_STRING_LENGTH]) |
|
|
HASHData(const u_char *data, size_t len, char buf[HASH_DIGEST_STRING_LENGTH]) |
|
|
{ |
|
|
{ |
|
|
HASH_CTX ctx; |
|
|
|
|
|
|
|
|
HASH_CTX ctx; |
|
|
|
|
|
|
|
|
HASHInit(&ctx); |
|
|
|
|
|
HASHUpdate(&ctx, data, len); |
|
|
|
|
|
return(HASHEnd(&ctx, buf)); |
|
|
|
|
|
|
|
|
HASHInit(&ctx); |
|
|
|
|
|
HASHUpdate(&ctx, data, len); |
|
|
|
|
|
return (HASHEnd(&ctx, buf)); |
|
|
} |
|
|
} |