aboutsummaryrefslogtreecommitdiffstats
path: root/common.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2023-07-06 15:51:36 +0200
committerMattias Andrée <maandree@kth.se>2023-07-06 15:51:36 +0200
commit54b1980cc5d13d8ae2f697ee4834bd6b692bf7ab (patch)
tree3b894eca9af7850f805c383cf185ae05d68a12f4 /common.c
parentbump year (diff)
downloadblakesum-54b1980cc5d13d8ae2f697ee4834bd6b692bf7ab.tar.gz
blakesum-54b1980cc5d13d8ae2f697ee4834bd6b692bf7ab.tar.bz2
blakesum-54b1980cc5d13d8ae2f697ee4834bd6b692bf7ab.tar.xz
Add -P (pepper), -S (salt), and -K (key) to b2sum + m fix1.0
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--common.c74
1 files changed, 64 insertions, 10 deletions
diff --git a/common.c b/common.c
index ab34e2c..45a764b 100644
--- a/common.c
+++ b/common.c
@@ -227,9 +227,8 @@ hash_and_print(const char *path, size_t hashlen, int decode_hex, char newline, i
return 0;
}
-
-void
-parse_salt(uint_least8_t *salt, const char *s, size_t required_length)
+static void
+parse_salt_or_pepper(uint_least8_t *out, const char *s, size_t required_length, const char *type)
{
size_t i;
@@ -239,8 +238,8 @@ parse_salt(uint_least8_t *salt, const char *s, size_t required_length)
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));
+ out[i] = (uint_least8_t)((((s[0] & 15) + (s[0] > '9' ? 9 : 0)) << 4) |
+ (s[1] & 15) + (s[1] > '9' ? 9 : 0));
}
if (*s)
@@ -249,16 +248,71 @@ parse_salt(uint_least8_t *salt, const char *s, size_t required_length)
return;
not_hexadecimal:
- fprintf(stderr, "%s: specified salt contains non-hexadecimal-digit character\n", argv0);
+ fprintf(stderr, "%s: specified %s contains non-hexadecimal-digit character\n", argv0, type);
exit(2);
too_short:
- fprintf(stderr, "%s: specified salt is shorter than expected, should be %zu hexadecimal digits\n",
- argv0, required_length * 2);
+ fprintf(stderr, "%s: specified %s is shorter than expected, should be %zu hexadecimal digits\n",
+ argv0, type, required_length * 2);
+ exit(2);
+
+too_long:
+ fprintf(stderr, "%s: specified %s is longer than expected, should be %zu hexadecimal digits\n",
+ argv0, type, required_length * 2);
+ exit(2);
+}
+
+void
+parse_salt(uint_least8_t *salt, const char *s, size_t required_length)
+{
+ parse_salt_or_pepper(salt, s, required_length, "salt");
+}
+
+void
+parse_pepper(uint_least8_t *pepper, const char *s, size_t required_length)
+{
+ parse_salt_or_pepper(pepper, s, required_length, "pepper");
+}
+
+size_t
+parse_key(uint_least8_t *key, const char *s, size_t maximum_length)
+{
+ size_t i;
+
+ if (!*s)
+ goto empty_key;
+
+ for (i = 0; i < maximum_length; i++, s = &s[2]) {
+ if (!s[0])
+ break;
+ if (!s[1])
+ goto odd_length;
+ if (!isxdigit(s[0]) || !isxdigit(s[1]))
+ goto not_hexadecimal;
+
+ key[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 i;
+
+empty_key:
+ fprintf(stderr, "%s: specified key is empty\n", argv0);
+ exit(2);
+
+odd_length:
+ fprintf(stderr, "%s: specified key contains an odd number of hexadecimal digits\n", argv0);
+ exit(2);
+
+not_hexadecimal:
+ fprintf(stderr, "%s: specified key contains non-hexadecimal-digit character\n", argv0);
exit(2);
too_long:
- fprintf(stderr, "%s: specified salt is longer than expected, should be %zu hexadecimal digits\n",
- argv0, required_length * 2);
+ fprintf(stderr, "%s: specified key is longer than allowed, should be at most %zu hexadecimal digits\n",
+ argv0, maximum_length * 2);
exit(2);
}