summaryrefslogtreecommitdiff
path: root/userland/minibox/utilities/sha1sum.c
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-07-03 18:32:04 +0200
committerAnton Kling <anton@kling.gg>2024-07-03 18:32:04 +0200
commit6ec139d3ef7c1d2a52bb786779dd1914f125eda4 (patch)
tree30c75cd402e0c3ca6bcb5ac3d9226cf394b6721a /userland/minibox/utilities/sha1sum.c
parent4e7918753175dbd8fc38bc7c5b176517e1dbef2f (diff)
rdate: Add a very basic implementation rdate
Also adds sha1sum.c file which I forgot in a previous commit
Diffstat (limited to 'userland/minibox/utilities/sha1sum.c')
-rw-r--r--userland/minibox/utilities/sha1sum.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/userland/minibox/utilities/sha1sum.c b/userland/minibox/utilities/sha1sum.c
new file mode 100644
index 0000000..9c98cc0
--- /dev/null
+++ b/userland/minibox/utilities/sha1sum.c
@@ -0,0 +1,62 @@
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <tb/sha1.h>
+#include <unistd.h>
+
+static int sha1_hash_file(const char *name, int fd) {
+ SHA1_CTX ctx;
+ SHA1_Init(&ctx);
+
+ for (;;) {
+ char buffer[4096];
+ int rc = read(fd, buffer, 4096);
+ if (-1 == rc) {
+ perror("read");
+ return 0;
+ }
+ if (0 == rc) {
+ break;
+ }
+ SHA1_Update(&ctx, buffer, rc);
+ }
+ unsigned char digest[SHA1_LEN];
+ SHA1_Final(&ctx, digest);
+
+ printf("%s: ", name);
+ for (int i = 0; i < SHA1_LEN; i++) {
+ printf("%02x", digest[i]);
+ }
+ printf("\n");
+ return 1;
+}
+
+int sha1sum_main(int argc, char **argv) {
+ int fd = STDIN_FILENO;
+
+ // If no file operands are specified, the standard input shall be
+ // used.
+ if (argc < 2) {
+ return (sha1_hash_file("-", 0)) ? 0 : 1;
+ }
+
+ argv++;
+ for (; *argv; argv++) {
+ if (0 == strcmp(*argv, "-")) {
+ if (!sha1_hash_file("-", 0)) {
+ return 1;
+ }
+ continue;
+ }
+
+ if (-1 == (fd = open(*argv, O_RDONLY, 0))) {
+ perror(*argv);
+ return 1;
+ }
+ if (!sha1_hash_file(*argv, fd)) {
+ return 1;
+ }
+ close(fd);
+ }
+ return 0;
+}