---
Makefile | 1 +
crypt.h | 9 +++++++
md5sum.c | 78 +++++++++++++++++++++++++++++++++++++++---------------------
sha1sum.c | 78 +++++++++++++++++++++++++++++++++++++++---------------------
util/crypt.c | 25 +++++++++++++++++++
5 files changed, 137 insertions(+), 54 deletions(-)
create mode 100644 crypt.h
create mode 100644 util/crypt.c
diff --git a/Makefile b/Makefile
index f825ec4..c58fb86 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -10,6 +10,7 @@ LIB = \
util/apathmax.o \
util/concat.o \
util/cp.o \
+ util/crypt.o \
util/enmasse.o \
util/eprintf.o \
util/estrtol.o \
diff --git a/crypt.h b/crypt.h
new file mode 100644
index 0000000..ad0392e
--- /dev/null
+++ b/crypt.h
_AT_@ -0,0 +1,9 @@
+struct crypt_ops {
+ void (*init)(struct crypt_ops *);
+ void (*update)(struct crypt_ops *, const void *, unsigned long);
+ void (*sum)(struct crypt_ops *);
+ void (*print)(struct crypt_ops *, const char *);
+ void *priv;
+};
+
+int cryptsum(struct crypt_ops *ops, FILE *fp, const char *f);
diff --git a/md5sum.c b/md5sum.c
index 722416f..c66909c 100644
--- a/md5sum.c
+++ b/md5sum.c
_AT_@ -1,15 +1,28 @@
/* 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);
+static void crypt_md5_init(struct crypt_ops *);
+static void crypt_md5_update(struct crypt_ops *, const void *, unsigned long);
+static void crypt_md5_sum(struct crypt_ops *);
+static void crypt_md5_print(struct crypt_ops *, const char *);
+
+struct md5_priv {
+ struct md5 s;
+ unsigned char digest[MD5_DIGEST_LENGTH];
+} md5_priv;
+
+struct crypt_ops md5_ops = {
+ crypt_md5_init,
+ crypt_md5_update,
+ crypt_md5_sum,
+ crypt_md5_print,
+ &md5_priv,
+};
static void
usage(void)
_AT_@ -20,7 +33,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- int fd;
+ FILE *fp;
ARGBEGIN {
default:
_AT_@ -28,13 +41,13 @@ main(int argc, char *argv[])
} ARGEND;
if (argc == 0) {
- md5sum(STDIN_FILENO, "<stdin>");
+ cryptsum(&md5_ops, stdin, "<stdin>");
} 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);
+ fclose(fp);
argv++;
}
}
_AT_@ -43,25 +56,36 @@ main(int argc, char *argv[])
}
static void
-md5sum(int fd, const char *f)
+crypt_md5_init(struct crypt_ops *ops)
{
- unsigned char buf[BUFSIZ];
- unsigned char digest[MD5_DIGEST_LENGTH];
- struct md5 s;
- ssize_t n;
- int i;
+ struct md5_priv *priv = ops->priv;
- 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_init(&priv->s);
+}
+
+static void
+crypt_md5_update(struct crypt_ops *ops, const void *m, unsigned long len)
+{
+ struct md5_priv *priv = ops->priv;
+
+ md5_update(&priv->s, m, len);
+}
+
+static void
+crypt_md5_sum(struct crypt_ops *ops)
+{
+ struct md5_priv *priv = ops->priv;
- md5_sum(&s, digest);
+ md5_sum(&priv->s, priv->digest);
+}
+
+static void
+crypt_md5_print(struct crypt_ops *ops, const char *f)
+{
+ struct md5_priv *priv = ops->priv;
+ int i;
- for (i = 0; i < sizeof(digest); i++)
- printf("%02x", digest[i]);
+ for (i = 0; i < sizeof(priv->digest); i++)
+ printf("%02x", priv->digest[i]);
printf(" %s\n", f);
}
diff --git a/sha1sum.c b/sha1sum.c
index e38346a..558be90 100644
--- a/sha1sum.c
+++ b/sha1sum.c
_AT_@ -1,15 +1,28 @@
/* 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);
+static void crypt_sha1_init(struct crypt_ops *);
+static void crypt_sha1_update(struct crypt_ops *, const void *, unsigned long);
+static void crypt_sha1_sum(struct crypt_ops *);
+static void crypt_sha1_print(struct crypt_ops *, const char *);
+
+struct sha1_priv {
+ struct sha1 s;
+ unsigned char digest[SHA1_DIGEST_LENGTH];
+} sha1_priv;
+
+struct crypt_ops sha1_ops = {
+ crypt_sha1_init,
+ crypt_sha1_update,
+ crypt_sha1_sum,
+ crypt_sha1_print,
+ &sha1_priv,
+};
static void
usage(void)
_AT_@ -20,7 +33,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- int fd;
+ FILE *fp;
ARGBEGIN {
default:
_AT_@ -28,13 +41,13 @@ main(int argc, char *argv[])
} ARGEND;
if (argc == 0) {
- sha1sum(STDIN_FILENO, "<stdin>");
+ cryptsum(&sha1_ops, stdin, "<stdin>");
} 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);
+ fclose(fp);
argv++;
}
}
_AT_@ -43,25 +56,36 @@ main(int argc, char *argv[])
}
static void
-sha1sum(int fd, const char *f)
+crypt_sha1_init(struct crypt_ops *ops)
{
- unsigned char buf[BUFSIZ];
- unsigned char digest[SHA1_DIGEST_LENGTH];
- struct sha1 s;
- ssize_t n;
- int i;
+ struct sha1_priv *priv = ops->priv;
- 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_init(&priv->s);
+}
+
+static void
+crypt_sha1_update(struct crypt_ops *ops, const void *m, unsigned long len)
+{
+ struct sha1_priv *priv = ops->priv;
+
+ sha1_update(&priv->s, m, len);
+}
+
+static void
+crypt_sha1_sum(struct crypt_ops *ops)
+{
+ struct sha1_priv *priv = ops->priv;
- sha1_sum(&s, digest);
+ sha1_sum(&priv->s, priv->digest);
+}
+
+static void
+crypt_sha1_print(struct crypt_ops *ops, const char *f)
+{
+ struct sha1_priv *priv = ops->priv;
+ int i;
- for (i = 0; i < sizeof(digest); i++)
- printf("%02x", digest[i]);
+ for (i = 0; i < sizeof(priv->digest); i++)
+ printf("%02x", priv->digest[i]);
printf(" %s\n", f);
}
diff --git a/util/crypt.c b/util/crypt.c
new file mode 100644
index 0000000..878e18d
--- /dev/null
+++ b/util/crypt.c
_AT_@ -0,0 +1,25 @@
+#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)
+{
+ unsigned char buf[BUFSIZ];
+ size_t n;
+
+ ops->init(ops);
+ while ((n = fread(buf, 1, sizeof(buf), fp)) > 0)
+ ops->update(ops, buf, n);
+ if (ferror(fp)) {
+ eprintf("%s: read error:", f);
+ return 1;
+ }
+ ops->sum(ops);
+ /* Optionally, print the message digest */
+ if (ops->print)
+ ops->print(ops, f);
+ return 0;
+}
--
1.8.3.2
--/04w6evG8XlLl3ft--
Received on Mon Sep 17 2001 - 00:00:00 CEST
This archive was generated by hypermail 2.3.0 : Sun Jul 07 2013 - 17:24:05 CEST