Added commands "sha224sum", "sha256sum", "sha384sum" and "sha512sum". Notes: Sun Jan 2 02:28:04 EST 2011 geoff Surely options to sha1sum would make more sense than a handful of new, very similar commands, particularly since there will only be more sha variants in future. Wed Jan 5 18:25:20 EST 2011 geoff done, but very differently. see new sum(1), soon to appear. Reference: /n/sources/patch/applied/sha2sum Date: Sat Jan 1 20:47:03 CET 2011 Signed-off-by: djc@9grid.fr Reviewed-by: geoff --- /sys/man/1/sum Sat Jan 1 20:46:39 2011 +++ /sys/man/1/sum Sat Jan 1 20:46:36 2011 @@ -1,6 +1,6 @@ .TH SUM 1 .SH NAME -sum, md5sum, sha1sum \- sum and count blocks in a file +sum, md5sum, sha1sum, sha224sum, sha256sum, sha384sum, sha512sum \- sum and count blocks in a file .SH SYNOPSIS .B sum [ @@ -19,6 +19,26 @@ [ .I file ... ] +.PP +.B sha224sum +[ +.I file ... +] +.PP +.B sha256sum +[ +.I file ... +] +.PP +.B sha384sum +[ +.I file ... +] +.PP +.B sha512sum +[ +.I file ... +] .SH DESCRIPTION By default, .I sum @@ -59,19 +79,63 @@ summed. .PP .B Sha1sum -computes the 40 hex digit National Institute of Standards and Technology SHA1 secure hash algorithm +computes the 40 hex digit National Institute of Standards and Technology SHA-1 secure hash algorithm described in FIPS PUB 180-1. If no .I files are given, the standard input is summed. +.PP +.B Sha224sum +computes the 56 hex digit National Institute of Standards and Technology SHA-224 secure hash algorithm +described in FIPS PUB 180-3. +If no +.I files +are given, +the standard input is +summed. +.PP +.B Sha256sum +computes the 64 hex digit National Institute of Standards and Technology SHA-256 secure hash algorithm +described in FIPS PUB 180-2. +If no +.I files +are given, +the standard input is +summed. +.PP +.B Sha384sum +computes the 96 hex digit National Institute of Standards and Technology SHA-384 secure hash algorithm +described in FIPS PUB 180-2. +If no +.I files +are given, +the standard input is +summed. +.PP +.B Sha512sum +computes the 128 hex digit National Institute of Standards and Technology SHA-512 secure hash algorithm +described in FIPS PUB 180-2. +If no +.I files +are given, +the standard input is +summed. .SH SOURCE .B /sys/src/cmd/sum.c .br .B /sys/src/cmd/md5sum.c .br .B /sys/src/cmd/sha1sum.c +.br +.B /sys/src/cmd/sha224sum.c +.br +.B /sys/src/cmd/sha256sum.c +.br +.B /sys/src/cmd/sha384sum.c +.br +.B /sys/src/cmd/sha512sum.c .SH "SEE ALSO" .IR cmp (1), .IR wc (1) --- /sys/src/cmd/sha224sum.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/sha224sum.c Wed Jan 5 23:53:49 2011 @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#pragma varargck type "M" uchar* + +static DS* (*shafunc)(uchar *data, ulong dlen, uchar *digest, DS *state); +static int shadlen; + +static int +digestfmt(Fmt *fmt) +{ + char buf[SHA2_512dlen*2 + 1]; + uchar *p; + int i; + + p = va_arg(fmt->args, uchar*); + for(i = 0; i < shadlen; i++) + sprint(buf + 2*i, "%.2ux", p[i]); + return fmtstrcpy(fmt, buf); +} + +static void +sum(int fd, char *name) +{ + int n; + uchar buf[8192], digest[SHA2_512dlen]; + DigestState *s; + + s = (*shafunc)(nil, 0, nil, nil); + while((n = read(fd, buf, sizeof buf)) > 0) + (*shafunc)(buf, n, nil, s); + if(n < 0){ + fprint(2, "reading %s: %r\n", name? name: "stdin"); + return; + } + (*shafunc)(nil, 0, digest, s); + if(name == nil) + print("%M\n", digest); + else + print("%M\t%s\n", digest, name); +} + +void +main(int argc, char *argv[]) +{ + int i, fd; + + shafunc = sha2_224; + shadlen = SHA2_224dlen; + ARGBEGIN{ + default: + fprint(2, "usage: %s [file...]\n", argv0); + exits("usage"); + }ARGEND + + fmtinstall('M', digestfmt); + + if(argc == 0) + sum(0, nil); + else + for(i = 0; i < argc; i++){ + fd = open(argv[i], OREAD); + if(fd < 0){ + fprint(2, "%s: can't open %s: %r\n", argv0, argv[i]); + continue; + } + sum(fd, argv[i]); + close(fd); + } + exits(nil); +} --- /sys/src/cmd/sha256sum.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/sha256sum.c Sat Jan 1 20:46:42 2011 @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#pragma varargck type "M" uchar* + +static int +digestfmt(Fmt *fmt) +{ + char buf[SHA2_256dlen*2+1]; + uchar *p; + int i; + + p = va_arg(fmt->args, uchar*); + for(i=0; i 0) + sha2_256(buf, n, nil, s); + if(n < 0){ + fprint(2, "reading %s: %r\n", name ? name : "stdin"); + return; + } + sha2_256(nil, 0, digest, s); + if(name == nil) + print("%M\n", digest); + else + print("%M\t%s\n", digest, name); +} + +void +main(int argc, char *argv[]) +{ + int i, fd; + + ARGBEGIN{ + default: + fprint(2, "usage: sha256sum [file...]\n"); + exits("usage"); + }ARGEND + + fmtinstall('M', digestfmt); + + if(argc == 0) + sum(0, nil); + else for(i = 0; i < argc; i++){ + fd = open(argv[i], OREAD); + if(fd < 0){ + fprint(2, "sha256sum: can't open %s: %r\n", argv[i]); + continue; + } + sum(fd, argv[i]); + close(fd); + } + exits(nil); +} --- /sys/src/cmd/sha384sum.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/sha384sum.c Sat Jan 1 20:46:44 2011 @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#pragma varargck type "M" uchar* + +static int +digestfmt(Fmt *fmt) +{ + char buf[SHA2_384dlen*2+1]; + uchar *p; + int i; + + p = va_arg(fmt->args, uchar*); + for(i=0; i 0) + sha2_384(buf, n, nil, s); + if(n < 0){ + fprint(2, "reading %s: %r\n", name ? name : "stdin"); + return; + } + sha2_384(nil, 0, digest, s); + if(name == nil) + print("%M\n", digest); + else + print("%M\t%s\n", digest, name); +} + +void +main(int argc, char *argv[]) +{ + int i, fd; + + ARGBEGIN{ + default: + fprint(2, "usage: sha384sum [file...]\n"); + exits("usage"); + }ARGEND + + fmtinstall('M', digestfmt); + + if(argc == 0) + sum(0, nil); + else for(i = 0; i < argc; i++){ + fd = open(argv[i], OREAD); + if(fd < 0){ + fprint(2, "sha384sum: can't open %s: %r\n", argv[i]); + continue; + } + sum(fd, argv[i]); + close(fd); + } + exits(nil); +} --- /sys/src/cmd/sha512sum.c Thu Jan 1 00:00:00 1970 +++ /sys/src/cmd/sha512sum.c Sat Jan 1 20:46:46 2011 @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#pragma varargck type "M" uchar* + +static int +digestfmt(Fmt *fmt) +{ + char buf[SHA2_512dlen*2+1]; + uchar *p; + int i; + + p = va_arg(fmt->args, uchar*); + for(i=0; i 0) + sha2_512(buf, n, nil, s); + if(n < 0){ + fprint(2, "reading %s: %r\n", name ? name : "stdin"); + return; + } + sha2_512(nil, 0, digest, s); + if(name == nil) + print("%M\n", digest); + else + print("%M\t%s\n", digest, name); +} + +void +main(int argc, char *argv[]) +{ + int i, fd; + + ARGBEGIN{ + default: + fprint(2, "usage: sha512sum [file...]\n"); + exits("usage"); + }ARGEND + + fmtinstall('M', digestfmt); + + if(argc == 0) + sum(0, nil); + else for(i = 0; i < argc; i++){ + fd = open(argv[i], OREAD); + if(fd < 0){ + fprint(2, "sha512sum: can't open %s: %r\n", argv[i]); + continue; + } + sum(fd, argv[i]); + close(fd); + } + exits(nil); +}