Skip to content

Commit

Permalink
Update otp.js
Browse files Browse the repository at this point in the history
Add generateSecret method.
  • Loading branch information
coreybutler authored Jun 22, 2024
1 parent d227f40 commit 66151c6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/otp/otp.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,23 @@ export async function TOTP (secret, cfg = {}) {

return HOTP(secret, cfg)
}

/**
* Generate a TOTP-compliant UTF-8 secret.
* @param {number} length (16, 32)
* The length of the secret.
* @returns {boolean}
* Returns a randomly generated UTTF-8 string
*/
export function generateSecret(length = 16) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_+<>'
let randomCode = ''

// Generate random characters
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length)
randomCode += characters.charAt(randomIndex)
}

return randomCode
}

0 comments on commit 66151c6

Please sign in to comment.