j256 / two-factor-auth

Two Factor Authentication Java code implementing the Time-based One-time Password Algorithm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use with TOTP systems that generate Hex secrets

craigmit opened this issue · comments

Some TOTP systems will generate Hex secrets. This project still works with them. You just need to replace decodeBase32 with:

static byte[] decodeHexToBase32(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

And it works great! 🙂

An example hex secret would look like: 1234567890abcdef1234567890abcdef12345678