Documentation
¶
Overview ¶
Package otp provides functionalities for generating and validating HOTP(IETF RFC4226) and TOTP(IETF RFC6238) codes.
Index ¶
- Constants
- func DecodeBase32Secret(secret string) ([]byte, error)
- func DynamicTruncation(b []byte, d Digit) (int32, error)
- func GenerateBase32Secret(byteLen uint32, randomReader io.Reader) (string, error)
- func GenerateHOTPCode(secret []byte, counter uint64, d Digit) (int32, error)
- func GenerateHOTPCodeAlgo(a Algo, secret []byte, counter uint64, d Digit) (int32, error)
- func GenerateRandomSecret(byteLen uint32, randomReader io.Reader) ([]byte, error)
- func GenerateTOTPCode(secret []byte, t time.Time, d Digit) (int32, error)
- func GenerateTOTPCodeAlgo(a Algo, secret []byte, t time.Time, d Digit) (int32, error)
- func GetTOTPCounter(t time.Time, period int64) uint64
- func ValidateHOTPCode(code int32, secret []byte, counter uint64, d Digit) (bool, error)
- func ValidateHOTPCodeAlgo(a Algo, code int32, secret []byte, counter uint64, d Digit) (bool, error)
- func ValidateTOTPCode(passcode int32, secret []byte, t time.Time, d Digit) (bool, error)
- func ValidateTOTPCodeAlgo(a Algo, passcode int32, secret []byte, t time.Time, d Digit) (bool, error)
- func ZeroFill(code int32, d Digit) string
- type Algo
- type Digit
- type GoogleAuthKey
- type GoogleAuthKeyParam
- type Type
Constants ¶
const ( AlgoSHA1 = "sha1" AlgoSHA256 = "sha256" AlgoSHA512 = "sha512" )
Variables ¶
This section is empty.
Functions ¶
func DecodeBase32Secret ¶
DecodeBase32Secret decodes the encoded secret and returns its raw byte form to be used by the OTP functions
func DynamicTruncation ¶
DynamicTruncation as defined in the rfc4226#section-5.4
func GenerateBase32Secret ¶
GenerateBase32Secret generates an otp secret with length = `byteLen` encoded in base32. This is required for the otp to work with Google Authenticator. If the randomReader is nil, it will use the default crypto/rand.Reader to read the bytes from.
func GenerateHOTPCode ¶
GenerateHOTPCode calls GenerateHOTPCodeAlgo with algo = AlgoSHA1
func GenerateHOTPCodeAlgo ¶
GenerateHOTPCodeAlgo generates an otp code based on `secret` using HMAC-<Algo> hashing algorithm and `counter` as its moving factor.
func GenerateRandomSecret ¶
GenerateRandomSecret generate random bytes with length `byteLen` from the randomReader. If the randomReader is nil, it will use the default crypto/rand.Reader to read the bytes from.
func GenerateTOTPCode ¶
GenerateTOTPCode calls GenerateTOTPCodeAlgo with algo = AlgoSHA1
func GenerateTOTPCodeAlgo ¶
GenerateTOTPCodeAlgo generates an otp code based on `secret` using HMAC-<Algo> hashing algorithm and `floor(t/30)` as its moving factor.
func GetTOTPCounter ¶
GetTOTPCounter converts time t into a counter that can be used as the moving factor for the HOTP.
func ValidateHOTPCode ¶
ValidateHOTPCode calls ValidateHOTPCodeAlgo with algo = AlgoSHA1
func ValidateHOTPCodeAlgo ¶
ValidateHOTPCodeAlgo validates an otp code based on `secret` using HMAC-<Algo> hashing algorithm and `counter` as its moving factor.
func ValidateTOTPCode ¶
ValidateTOTPCode calls ValidateTOTPCodeAlgo with algo = AlgoSHA1
Types ¶
type Digit ¶
type Digit int
Digit define the type of the number of digit for the OTP. Currently the most common digits are 6 and 8.
type GoogleAuthKey ¶
type GoogleAuthKey struct {
GoogleAuthKeyParam
Secret string
}
GoogleAuthKey provides key that works with Google Authenticator or any clients that have a similar implementation.
func GenerateGoogleAuthKey ¶
func GenerateGoogleAuthKey(p GoogleAuthKeyParam) (GoogleAuthKey, error)
GenerateGoogleAuthKey generates a new usable key for Google Authenticator.
func (*GoogleAuthKey) String ¶
func (k *GoogleAuthKey) String() string
String encodes the key into a string url that can be shown as a QR code and then scanned by Google Authenticator.
type GoogleAuthKeyParam ¶
type GoogleAuthKeyParam struct {
// Issuer of the otp
Issuer string
// AccountName of the user
AccountName string
// Type of the OTP(hotp/totp)
Type Type
// SecretByteLength defines the length of bytes will be read from RandReader
SecretByteLength uint32
// RandReader is the reader to read random bytes from. If nil, then it will
// use the crypto/rand.Reader.
RandReader io.Reader
}
GoogleAuthKeyParam defines the structure of otp key supported by Google Authenticator App.