MCPcopy
hub / github.com/rsc/2fa / hotp

Function hotp

main.go:331–341  ·  view source on GitHub ↗
(key []byte, counter uint64, digits int)

Source from the content-addressed store, hash-verified

329}
330
331func hotp(key []byte, counter uint64, digits int) int {
332 h := hmac.New(sha1.New, key)
333 binary.Write(h, binary.BigEndian, counter)
334 sum := h.Sum(nil)
335 v := binary.BigEndian.Uint32(sum[sum[len(sum)-1]&0x0F:]) & 0x7FFFFFFF
336 d := uint32(1)
337 for i := 0; i < digits && i < 8; i++ {
338 d *= 10
339 }
340 return int(v % d)
341}
342
343func totp(key []byte, t time.Time, digits int) int {
344 return hotp(key, uint64(t.UnixNano())/30e9, digits)

Callers 2

codeMethod · 0.85
totpFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected