mirror of git://git.suckless.org/sbase
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.master
parent
48e6870bb7
commit
9ac01f59be
@ -0,0 +1,10 @@ |
||||
struct crypt_ops { |
||||
void (*init)(void *); |
||||
void (*update)(void *, const void *, unsigned long); |
||||
void (*sum)(void *, uint8_t *); |
||||
void *s; |
||||
}; |
||||
|
||||
int cryptsum(struct crypt_ops *ops, FILE *fp, const char *f, |
||||
uint8_t *md); |
||||
void mdprint(const uint8_t *md, const char *f, size_t len); |
@ -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); |
||||
} |
Loading…
Reference in new issue