HashOTP returns the hex-encoded HMAC-SHA256 of the OTP value under the supplied server key. OTPs are short-lived (minutes) so we do not need a reversible primitive — only the verifier needs to be able to recompute the digest from a candidate value. This means an offline DB dump no longer reveals usa
(otp, key string)
| 13 | // the digest from a candidate value. This means an offline DB dump no |
| 14 | // longer reveals usable OTPs. |
| 15 | func HashOTP(otp, key string) string { |
| 16 | h := hmac.New(sha256.New, []byte(key)) |
| 17 | h.Write([]byte(otp)) |
| 18 | return hex.EncodeToString(h.Sum(nil)) |
| 19 | } |
| 20 | |
| 21 | // VerifyOTPHash compares a candidate plaintext OTP against a stored HMAC |
| 22 | // digest in constant time. |
no outgoing calls