Add crypt.[ch] and update md5sum and sha1sum

Factor out the code from md5sum and sha1sum into a util function.

Use FILE * instead of a file descriptor.  This will make it a bit
easier/more consistent when we implement support for the -c option.
This commit is contained in:
sin
2013-07-07 15:29:45 +01:00
committed by David Galos
parent 48e6870bb7
commit 9ac01f59be
9 changed files with 98 additions and 83 deletions

33
util/crypt.c Normal file
View File

@@ -0,0 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "../util.h"
#include "../crypt.h"
int
cryptsum(struct crypt_ops *ops, FILE *fp, const char *f,
uint8_t *md)
{
unsigned char buf[BUFSIZ];
size_t n;
ops->init(ops->s);
while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
ops->update(ops->s, buf, n);
if (ferror(fp)) {
eprintf("%s: read error:", f);
return 1;
}
ops->sum(ops->s, md);
return 0;
}
void
mdprint(const uint8_t *md, const char *f, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
printf("%02x", md[i]);
printf(" %s\n", f);
}

View File

@@ -95,8 +95,9 @@ static void pad(struct md5 *s)
processblock(s, s->buf);
}
void md5_init(struct md5 *s)
void md5_init(void *ctx)
{
struct md5 *s = ctx;
s->len = 0;
s->h[0] = 0x67452301;
s->h[1] = 0xefcdab89;
@@ -104,8 +105,9 @@ void md5_init(struct md5 *s)
s->h[3] = 0x10325476;
}
void md5_sum(struct md5 *s, uint8_t md[16])
void md5_sum(void *ctx, uint8_t md[16])
{
struct md5 *s = ctx;
int i;
pad(s);
@@ -117,8 +119,9 @@ void md5_sum(struct md5 *s, uint8_t md[16])
}
}
void md5_update(struct md5 *s, const void *m, unsigned long len)
void md5_update(void *ctx, const void *m, unsigned long len)
{
struct md5 *s = ctx;
const uint8_t *p = m;
unsigned r = s->len % 64;

View File

@@ -89,8 +89,10 @@ static void pad(struct sha1 *s)
processblock(s, s->buf);
}
void sha1_init(struct sha1 *s)
void sha1_init(void *ctx)
{
struct sha1 *s = ctx;
s->len = 0;
s->h[0] = 0x67452301;
s->h[1] = 0xEFCDAB89;
@@ -99,8 +101,9 @@ void sha1_init(struct sha1 *s)
s->h[4] = 0xC3D2E1F0;
}
void sha1_sum(struct sha1 *s, uint8_t md[20])
void sha1_sum(void *ctx, uint8_t md[20])
{
struct sha1 *s = ctx;
int i;
pad(s);
@@ -112,8 +115,9 @@ void sha1_sum(struct sha1 *s, uint8_t md[20])
}
}
void sha1_update(struct sha1 *s, const void *m, unsigned long len)
void sha1_update(void *ctx, const void *m, unsigned long len)
{
struct sha1 *s = ctx;
const uint8_t *p = m;
unsigned r = s->len % 64;