From: alex Date: Fri, 12 Dec 2025 07:02:13 +0000 (-0800) Subject: chore: add formatting X-Git-Tag: v1.0.1^0 X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=totp chore: add formatting --- diff --git a/Makefile.am b/Makefile.am index 8c4aa60..e99a806 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,3 +18,6 @@ totp_SOURCES += \ inc/usage.h SUBDIRS = . test + +format: + VERSION_CONTROL=none indent -linux -brf $(totp_SOURCES) diff --git a/inc/args.h b/inc/args.h index ebc3cdd..1570ea3 100644 --- a/inc/args.h +++ b/inc/args.h @@ -12,6 +12,6 @@ #include #include -int args(int, char**); +int args(int, char **); #endif diff --git a/inc/base32.h b/inc/base32.h index c75ccc0..234c392 100644 --- a/inc/base32.h +++ b/inc/base32.h @@ -4,6 +4,6 @@ #include #include -int from_base32(unsigned char**, ssize_t*); +int from_base32(unsigned char **, ssize_t *); #endif diff --git a/inc/main.h b/inc/main.h index cf632a2..51a565f 100644 --- a/inc/main.h +++ b/inc/main.h @@ -9,6 +9,6 @@ #include #include -int main(int,char**); +int main(int, char **); #endif diff --git a/inc/totp.h b/inc/totp.h index 69505be..821d140 100644 --- a/inc/totp.h +++ b/inc/totp.h @@ -12,6 +12,6 @@ extern time_t now; -int totp(unsigned char*, ssize_t, uint32_t*); +int totp(unsigned char *, ssize_t, uint32_t *); #endif diff --git a/manifest.scm b/manifest.scm index 6c53f86..4b2fcf2 100644 --- a/manifest.scm +++ b/manifest.scm @@ -1,11 +1,12 @@ (specifications->manifest (list "autoconf" "automake" "coreutils" - "gawk" + "gawk" "gcc-toolchain" - "gdb" + "gdb" "grep" + "indent" "make" - "openssl" + "openssl" "sed" - "valgrind")) + "valgrind")) diff --git a/src/args.c b/src/args.c index 1ae55f1..3b461e1 100644 --- a/src/args.c +++ b/src/args.c @@ -3,7 +3,7 @@ static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"time", required_argument, 0, 't'}, - {0,0,0,0} + {0, 0, 0, 0} }; time_t now = 0; @@ -12,37 +12,44 @@ int args(int argc, char **argv) { struct tm tm; int c; - while(1) { + while (1) { int option_index = 0; - if((c = getopt_long(argc, argv, "ht:", long_options, &option_index)) == -1) { break; } - - switch(c) { - case 0: - if(long_options[option_index].flag != 0) { break; } - - fprintf(stderr, "option %s", long_options[option_index].name); - if(optarg) { - fprintf(stderr, " with arg %s", optarg); - } - fprintf(stderr, "\n"); - return -1; - - break; - case 'h': - usage(); - return -1; - case 't': - memset(&tm, 0, sizeof(tm)); - - if(strptime(optarg, "%F %T %z", &tm) != NULL) { - now = mktime(&tm); - } + if ((c = + getopt_long(argc, argv, "ht:", long_options, + &option_index)) == -1) { + break; + } + switch (c) { + case 0: + if (long_options[option_index].flag != 0) { break; - case '?': - default: - return -1; + } + + fprintf(stderr, "option %s", + long_options[option_index].name); + if (optarg) { + fprintf(stderr, " with arg %s", optarg); + } + fprintf(stderr, "\n"); + return -1; + + break; + case 'h': + usage(); + return -1; + case 't': + memset(&tm, 0, sizeof(tm)); + + if (strptime(optarg, "%F %T %z", &tm) != NULL) { + now = mktime(&tm); + } + + break; + case '?': + default: + return -1; } } diff --git a/src/base32.c b/src/base32.c index 500211e..f40b92c 100644 --- a/src/base32.c +++ b/src/base32.c @@ -5,24 +5,24 @@ int from_base32(unsigned char **raw, ssize_t *n) { char c, to_set; size_t i, j, offset, len; - if(NULL == raw || NULL == *raw) { + if (NULL == raw || NULL == *raw) { fprintf(stderr, "invalid buffer\n"); return -1; } - if(NULL == n || *n <= 0) { + if (NULL == n || *n <= 0) { fprintf(stderr, "invalid secret size\n"); return -1; } - len = ((*n)*5)>>3; - if(len == 0) { + len = ((*n) * 5) >> 3; + if (len == 0) { fprintf(stderr, "invalid secret size\n"); return -1; } - + buf = calloc(len, sizeof(char)); - if(NULL == buf) { + if (NULL == buf) { perror("calloc"); return -1; } @@ -30,14 +30,14 @@ int from_base32(unsigned char **raw, ssize_t *n) { offset = 0; p = *raw; j = 0; - for(i = 0; i < *n; i++) { + for (i = 0; i < *n; i++) { c = p[i]; - if(c >= 'A' && c <= 'Z') { + if (c >= 'A' && c <= 'Z') { to_set = c - 65; - } else if(c >= '2' && c <= '7') { + } else if (c >= '2' && c <= '7') { to_set = c - 24; - } else if(c == '=') { + } else if (c == '=') { continue; } else { fprintf(stderr, "invalid base32 character %c\n", c); @@ -47,7 +47,7 @@ int from_base32(unsigned char **raw, ssize_t *n) { // No need to mask this value since it will always be <31 buf[j] += (to_set << 3) >> offset; - if(((offset + 5) > 7) && (j + 1 < len)) { + if (((offset + 5) > 7) && (j + 1 < len)) { j++; buf[j] += ((to_set << (8 - (5 - (8 - offset)))) & 0xff); } diff --git a/src/main.c b/src/main.c index dc228b0..3827a2d 100644 --- a/src/main.c +++ b/src/main.c @@ -9,26 +9,34 @@ int main(int argc, char **argv) { buf = NULL; now = 0; - if(args(argc, argv)<0) { return EXIT_FAILURE; } + if (args(argc, argv) < 0) { + return EXIT_FAILURE; + } - if((n = getline((char **)&buf, &len, stdin)) <= 0) { + if ((n = getline((char **)&buf, &len, stdin)) <= 0) { perror("getline failed"); fprintf(stderr, "No secret provided\n"); goto clean; } - // Trim trailing '\n' n--; - if(from_base32(&buf, &n) < 0) { goto clean; } - if(totp((unsigned char *)buf, n, &code) < 0) { goto clean; } + if (from_base32(&buf, &n) < 0) { + goto clean; + } + + if (totp((unsigned char *)buf, n, &code) < 0) { + goto clean; + } fprintf(stdout, "%.6u\n", code % 1000000); free(buf); return EXIT_SUCCESS; - clean: - if(buf != NULL) { free(buf); } - return EXIT_FAILURE; + clean: + if (buf != NULL) { + free(buf); + } + return EXIT_FAILURE; } diff --git a/src/totp.c b/src/totp.c index d8c8c6e..55c07e0 100644 --- a/src/totp.c +++ b/src/totp.c @@ -6,31 +6,37 @@ int totp(unsigned char *key, ssize_t n, uint32_t *code) { size_t offset; unsigned int hash_len = SHA_DIGEST_LENGTH; - if(NULL == key) { return -1; } - if(n <= 0) { return -1; } + if (NULL == key) { + return -1; + } + + if (n <= 0) { + return -1; + } - if(now == 0) { + if (now == 0) { now = time(NULL); } now = floor(now / 30); // Guarantee that the data buffer is in big-endian order - for(size_t i = 0; i> (8*i) & 0xff; + for (size_t i = 0; i < sizeof(time_t); i++) { + data[sizeof(time_t) - (1 + i)] = now >> (8 * i) & 0xff; } - if(NULL == HMAC(EVP_sha1(), key, n, data, sizeof(time_t), hash, &hash_len)) { + if (NULL == + HMAC(EVP_sha1(), key, n, data, sizeof(time_t), hash, &hash_len)) { fprintf(stderr, "hashing failed\n"); return -1; } offset = hash[SHA_DIGEST_LENGTH - 1] & 0xf; - (*code) = ((hash[offset] & 0x7f) << 24) - | ((hash[offset+1] & 0xff) << 16) - | ((hash[offset+2] & 0xff) << 8) - | (hash[offset+3] & 0xff); + (*code) = ((hash[offset] & 0x7f) << 24) + | ((hash[offset + 1] & 0xff) << 16) + | ((hash[offset + 2] & 0xff) << 8) + | (hash[offset + 3] & 0xff); return 1; } diff --git a/src/usage.c b/src/usage.c index 8500a3e..6adfca0 100644 --- a/src/usage.c +++ b/src/usage.c @@ -4,5 +4,6 @@ void usage() { fprintf(stderr, "Usage:\n"); fprintf(stderr, "\t[SECRET] | totp\n"); fprintf(stderr, "\n"); - fprintf(stderr, "Pass a base32 encoded secrets to stdin and generate a totp code at the current time.\n"); + fprintf(stderr, + "Pass a base32 encoded secrets to stdin and generate a totp code at the current time.\n"); }