sophuwu.site > otpp   
              37
            
             #include "otpp.h"

int validateSecret(char secret[]) {
    if (strlen(secret) != 32) {
        return 0;
    }
    for (int i = 0; i < 32; i++) {
        if (!((secret[i] >= '0' && secret[i] <= '9') || (secret[i] >= 'A' && secret[i] <= 'F'))) {
            return 0;
        }
    }
    return 1;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        printf("usage: %s <input>\n", argv[0]);
        printf("options:\n");
        printf("   --gen     Generate new secret\n");
        printf("   <secret>  Get a token for a secret\n");
        return 1;
    }
    if (strcmp(argv[1], "--gen") == 0) {
        OTP otp;
        otp.generateNewSecret();
        printf("generated new secret:\n%s\n", otp.exportSecret());
        return 0;
    }
    if (!validateSecret(argv[1])) {
        printf("invalid secret\nuse --gen to generate a new secret\n");
        return 1;
    }
    OTP otp;
    otp.importSecret(argv[1]);
    printf("current token:\n%d\n", otp.currentToken());
    return 0;
}