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:
parent
48e6870bb7
commit
9ac01f59be
3
Makefile
3
Makefile
@ -3,13 +3,14 @@ include config.mk
|
||||
.POSIX:
|
||||
.SUFFIXES: .c .o
|
||||
|
||||
HDR = fs.h text.h util.h arg.h
|
||||
HDR = crypt.h fs.h text.h md5.h sha1.h util.h arg.h
|
||||
LIB = \
|
||||
util/afgets.o \
|
||||
util/agetcwd.o \
|
||||
util/apathmax.o \
|
||||
util/concat.o \
|
||||
util/cp.o \
|
||||
util/crypt.o \
|
||||
util/enmasse.o \
|
||||
util/eprintf.o \
|
||||
util/estrtol.o \
|
||||
|
10
crypt.h
Normal file
10
crypt.h
Normal file
@ -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);
|
6
md5.h
6
md5.h
@ -9,10 +9,10 @@ struct md5 {
|
||||
enum { MD5_DIGEST_LENGTH = 16 };
|
||||
|
||||
/* reset state */
|
||||
void md5_init(struct md5 *s);
|
||||
void md5_init(void *ctx);
|
||||
/* process message */
|
||||
void md5_update(struct md5 *s, const void *m, unsigned long len);
|
||||
void md5_update(void *ctx, const void *m, unsigned long len);
|
||||
/* get message digest */
|
||||
/* state is ruined after sum, keep a copy if multiple sum is needed */
|
||||
/* part of the message might be left in s, zero it if secrecy is needed */
|
||||
void md5_sum(struct md5 *s, uint8_t md[MD5_DIGEST_LENGTH]);
|
||||
void md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH]);
|
||||
|
52
md5sum.c
52
md5sum.c
@ -1,15 +1,18 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "util.h"
|
||||
#include "crypt.h"
|
||||
#include "md5.h"
|
||||
|
||||
static void md5sum(int fd, const char *f);
|
||||
struct md5 s;
|
||||
struct crypt_ops md5_ops = {
|
||||
md5_init,
|
||||
md5_update,
|
||||
md5_sum,
|
||||
&s,
|
||||
};
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
@ -20,7 +23,8 @@ usage(void)
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
FILE *fp;
|
||||
uint8_t md[MD5_DIGEST_LENGTH];
|
||||
|
||||
ARGBEGIN {
|
||||
default:
|
||||
@ -28,40 +32,18 @@ main(int argc, char *argv[])
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0) {
|
||||
md5sum(STDIN_FILENO, "<stdin>");
|
||||
cryptsum(&md5_ops, stdin, "<stdin>", md);
|
||||
mdprint(md, "<stdin>", sizeof(md));
|
||||
} else {
|
||||
for (; argc > 0; argc--) {
|
||||
if ((fd = open(*argv, O_RDONLY)) < 0)
|
||||
eprintf("open %s:", *argv);
|
||||
md5sum(fd, *argv);
|
||||
close(fd);
|
||||
if ((fp = fopen(*argv, "r")) == NULL)
|
||||
eprintf("fopen %s:", *argv);
|
||||
cryptsum(&md5_ops, fp, *argv, md);
|
||||
mdprint(md, *argv, sizeof(md));
|
||||
fclose(fp);
|
||||
argv++;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
md5sum(int fd, const char *f)
|
||||
{
|
||||
unsigned char buf[BUFSIZ];
|
||||
unsigned char digest[MD5_DIGEST_LENGTH];
|
||||
struct md5 s;
|
||||
ssize_t n;
|
||||
int i;
|
||||
|
||||
md5_init(&s);
|
||||
while ((n = read(fd, buf, sizeof buf)) > 0)
|
||||
md5_update(&s, buf, n);
|
||||
if (n < 0) {
|
||||
eprintf("%s: read error:", f);
|
||||
return;
|
||||
}
|
||||
|
||||
md5_sum(&s, digest);
|
||||
|
||||
for (i = 0; i < sizeof(digest); i++)
|
||||
printf("%02x", digest[i]);
|
||||
printf(" %s\n", f);
|
||||
}
|
||||
|
6
sha1.h
6
sha1.h
@ -9,10 +9,10 @@ struct sha1 {
|
||||
enum { SHA1_DIGEST_LENGTH = 20 };
|
||||
|
||||
/* reset state */
|
||||
void sha1_init(struct sha1 *s);
|
||||
void sha1_init(void *ctx);
|
||||
/* process message */
|
||||
void sha1_update(struct sha1 *s, const void *m, unsigned long len);
|
||||
void sha1_update(void *ctx, const void *m, unsigned long len);
|
||||
/* get message digest */
|
||||
/* state is ruined after sum, keep a copy if multiple sum is needed */
|
||||
/* part of the message might be left in s, zero it if secrecy is needed */
|
||||
void sha1_sum(struct sha1 *s, uint8_t md[SHA1_DIGEST_LENGTH]);
|
||||
void sha1_sum(void *ctx, uint8_t md[SHA1_DIGEST_LENGTH]);
|
||||
|
52
sha1sum.c
52
sha1sum.c
@ -1,15 +1,18 @@
|
||||
/* See LICENSE file for copyright and license details. */
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "util.h"
|
||||
#include "crypt.h"
|
||||
#include "sha1.h"
|
||||
|
||||
static void sha1sum(int fd, const char *f);
|
||||
struct sha1 s;
|
||||
struct crypt_ops sha1_ops = {
|
||||
sha1_init,
|
||||
sha1_update,
|
||||
sha1_sum,
|
||||
&s,
|
||||
};
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
@ -20,7 +23,8 @@ usage(void)
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int fd;
|
||||
FILE *fp;
|
||||
uint8_t md[SHA1_DIGEST_LENGTH];
|
||||
|
||||
ARGBEGIN {
|
||||
default:
|
||||
@ -28,40 +32,18 @@ main(int argc, char *argv[])
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0) {
|
||||
sha1sum(STDIN_FILENO, "<stdin>");
|
||||
cryptsum(&sha1_ops, stdin, "<stdin>", md);
|
||||
mdprint(md, "<stdin>", sizeof(md));
|
||||
} else {
|
||||
for (; argc > 0; argc--) {
|
||||
if ((fd = open(*argv, O_RDONLY)) < 0)
|
||||
eprintf("open %s:", *argv);
|
||||
sha1sum(fd, *argv);
|
||||
close(fd);
|
||||
if ((fp = fopen(*argv, "r")) == NULL)
|
||||
eprintf("fopen %s:", *argv);
|
||||
cryptsum(&sha1_ops, fp, *argv, md);
|
||||
mdprint(md, *argv, sizeof(md));
|
||||
fclose(fp);
|
||||
argv++;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
sha1sum(int fd, const char *f)
|
||||
{
|
||||
unsigned char buf[BUFSIZ];
|
||||
unsigned char digest[SHA1_DIGEST_LENGTH];
|
||||
struct sha1 s;
|
||||
ssize_t n;
|
||||
int i;
|
||||
|
||||
sha1_init(&s);
|
||||
while ((n = read(fd, buf, sizeof buf)) > 0)
|
||||
sha1_update(&s, buf, n);
|
||||
if (n < 0) {
|
||||
eprintf("%s: read error:", f);
|
||||
return;
|
||||
}
|
||||
|
||||
sha1_sum(&s, digest);
|
||||
|
||||
for (i = 0; i < sizeof(digest); i++)
|
||||
printf("%02x", digest[i]);
|
||||
printf(" %s\n", f);
|
||||
}
|
||||
|
33
util/crypt.c
Normal file
33
util/crypt.c
Normal 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);
|
||||
}
|
@ -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;
|
||||
|
||||
|
10
util/sha1.c
10
util/sha1.c
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user