Skip to content

Add SHA-1 and SHA-512 support, fix log.Fatal in library code#10

Merged
MrTuNNe merged 1 commit into
mainfrom
feature/multi-algorithm-support
May 18, 2026
Merged

Add SHA-1 and SHA-512 support, fix log.Fatal in library code#10
MrTuNNe merged 1 commit into
mainfrom
feature/multi-algorithm-support

Conversation

@MrTuNNe

@MrTuNNe MrTuNNe commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add Algorithm type with SHA1, SHA256, SHA512 constants and a matching field on the TOTP struct; zero value defaults to SHA-256 so all existing code compiles and behaves identically
  • Replace log.Fatal calls in Verify and VerifyWithTimestamp with return false — library code must never terminate the host process
  • Fix validateSecret silently mutating totp.Key by padding into a local variable instead
  • Update GenerateURI to embed the actual algorithm in the URI rather than hardcoding SHA256
  • Add tests for SHA-1, SHA-512, and cross-algorithm isolation (all three algorithms must produce distinct codes)
  • Update README: document the Algorithm field, add a usage snippet for each algorithm, and fix the code example which ignored errors from GenerateTOTP/GenerateURI

Test plan

  • go test ./... passes (8 tests, all green)
  • Existing SHA-256 tests (TestTOTP_GenerateTOTP, TestTOTP_VerifyTimestampOK) still produce the same codes — backward compat confirmed
  • TestTOTP_SHA1 and TestTOTP_SHA512 verify correct codes and window expiry for each new algorithm
  • TestTOTP_AlgorithmIsolation confirms no two algorithms produce the same code for the same input

- 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
@MrTuNNe MrTuNNe merged commit a0f1ebe into main May 18, 2026
1 check passed
@MrTuNNe MrTuNNe deleted the feature/multi-algorithm-support branch May 18, 2026 08:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant