[dev] [sbase] [patch v4] Add md5sum

2013-07-04 Thread sin
Hi,

This is using nsz's stripped down md5 implementation
based on rfc1321 and libtomcrypt.

I will send a patch incrementally at some point
to implement the -c option.

Thanks,
stateless
>From ac8a2f508ca8cc5e66ee8b81b0ff4d16196e1dee Mon Sep 17 00:00:00 2001
From: stateless 
Date: Wed, 19 Jun 2013 09:54:52 +0100
Subject: [PATCH v4] Add md5sum

No support for -c at the moment.
---
 Makefile   |   2 +
 md5.h  |  18 
 md5sum.1   |   8 
 md5sum.c   |  67 +
 util/md5.c | 139 +
 5 files changed, 234 insertions(+)
 create mode 100644 md5.h
 create mode 100644 md5sum.1
 create mode 100644 md5sum.c
 create mode 100644 util/md5.c

diff --git a/Makefile b/Makefile
index 26c831f..a54f902 100644
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ LIB = \
util/estrtol.o   \
util/fnck.o  \
util/getlines.o  \
+   util/md5.o   \
util/putword.o   \
util/recurse.o   \
util/rm.o
@@ -45,6 +46,7 @@ SRC = \
ln.c   \
ls.c   \
mc.c   \
+   md5sum.c   \
mkdir.c\
mkfifo.c   \
mknod.c\
diff --git a/md5.h b/md5.h
new file mode 100644
index 000..0895d3e
--- /dev/null
+++ b/md5.h
@@ -0,0 +1,18 @@
+/* public domain md5 implementation based on rfc1321 and libtomcrypt */
+
+struct md5 {
+   uint64_t len;/* processed message length */
+   uint32_t h[4];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { MD5_DIGEST_LENGTH = 16 };
+
+/* reset state */
+void md5_init(struct md5 *s);
+/* process message */
+void md5_update(struct md5 *s, 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]);
diff --git a/md5sum.1 b/md5sum.1
new file mode 100644
index 000..93ecb2f
--- /dev/null
+++ b/md5sum.1
@@ -0,0 +1,8 @@
+.TH MD5SUM 1 sbase\-VERSION
+.SH NAME
+md5sum \- compute MD5 message digest
+.SH SYNOPSIS
+.B md5sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print MD5 (128-bit) checksums. With no file, read standard input.
diff --git a/md5sum.c b/md5sum.c
new file mode 100644
index 000..722416f
--- /dev/null
+++ b/md5sum.c
@@ -0,0 +1,67 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+#include "md5.h"
+
+static void md5sum(int fd, const char *f);
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   int fd;
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   md5sum(STDIN_FILENO, "");
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fd = open(*argv, O_RDONLY)) < 0)
+   eprintf("open %s:", *argv);
+   md5sum(fd, *argv);
+   close(fd);
+   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);
+}
diff --git a/util/md5.c b/util/md5.c
new file mode 100644
index 000..0d8353d
--- /dev/null
+++ b/util/md5.c
@@ -0,0 +1,139 @@
+/* public domain md5 implementation based on rfc1321 and libtomcrypt */
+#include 
+#include 
+#include "../md5.h"
+
+static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); }
+#define F(x,y,z) (z ^ (x & (y ^ z)))
+#define G(x,y,z) (y ^ (z & (y ^ x)))
+#define H(x,y,z) (x ^ y ^ z)
+#define I(x,y,z) (y ^ (x | ~z))
+#define FF(a,b,c,d,w,s,t) a += F(b,c,d) + w + t; a = rol(a,s) + b
+#define GG(a,b,c,d,w,s,t) a += G(b,c,d) + w + t; a = rol(a,s) + b
+#define HH(a,b,c,d,w,s,t) a += H(b,c,d) + w + t; a = rol(a,s) + b
+#define II(a,b,c,d,w,s,t) a += I(b,c,d) + w + t; a = rol(a,s) + b
+
+static const uint32_t tab[64] = {
+   0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 
0xa8304613, 0xfd469501,
+   0x698098d8, 0x8b44f7af, 0x5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 
0xa679438e, 0x49b40821,
+   0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 
0xd8a1e681, 0xe7d3fbc8,
+   0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 

[dev] [sbase] [patch] Add sha1sum

2013-07-04 Thread sin
Hi,

Added sha1sum(1) based on nsz's stripped down version
of libtomcrypt from http://port70.net/~nsz/crypt/.

Thanks,
stateless
>From a1714a24098fe363fc244f28e07ab0cd0814de2e Mon Sep 17 00:00:00 2001
From: stateless 
Date: Thu, 4 Jul 2013 12:14:14 +0100
Subject: [PATCH] Add sha1sum

No support for -c at the moment.
---
 Makefile|   4 +-
 sha1.h  |  18 
 sha1sum.1   |   8 
 sha1sum.c   |  67 ++
 util/sha1.c | 134 
 5 files changed, 230 insertions(+), 1 deletion(-)
 create mode 100644 sha1.h
 create mode 100644 sha1sum.1
 create mode 100644 sha1sum.c
 create mode 100644 util/sha1.c

diff --git a/Makefile b/Makefile
index a54f902..f825ec4 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,8 @@ LIB = \
util/md5.o   \
util/putword.o   \
util/recurse.o   \
-   util/rm.o
+   util/rm.o\
+   util/sha1.o
 
 SRC = \
basename.c \
@@ -76,6 +77,7 @@ SRC = \
uniq.c \
unlink.c   \
seq.c  \
+   sha1sum.c  \
wc.c   \
who.c  \
yes.c
diff --git a/sha1.h b/sha1.h
new file mode 100644
index 000..e11f49e
--- /dev/null
+++ b/sha1.h
@@ -0,0 +1,18 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+
+struct sha1 {
+   uint64_t len;/* processed message length */
+   uint32_t h[5];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { SHA1_DIGEST_LENGTH = 20 };
+
+/* reset state */
+void sha1_init(struct sha1 *s);
+/* process message */
+void sha1_update(struct sha1 *s, 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]);
diff --git a/sha1sum.1 b/sha1sum.1
new file mode 100644
index 000..10ac3c5
--- /dev/null
+++ b/sha1sum.1
@@ -0,0 +1,8 @@
+.TH SHA1SUM 1 sbase\-VERSION
+.SH NAME
+sha1sum \- compute SHA1 message digest
+.SH SYNOPSIS
+.B sha1sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print SH1 (160-bit) checksums. With no file, read standard input.
diff --git a/sha1sum.c b/sha1sum.c
new file mode 100644
index 000..e38346a
--- /dev/null
+++ b/sha1sum.c
@@ -0,0 +1,67 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+#include "sha1.h"
+
+static void sha1sum(int fd, const char *f);
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   int fd;
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   sha1sum(STDIN_FILENO, "");
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fd = open(*argv, O_RDONLY)) < 0)
+   eprintf("open %s:", *argv);
+   sha1sum(fd, *argv);
+   close(fd);
+   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);
+}
diff --git a/util/sha1.c b/util/sha1.c
new file mode 100644
index 000..f828395
--- /dev/null
+++ b/util/sha1.c
@@ -0,0 +1,134 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+#include 
+#include 
+#include "../sha1.h"
+
+static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); }
+#define F0(b,c,d) (d ^ (b & (c ^ d)))
+#define F1(b,c,d) (b ^ c ^ d)
+#define F2(b,c,d) ((b & c) | (d & (b | c)))
+#define F3(b,c,d) (b ^ c ^ d)
+#define G0(a,b,c,d,e,i) e += rol(a,5)+F0(b,c,d)+W[i]+0x5A827999; b = rol(b,30)
+#define G1(a,b,c,d,e,i) e += rol(a,5)+F1(b,c,d)+W[i]+0x6ED9EBA1; b = rol(b,30)
+#define G2(a,b,c,d,e,i) e += rol(a,5)+F2(b,c,d)+W[i]+0x8F1BBCDC; b = rol(b,30)
+#define G3(a,b,c,d,e,i) e += rol(a,5)+F3(b,c,d)+W[i]+0xCA62C1D6; b = rol(b,30)
+
+static void processblock(struct sha1 *s, const uint8_t *buf)
+{
+   uint32_t W[80], a, b, c, d, e;
+   int i;
+
+   for (i = 0; i < 16; i++) {
+   W[i] = (uint32_t)buf[4*i]<<24;
+   W[i] |= (uint32_t)buf[4*i+1]<<16;
+   W[i] |= (uint32_t)buf[4*i+2]<<8;
+   W[i] |= buf[4*i+3];
+   }
+   for (; i < 80; i++)
+  

[dev] Parentheses

2013-07-04 Thread Bjartur Thorlacius

Galos, David:

The types array stood out to me as a bit of a code smell.
After pondering over it for a couple of minutes, I realized
that you could just do this:

type = argv[1][0] == 'b' ? S_IFBLK : S_IFCHR;

type = ((argv[1][0] == 'b') ? S_IFBLK : S_IFCHR)



Re: [dev] [sbase] [patch] Add sha1sum

2013-07-04 Thread Robert Ransom
On 7/4/13, sin  wrote:
> Hi,
>
> Added sha1sum(1) based on nsz's stripped down version
> of libtomcrypt from http://port70.net/~nsz/crypt/.

It's "SHA-1", not "SH1".

sha1sum.c is very similar to md5sum.c; ideally, more of the common
code between those programs would be in a library routine.


Robert Ransom



Re: [dev] [sbase] [patch] Add sha1sum

2013-07-04 Thread sin
On Thu, Jul 04, 2013 at 12:34:14PM +, Robert Ransom wrote:
> On 7/4/13, sin  wrote:
> > Hi,
> >
> > Added sha1sum(1) based on nsz's stripped down version
> > of libtomcrypt from http://port70.net/~nsz/crypt/.
> 
> It's "SHA-1", not "SH1".

Oops, that's a typo - thanks for spotting.  Will re-send.

> sha1sum.c is very similar to md5sum.c; ideally, more of the common
> code between those programs would be in a library routine.

Yeah they are very similar, however, the code is very simple and
there is not going to be more programs in sbase that will use that code
as far as I know.  If that is indeed the case then I can factor out
the common bits incrementally when we need it.

Both the md5.[ch] and sha1.[ch] code is very simple to use.

Thanks,
stateless



[dev] [sbase] [patch v2] Add sha1sum

2013-07-04 Thread sin
Hi, 

Added sha1sum(1) based on nsz's stripped down version 
of libtomcrypt from http://port70.net/~nsz/crypt/.

No support for -c at the moment.

Thanks,
stateless
>From 71ffbed273a508345c5f4771c50892a4fcf4b057 Mon Sep 17 00:00:00 2001
From: stateless 
Date: Thu, 4 Jul 2013 12:14:14 +0100
Subject: [PATCH] Add sha1sum

No support for -c at the moment.
---
 Makefile|   4 +-
 sha1.h  |  18 
 sha1sum.1   |   8 
 sha1sum.c   |  67 ++
 util/sha1.c | 134 
 5 files changed, 230 insertions(+), 1 deletion(-)
 create mode 100644 sha1.h
 create mode 100644 sha1sum.1
 create mode 100644 sha1sum.c
 create mode 100644 util/sha1.c

diff --git a/Makefile b/Makefile
index a54f902..f825ec4 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,8 @@ LIB = \
util/md5.o   \
util/putword.o   \
util/recurse.o   \
-   util/rm.o
+   util/rm.o\
+   util/sha1.o
 
 SRC = \
basename.c \
@@ -76,6 +77,7 @@ SRC = \
uniq.c \
unlink.c   \
seq.c  \
+   sha1sum.c  \
wc.c   \
who.c  \
yes.c
diff --git a/sha1.h b/sha1.h
new file mode 100644
index 000..e11f49e
--- /dev/null
+++ b/sha1.h
@@ -0,0 +1,18 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+
+struct sha1 {
+   uint64_t len;/* processed message length */
+   uint32_t h[5];   /* hash state */
+   uint8_t buf[64]; /* message block buffer */
+};
+
+enum { SHA1_DIGEST_LENGTH = 20 };
+
+/* reset state */
+void sha1_init(struct sha1 *s);
+/* process message */
+void sha1_update(struct sha1 *s, 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]);
diff --git a/sha1sum.1 b/sha1sum.1
new file mode 100644
index 000..8f8a2df
--- /dev/null
+++ b/sha1sum.1
@@ -0,0 +1,8 @@
+.TH SHA1SUM 1 sbase\-VERSION
+.SH NAME
+sha1sum \- compute SHA-1 message digest
+.SH SYNOPSIS
+.B sha1sum
+.RI [ file ...]
+.SH DESCRIPTION
+Print SHA-1 (160-bit) checksums. With no file, read standard input.
diff --git a/sha1sum.c b/sha1sum.c
new file mode 100644
index 000..e38346a
--- /dev/null
+++ b/sha1sum.c
@@ -0,0 +1,67 @@
+/* See LICENSE file for copyright and license details. */
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include "util.h"
+#include "sha1.h"
+
+static void sha1sum(int fd, const char *f);
+
+static void
+usage(void)
+{
+   eprintf("usage: %s [file...]\n", argv0);
+}
+
+int
+main(int argc, char *argv[])
+{
+   int fd;
+
+   ARGBEGIN {
+   default:
+   usage();
+   } ARGEND;
+
+   if (argc == 0) {
+   sha1sum(STDIN_FILENO, "");
+   } else {
+   for (; argc > 0; argc--) {
+   if ((fd = open(*argv, O_RDONLY)) < 0)
+   eprintf("open %s:", *argv);
+   sha1sum(fd, *argv);
+   close(fd);
+   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);
+}
diff --git a/util/sha1.c b/util/sha1.c
new file mode 100644
index 000..f828395
--- /dev/null
+++ b/util/sha1.c
@@ -0,0 +1,134 @@
+/* public domain sha1 implementation based on rfc3174 and libtomcrypt */
+#include 
+#include 
+#include "../sha1.h"
+
+static uint32_t rol(uint32_t n, int k) { return (n << k) | (n >> (32-k)); }
+#define F0(b,c,d) (d ^ (b & (c ^ d)))
+#define F1(b,c,d) (b ^ c ^ d)
+#define F2(b,c,d) ((b & c) | (d & (b | c)))
+#define F3(b,c,d) (b ^ c ^ d)
+#define G0(a,b,c,d,e,i) e += rol(a,5)+F0(b,c,d)+W[i]+0x5A827999; b = rol(b,30)
+#define G1(a,b,c,d,e,i) e += rol(a,5)+F1(b,c,d)+W[i]+0x6ED9EBA1; b = rol(b,30)
+#define G2(a,b,c,d,e,i) e += rol(a,5)+F2(b,c,d)+W[i]+0x8F1BBCDC; b = rol(b,30)
+#define G3(a,b,c,d,e,i) e += rol(a,5)+F3(b,c,d)+W[i]+0xCA62C1D6; b = rol(b,30)
+
+static void processblock(struct sha1 *s, const uint8_t *buf)
+{
+   uint32_t W[80], a, b, c, d, e;
+   int i;
+
+   for (i = 0; i < 16; i++) {
+   W[i] = (uint32_t)buf[4*i]<<24;
+   W[i] |= (uint32_t)buf[4*i+1]<<16;
+   W[i] |= (uint32_t)buf[4*i+2]<<8;
+   W[i] |= buf[4*i+3];
+ 

Re: [dev] Parentheses

2013-07-04 Thread Truls Becken
On 2013-07-04, at 14:12, Bjartur Thorlacius wrote:

>> type = argv[1][0] == 'b' ? S_IFBLK : S_IFCHR;
> type = ((argv[1][0] == 'b') ? S_IFBLK : S_IFCHR)

Yes, David did the commit with parens around the test. Not around the elvis 
operator, though.

-Truls