Add SHA-1 and SHA-512 support, fix log.Fatal in library code#10
Merged
Conversation
- Add Algorithm type with SHA1, SHA256, SHA512 constants - Add Algorithm field to TOTP struct (zero value defaults to SHA256 for backward compat) - Replace log.Fatal in Verify/VerifyWithTimestamp with return false - Fix validateSecret mutating totp.Key by using a local variable - Update GenerateURI to reflect the actual algorithm in the URI - Add tests for SHA1, SHA512, and cross-algorithm isolation - Update README with algorithm docs and fix incorrect code examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Algorithmtype withSHA1,SHA256,SHA512constants and a matching field on theTOTPstruct; zero value defaults to SHA-256 so all existing code compiles and behaves identicallylog.Fatalcalls inVerifyandVerifyWithTimestampwithreturn false— library code must never terminate the host processvalidateSecretsilently mutatingtotp.Keyby padding into a local variable insteadGenerateURIto embed the actual algorithm in the URI rather than hardcodingSHA256Algorithmfield, add a usage snippet for each algorithm, and fix the code example which ignored errors fromGenerateTOTP/GenerateURITest plan
go test ./...passes (8 tests, all green)TestTOTP_GenerateTOTP,TestTOTP_VerifyTimestampOK) still produce the same codes — backward compat confirmedTestTOTP_SHA1andTestTOTP_SHA512verify correct codes and window expiry for each new algorithmTestTOTP_AlgorithmIsolationconfirms no two algorithms produce the same code for the same input