Add cryptmain() and factor out the code from the crypt tools
This commit is contained in:
parent
4d014e66fd
commit
573d1954b2
2
crypt.h
2
crypt.h
|
@ -5,6 +5,8 @@ struct crypt_ops {
|
|||
void *s;
|
||||
};
|
||||
|
||||
int cryptmain(int argc, char *argv[],
|
||||
struct crypt_ops *ops, uint8_t *md, size_t sz);
|
||||
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);
|
||||
|
|
17
md5sum.c
17
md5sum.c
|
@ -23,7 +23,6 @@ usage(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fp;
|
||||
uint8_t md[MD5_DIGEST_LENGTH];
|
||||
|
||||
ARGBEGIN {
|
||||
|
@ -33,19 +32,5 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0) {
|
||||
cryptsum(&md5_ops, stdin, "<stdin>", md);
|
||||
mdprint(md, "<stdin>", sizeof(md));
|
||||
} else {
|
||||
for (; argc > 0; argc--) {
|
||||
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;
|
||||
return cryptmain(argc, argv, &md5_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
17
sha1sum.c
17
sha1sum.c
|
@ -23,7 +23,6 @@ usage(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fp;
|
||||
uint8_t md[SHA1_DIGEST_LENGTH];
|
||||
|
||||
ARGBEGIN {
|
||||
|
@ -33,19 +32,5 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0) {
|
||||
cryptsum(&sha1_ops, stdin, "<stdin>", md);
|
||||
mdprint(md, "<stdin>", sizeof(md));
|
||||
} else {
|
||||
for (; argc > 0; argc--) {
|
||||
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;
|
||||
return cryptmain(argc, argv, &sha1_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
17
sha256sum.c
17
sha256sum.c
|
@ -23,7 +23,6 @@ usage(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fp;
|
||||
uint8_t md[SHA256_DIGEST_LENGTH];
|
||||
|
||||
ARGBEGIN {
|
||||
|
@ -33,19 +32,5 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0) {
|
||||
cryptsum(&sha256_ops, stdin, "<stdin>", md);
|
||||
mdprint(md, "<stdin>", sizeof(md));
|
||||
} else {
|
||||
for (; argc > 0; argc--) {
|
||||
if ((fp = fopen(*argv, "r")) == NULL)
|
||||
eprintf("fopen %s:", *argv);
|
||||
cryptsum(&sha256_ops, fp, *argv, md);
|
||||
mdprint(md, *argv, sizeof(md));
|
||||
fclose(fp);
|
||||
argv++;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return cryptmain(argc, argv, &sha256_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
17
sha512sum.c
17
sha512sum.c
|
@ -23,7 +23,6 @@ usage(void)
|
|||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fp;
|
||||
uint8_t md[SHA512_DIGEST_LENGTH];
|
||||
|
||||
ARGBEGIN {
|
||||
|
@ -33,19 +32,5 @@ main(int argc, char *argv[])
|
|||
usage();
|
||||
} ARGEND;
|
||||
|
||||
if (argc == 0) {
|
||||
cryptsum(&sha512_ops, stdin, "<stdin>", md);
|
||||
mdprint(md, "<stdin>", sizeof(md));
|
||||
} else {
|
||||
for (; argc > 0; argc--) {
|
||||
if ((fp = fopen(*argv, "r")) == NULL)
|
||||
eprintf("fopen %s:", *argv);
|
||||
cryptsum(&sha512_ops, fp, *argv, md);
|
||||
mdprint(md, *argv, sizeof(md));
|
||||
fclose(fp);
|
||||
argv++;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return cryptmain(argc, argv, &sha512_ops, md, sizeof(md));
|
||||
}
|
||||
|
|
22
util/crypt.c
22
util/crypt.c
|
@ -4,6 +4,28 @@
|
|||
#include "../util.h"
|
||||
#include "../crypt.h"
|
||||
|
||||
int
|
||||
cryptmain(int argc, char *argv[],
|
||||
struct crypt_ops *ops, uint8_t *md, size_t sz)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
if (argc == 0) {
|
||||
cryptsum(ops, stdin, "<stdin>", md);
|
||||
mdprint(md, "<stdin>", sz);
|
||||
} else {
|
||||
for (; argc > 0; argc--) {
|
||||
if ((fp = fopen(*argv, "r")) == NULL)
|
||||
eprintf("fopen %s:", *argv);
|
||||
cryptsum(ops, fp, *argv, md);
|
||||
mdprint(md, *argv, sz);
|
||||
fclose(fp);
|
||||
argv++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
cryptsum(struct crypt_ops *ops, FILE *fp, const char *f,
|
||||
uint8_t *md)
|
||||
|
|
Loading…
Reference in New Issue
Block a user