Skip to content
\n

So it seems the secret can still be 16 characters.

\n

However, when I set the secret length to 16; and generate a secret using the createSecret() method; I get an output of 26 characters?
\nE.g. JVD6M43FQR6XBTRB6GTPU27VNM

\n

Am I missing something or am I confused somewhere?

\n

I'd like to re-use the existing secrets for now.

","upvoteCount":1,"answerCount":11,"acceptedAnswer":{"@type":"Answer","text":"

To conclude this: the secret length as per the specifications (RFCs 4226 and 6238) is the length of the binary string that is given to the HMAC hash function - there is no base32 encoding involved at all. Google's \"Key URI format\" specification uses base32 encoding in order to make the binary secret string portable (URL safe) - the base32 encoding naturally results in longer strings than the original secret.

\n

However, some of the top used libraries on packagist use some kind of pseudo base32 encoding, with a shorter secret string than requested as a result, which is highly insecure.

\n

Some of the bad examples:

\n

phpgangsta/googleauthenticator
\nhttps://github.com/PHPGangsta/GoogleAuthenticator/blob/505c2af8337b559b33557f37cda38e5f843f3768/PHPGangsta/GoogleAuthenticator.php#L24-L53

\n

christian-riesen/otp
\nhttps://github.com/ChristianRiesen/otp/blob/aab865ae6d356993ad9d51f8a7e8f32b8a54730a/src/GoogleAuthenticator.php#L149-L170

\n

silverstripe/totp-authenticator
\nthis one is even worse as it uses the same library as I to base32 encode the secret. but then truncates it to the desired length
\nhttps://github.com/silverstripe/silverstripe-totp-authenticator/blob/4a98f33d141f3cbcaaa47c12a1e70386720866e3/src/RegisterHandler.php#L73-L82

\n

2amigos/2fa-library
\nhttps://github.com/2amigos/2fa-library/blob/1d858fd79389394b7432c46538e8a40169fcae9c/src/Support/Encoder.php#L95-L102

\n

(I'm gonna stop here, that was enough horrible code for today...)

\n

I'll close this issue here as resolved.

","upvoteCount":1,"url":"https://github.com/chillerlan/php-authenticator/discussions/10#discussioncomment-12184880"}}}

Length of secret? #10

Answered by codemasher
jbostoen asked this question in Q&A
Discussion options

You must be logged in to vote

To conclude this: the secret length as per the specifications (RFCs 4226 and 6238) is the length of the binary string that is given to the HMAC hash function - there is no base32 encoding involved at all. Google's "Key URI format" specification uses base32 encoding in order to make the binary secret string portable (URL safe) - the base32 encoding naturally results in longer strings than the original secret.

However, some of the top used libraries on packagist use some kind of pseudo base32 encoding, with a shorter secret string than requested as a result, which is highly insecure.

Some of the bad examples:

phpgangsta/googleauthenticator
https://github.com/PHPGangsta/GoogleAuthenticator/b…

Replies: 11 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by codemasher
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #8 on February 13, 2025 08:56.