-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateQRcode.c
More file actions
66 lines (51 loc) · 1.8 KB
/
generateQRcode.c
File metadata and controls
66 lines (51 loc) · 1.8 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "lib/encoding.h"
void convertStrToHex(char* inputStr, uint8_t*outputStr) {
int i;
int j;
char subStr[2];
for (i = 0, j = 0; i < strlen(inputStr); i+=2, j++) {
subStr[0] = inputStr[i];
subStr[1] = inputStr[i+1];
outputStr[j] = strtol(subStr, NULL, 16);
}
}
int
main(int argc, char * argv[])
{
if ( argc != 4 ) {
printf("Usage: %s [issuer] [accountName] [secretHex]\n", argv[0]);
return(-1);
}
char * issuer = argv[1];
char * accountName = argv[2];
char * secret_hex = argv[3];
assert (strlen(secret_hex) <= 20);
printf("\nIssuer: %s\nAccount Name: %s\nSecret (Hex): %s\n\n",
issuer, accountName, secret_hex);
// Create an otpauth:// URI and display a QR code that's compatible
// with Google Authenticator
// Convert secret_hex from string to hex representation
uint8_t buf[10];
convertStrToHex(secret_hex, buf);
// Base32 encode the secret
uint8_t secretHexEncoded[17];
base32_encode((const uint8_t *) buf, 10, secretHexEncoded, 16);
// Set end of encoded hex as null byte
secretHexEncoded[16] = '\0';
// URL encode account Name and issuer
const char* accountNameEncoded = urlEncode(accountName);
const char* issuerEncoded = urlEncode(issuer);
// Generate the HOTP URI
char *hotpURI = malloc(14 + strlen(accountNameEncoded) + 8 + strlen(issuerEncoded) + 8 + 16 + 10);
sprintf(hotpURI, "otpauth://hotp/%s?issuer=%s&secret=%s&counter=1", accountNameEncoded, issuerEncoded, secretHexEncoded);
displayQRcode(hotpURI);
// Generate the TOTP URL
char *totpURI = malloc(14 + strlen(accountNameEncoded) + 8 + strlen(issuerEncoded) + 8 + 16 + 10);
sprintf(totpURI, "otpauth://totp/%s?issuer=%s&secret=%s&period=30", accountNameEncoded, issuerEncoded, secretHexEncoded);
displayQRcode(totpURI);
return (0);
}