[hackers] [sbase] Add cryptmain() and factor out the code from the crypt tools || sin
commit 573d1954b28d81871d18683bdad14e02918c4f83
Author: sin <sin_AT_2f30.org>
Date: Mon Sep 2 11:17:55 2013 +0100
Add cryptmain() and factor out the code from the crypt tools
diff --git a/crypt.h b/crypt.h
index 9eedabf..f92187f 100644
--- a/crypt.h
+++ b/crypt.h
_AT_@ -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);
diff --git a/md5sum.c b/md5sum.c
index 7618663..b51ca4e 100644
--- a/md5sum.c
+++ b/md5sum.c
_AT_@ -23,7 +23,6 @@ usage(void)
int
main(int argc, char *argv[])
{
- FILE *fp;
uint8_t md[MD5_DIGEST_LENGTH];
ARGBEGIN {
_AT_@ -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));
}
diff --git a/sha1sum.c b/sha1sum.c
index 6d79f15..14bda3f 100644
--- a/sha1sum.c
+++ b/sha1sum.c
_AT_@ -23,7 +23,6 @@ usage(void)
int
main(int argc, char *argv[])
{
- FILE *fp;
uint8_t md[SHA1_DIGEST_LENGTH];
ARGBEGIN {
_AT_@ -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));
}
diff --git a/sha256sum.c b/sha256sum.c
index 5dfc6a5..957182e 100644
--- a/sha256sum.c
+++ b/sha256sum.c
_AT_@ -23,7 +23,6 @@ usage(void)
int
main(int argc, char *argv[])
{
- FILE *fp;
uint8_t md[SHA256_DIGEST_LENGTH];
ARGBEGIN {
_AT_@ -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));
}
diff --git a/sha512sum.c b/sha512sum.c
index edb1cd3..79bec11 100644
--- a/sha512sum.c
+++ b/sha512sum.c
_AT_@ -23,7 +23,6 @@ usage(void)
int
main(int argc, char *argv[])
{
- FILE *fp;
uint8_t md[SHA512_DIGEST_LENGTH];
ARGBEGIN {
_AT_@ -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));
}
diff --git a/util/crypt.c b/util/crypt.c
index 2f05275..326b028 100644
--- a/util/crypt.c
+++ b/util/crypt.c
_AT_@ -5,6 +5,28 @@
#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)
{
Received on Sat Oct 05 2013 - 16:29:27 CEST
This archive was generated by hypermail 2.3.0
: Sat Oct 05 2013 - 16:36:33 CEST