Skip to content

Instantly share code, notes, and snippets.

@iaindooley
Created October 16, 2024 05:22
Show Gist options
  • Save iaindooley/254a5fabee4a5f649aaa1ef0e4d948b1 to your computer and use it in GitHub Desktop.
Save iaindooley/254a5fabee4a5f649aaa1ef0e4d948b1 to your computer and use it in GitHub Desktop.
Personal TOTP URL Generator
function generateTOTPURL(secret, label,issuer) {
return `otpauth://totp/${label}?secret=${secret}&issuer=${issuer}`;
}
// Function to encode a string to Base32
function base32Encode(str) {
const base32Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
let binString = '';
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i);
const binChar = charCode.toString(2).padStart(8, '0');
binString += binChar;
}
let encodedStr = '';
for (let i = 0; i < binString.length; i += 5) {
const chunk = binString.slice(i, i + 5);
const index = parseInt(chunk, 2);
encodedStr += base32Chars[index];
}
return encodedStr;
}
const secret = 'PUT A GOOD SECRET HERE';
const label = 'SOME LABEL';
const issuer = 'SOME OTHER LABEL';
const encodedSecret = base32Encode(secret);
const totpURL = generateTOTPURL(encodedSecret, label, issuer);
//USE ANY QR CODE GENERATOR TO GENERATE THE QR CODE FROM THE totpURL
console.log(totpURL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment