aboutsummaryrefslogtreecommitdiffstats
path: root/common.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2023-07-06 07:20:03 +0200
committerMattias Andrée <maandree@kth.se>2023-07-06 07:20:03 +0200
commit0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506 (patch)
tree29dbd39a897dbef2e391a095d39fe03b07b22d94 /common.c
parentRemove -Wall -O3 from CFLAGS (unportable) and -s from LDFLAGS (diff)
downloadblakesum-0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506.tar.gz
blakesum-0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506.tar.bz2
blakesum-0c3cf7819ceb7fe970f0fc240fa177b4e6dd5506.tar.xz
Add -S (salt) to bsum
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'common.c')
-rw-r--r--common.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/common.c b/common.c
index c389285..ab34e2c 100644
--- a/common.c
+++ b/common.c
@@ -226,3 +226,39 @@ hash_and_print(const char *path, size_t hashlen, int decode_hex, char newline, i
free(hex);
return 0;
}
+
+
+void
+parse_salt(uint_least8_t *salt, const char *s, size_t required_length)
+{
+ size_t i;
+
+ for (i = 0; i < required_length; i++, s = &s[2]) {
+ if (!s[0] || !s[1])
+ goto too_short;
+ if (!isxdigit(s[0]) || !isxdigit(s[1]))
+ goto not_hexadecimal;
+
+ salt[i] = (uint_least8_t)((((s[0] & 15) + (s[0] > '9' ? 9 : 0)) << 4) |
+ (s[1] & 15) + (s[1] > '9' ? 9 : 0));
+ }
+
+ if (*s)
+ goto too_long;
+
+ return;
+
+not_hexadecimal:
+ fprintf(stderr, "%s: specified salt contains non-hexadecimal-digit character\n", argv0);
+ exit(2);
+
+too_short:
+ fprintf(stderr, "%s: specified salt is shorter than expected, should be %zu hexadecimal digits\n",
+ argv0, required_length * 2);
+ exit(2);
+
+too_long:
+ fprintf(stderr, "%s: specified salt is longer than expected, should be %zu hexadecimal digits\n",
+ argv0, required_length * 2);
+ exit(2);
+}