Add md5sum

No support for -c at the moment.
This commit is contained in:
stateless 2013-06-19 09:54:52 +01:00 committed by David Galos
parent 3c5f2b2407
commit 50054a652c
5 changed files with 234 additions and 0 deletions

View File

@ -15,6 +15,7 @@ LIB = \
util/estrtol.o \ util/estrtol.o \
util/fnck.o \ util/fnck.o \
util/getlines.o \ util/getlines.o \
util/md5.o \
util/putword.o \ util/putword.o \
util/recurse.o \ util/recurse.o \
util/rm.o util/rm.o
@ -45,6 +46,7 @@ SRC = \
ln.c \ ln.c \
ls.c \ ls.c \
mc.c \ mc.c \
md5sum.c \
mkdir.c \ mkdir.c \
mkfifo.c \ mkfifo.c \
mknod.c \ mknod.c \

18
md5.h Normal file
View File

@ -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]);

8
md5sum.1 Normal file
View File

@ -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.

67
md5sum.c Normal file
View File

@ -0,0 +1,67 @@
/* 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 "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, "<stdin>");
} 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);
}

139
util/md5.c Normal file
View File

@ -0,0 +1,139 @@
/* public domain md5 implementation based on rfc1321 and libtomcrypt */
#include <stdint.h>
#include <string.h>
#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, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
};
static void processblock(struct md5 *s, const uint8_t *buf)
{
uint32_t i, W[16], a, b, c, d;
for (i = 0; i < 16; i++) {
W[i] = buf[4*i];
W[i] |= (uint32_t)buf[4*i+1]<<8;
W[i] |= (uint32_t)buf[4*i+2]<<16;
W[i] |= (uint32_t)buf[4*i+3]<<24;
}
a = s->h[0];
b = s->h[1];
c = s->h[2];
d = s->h[3];
i = 0;
while (i < 16) {
FF(a,b,c,d, W[i], 7, tab[i]); i++;
FF(d,a,b,c, W[i], 12, tab[i]); i++;
FF(c,d,a,b, W[i], 17, tab[i]); i++;
FF(b,c,d,a, W[i], 22, tab[i]); i++;
}
while (i < 32) {
GG(a,b,c,d, W[(5*i+1)%16], 5, tab[i]); i++;
GG(d,a,b,c, W[(5*i+1)%16], 9, tab[i]); i++;
GG(c,d,a,b, W[(5*i+1)%16], 14, tab[i]); i++;
GG(b,c,d,a, W[(5*i+1)%16], 20, tab[i]); i++;
}
while (i < 48) {
HH(a,b,c,d, W[(3*i+5)%16], 4, tab[i]); i++;
HH(d,a,b,c, W[(3*i+5)%16], 11, tab[i]); i++;
HH(c,d,a,b, W[(3*i+5)%16], 16, tab[i]); i++;
HH(b,c,d,a, W[(3*i+5)%16], 23, tab[i]); i++;
}
while (i < 64) {
II(a,b,c,d, W[7*i%16], 6, tab[i]); i++;
II(d,a,b,c, W[7*i%16], 10, tab[i]); i++;
II(c,d,a,b, W[7*i%16], 15, tab[i]); i++;
II(b,c,d,a, W[7*i%16], 21, tab[i]); i++;
}
s->h[0] += a;
s->h[1] += b;
s->h[2] += c;
s->h[3] += d;
}
static void pad(struct md5 *s)
{
unsigned r = s->len % 64;
s->buf[r++] = 0x80;
if (r > 56) {
memset(s->buf + r, 0, 64 - r);
r = 0;
processblock(s, s->buf);
}
memset(s->buf + r, 0, 56 - r);
s->len *= 8;
s->buf[56] = s->len;
s->buf[57] = s->len >> 8;
s->buf[58] = s->len >> 16;
s->buf[59] = s->len >> 24;
s->buf[60] = s->len >> 32;
s->buf[61] = s->len >> 40;
s->buf[62] = s->len >> 48;
s->buf[63] = s->len >> 56;
processblock(s, s->buf);
}
void md5_init(struct md5 *s)
{
s->len = 0;
s->h[0] = 0x67452301;
s->h[1] = 0xefcdab89;
s->h[2] = 0x98badcfe;
s->h[3] = 0x10325476;
}
void md5_sum(struct md5 *s, uint8_t md[16])
{
int i;
pad(s);
for (i = 0; i < 4; i++) {
md[4*i] = s->h[i];
md[4*i+1] = s->h[i] >> 8;
md[4*i+2] = s->h[i] >> 16;
md[4*i+3] = s->h[i] >> 24;
}
}
void md5_update(struct md5 *s, const void *m, unsigned long len)
{
const uint8_t *p = m;
unsigned r = s->len % 64;
s->len += len;
if (r) {
if (len < 64 - r) {
memcpy(s->buf + r, p, len);
return;
}
memcpy(s->buf + r, p, 64 - r);
len -= 64 - r;
p += 64 - r;
processblock(s, s->buf);
}
for (; len >= 64; len -= 64, p += 64)
processblock(s, p);
memcpy(s->buf, p, len);
}