EncryptTOTPSecret encrypts the TOTP shared secret with AES-256-GCM (using the existing EncryptAES helper, which derives the key via HKDF) and prepends TOTPCipherPrefix so it is recognisable as ciphertext. TOTP secrets are long-lived — they are enrolled once per user and used forever — so a reversib
(plain, key string)
| 26 | // forever — so a reversible primitive is required (we need the original |
| 27 | // secret on every Validate call to compute the expected code). |
| 28 | func EncryptTOTPSecret(plain, key string) (string, error) { |
| 29 | if plain == "" { |
| 30 | return "", nil |
| 31 | } |
| 32 | ct, err := EncryptAES(key, plain) |
| 33 | if err != nil { |
| 34 | return "", err |
| 35 | } |
| 36 | return TOTPCipherPrefix + ct, nil |
| 37 | } |
| 38 | |
| 39 | // DecryptTOTPSecret decrypts a value previously written by |
| 40 | // EncryptTOTPSecret. It is strict: the stored value MUST carry the |