otp.c
changeset 56 a573346e6f7b
parent 55 0aaf56a96d21
child 57 4974e5368a29
equal deleted inserted replaced
55:0aaf56a96d21 56:a573346e6f7b
     1 #include <stdio.h>
     1 #include <stdio.h>
     2 #include <stdint.h>
     2 #include <stdint.h>
     3 #include <string.h>
     3 #include <string.h>
     4 #include <time.h>
     4 #include <time.h>
     5 #include "sha1.h"
     5 #include "sha1.h"
       
     6 #include "otp.h"
     6 
     7 
     7 static int base32_decode(const char *src, const int srclen, uint8_t *dst, const int dstlen)
     8 static int base32_decode(const char *src, const int srclen, uint8_t *dst, const int dstlen)
     8 {
     9 {
     9     const int len = srclen == -1 ? strlen((const char *) src) : srclen;
    10     const int len = srclen == -1 ? strlen((const char *) src) : srclen;
    10     int retval = 0;
    11     int retval = 0;
    54     }
    55     }
    55 
    56 
    56     return retval;
    57     return retval;
    57 }
    58 }
    58 
    59 
    59 static int totp(const char *base32_secret, char *dst, int dstlen)
    60 int totp(const char *base32_secret, char *dst, int dstlen)
    60 {
    61 {
    61     uint8_t decoded[64];
    62     uint8_t decoded[64];
    62     int decodedlen;
    63     int decodedlen;
    63     uint64_t secs;
    64     uint64_t secs;
    64     uint8_t timebytes[8];
    65     uint8_t timebytes[8];
    86 
    87 
    87     snprintf(dst, dstlen, "%06u", (unsigned int) val);
    88     snprintf(dst, dstlen, "%06u", (unsigned int) val);
    88     return 0;
    89     return 0;
    89 }
    90 }
    90 
    91 
    91 int main(int argc, char **argv)
       
    92 {
       
    93     char result[16];
       
    94     int i;
       
    95     for (i = 1; i < argc; i++) {
       
    96         printf("%s: ", argv[i]);
       
    97         if (totp(argv[i], result, sizeof (result)) == -1) {
       
    98             printf("[FAILED!]");
       
    99         } else {
       
   100             printf("%s (valid for %d more seconds)", result, 30 - ((int) (time(NULL) % 30)));
       
   101         }
       
   102         printf("\n");
       
   103     }
       
   104     return 0;
       
   105 }
       
   106 
       
   107 /* end of otp.c ... */
    92 /* end of otp.c ... */
   108 
    93