File: keyparse.c

package info (click to toggle)
dq 20230101-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,020 kB
  • sloc: ansic: 8,269; makefile: 363; sh: 176; python: 82
file content (38 lines) | stat: -rw-r--r-- 805 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * 20131204
 * Jan Mojzis
 * Public domain.
 */

#include "hexdecode.h"
#include "base32decode.h"
#include "byte.h"
#include "str.h"
#include "keyparse.h"

int keyparse(unsigned char *keys, long long keyslen, const char *xx) {

    long long len;
    const unsigned char *x = (const unsigned char *)xx;

    if (!xx) return 0;
    if (keyslen != 32) return 0;

    len = str_len(xx);

    switch (len) {
        case 54:
            if (!byte_isequal(x, 3, "uz5")) return 0;
            if (!base32decode(keys, 32, x + 3, 51)) return 0;
            break;
        case 51:
            if (!base32decode(keys, 32, x, 51)) return 0;
            break;
        case 64:
            if (!hexdecode(keys, 32, x, 64)) return 0;
            break;
        default:
            return 0;
    }
    return 1;
}