Documentation
¶
Overview ¶
Package otp 提供了一次性密码(One-Time Password)的实现,支持 HOTP(HMAC-based OTP)和 TOTP(Time-based OTP)。
主要功能:
1. HOTP 实现:
- 基于 HMAC-SHA1 算法
- 支持自定义计数器
- 可配置密码长度
- 验证窗口设置
2. TOTP 实现:
- 基于时间戳
- 支持自定义时间步长
- 兼容 Google Authenticator
- 时间偏差处理
3. 密钥管理:
- 密钥生成
- Base32 编码
- 密钥验证
- 安全存储建议
基本用法:
1. HOTP 生成和验证:
// 生成 HOTP 密码
code, err := otp.GenerateHOTP(key, counter, 6)
if err != nil {
// 处理错误
}
// 验证 HOTP 密码
valid := otp.ValidateHOTP(code, key, counter, 6)
2. TOTP 生成和验证:
// 生成 TOTP 密码
code, err := otp.GenerateTOTP(key, time.Now(), 30)
if err != nil {
// 处理错误
}
// 验证 TOTP 密码
valid := otp.ValidateTOTP(code, key, time.Now(), 30)
安全特性:
1. 密钥保护:
- 安全的密钥生成
- 密钥格式验证
- 密钥长度检查
2. 验证机制:
- 防重放攻击
- 时间窗口控制
- 计数器同步
3. 错误处理:
- 安全的错误信息
- 完整的错误检查
- panic 保护机制
性能优化:
1. 内存使用:
- 避免不必要的内存分配
- 使用适当的缓冲区大小
- 及时释放资源
2. 计算优化:
- 高效的 HMAC 计算
- 优化的时间处理
- 减少内存拷贝
注意事项:
1. 密钥管理:
- 安全生成密钥
- 安全传输密钥
- 安全存储密钥
2. 时间同步:
- 服务器时间同步
- 客户端时间校准
- 处理时间偏差
3. 最佳实践:
- 使用足够长的密码
- 合理设置验证窗口
- 注意并发安全
Index ¶
- func GenerateURL(secretKeyBase32 string, options ...OneTimePasswordOption) string
- func NewOneTimePassword(secretKeyBase32 string, options ...OneTimePasswordOption) (*oneTimePassword, error)
- func VeryfyPassword(secretKeyBase32, password string, options ...OneTimePasswordOption) bool
- type OneTimePassword
- type OneTimePasswordOption
- func WithDigits(digits int) OneTimePasswordOption
- func WithIssuer(issuer string) OneTimePasswordOption
- func WithLabel(label string) OneTimePasswordOption
- func WithPeriodSeconds(periodSeconds int) OneTimePasswordOption
- func WithSHA256() OneTimePasswordOption
- func WithSHA512() OneTimePasswordOption
- func WithWindowSize(windowSize int) OneTimePasswordOption
- type OneTimePasswordOptionFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateURL ¶
func GenerateURL(secretKeyBase32 string, options ...OneTimePasswordOption) string
GenerateURL 生成对应的 URL 表示形式的字符串。
参数:
- secretKeyBase32:Base32 编码的密钥种子,用于生成一次性密码。
- options:可选的配置选项列表,用于自定义 OTP 行为。
返回:
- string:生成的 URL 字符串,可用于设置二维码等。
func NewOneTimePassword ¶
func NewOneTimePassword(secretKeyBase32 string, options ...OneTimePasswordOption) (*oneTimePassword, error)
NewOneTimePassword 创建一个新的一次性密码实例。
参数:
- secretKeyBase32:Base32 编码的密钥种子,用于生成一次性密码。
- options:可选的配置选项列表,用于自定义 OTP 行为。
返回:
- *oneTimePassword:创建的一次性密码实例。
- error:如果密钥解码失败,则返回相应的错误。
func VeryfyPassword ¶
func VeryfyPassword(secretKeyBase32, password string, options ...OneTimePasswordOption) bool
VeryfyPassword 验证密码是否在指定时间窗口内。
参数:
- secretKeyBase32:Base32 编码的密钥种子,用于生成一次性密码。
- password:需要验证的密码。
- options:可选的配置选项列表,用于自定义 OTP 行为。
返回:
- bool:如果密码有效,则返回 true,否则返回 false。
Types ¶
type OneTimePassword ¶
type OneTimePassword interface {
// Password 根据当前时间生成密码。
//
// 返回:
// - string:生成的一次性密码字符串。
// - error:如果生成过程中出现错误,则返回相应的错误。
Password() (string, error)
// EffectivePassword 根据当前时间,生成指定时间窗口内的所有密码。
//
// 返回:
// - []string:时间窗口内的所有有效密码字符串切片。
// - error:如果生成过程中出现错误,则返回相应的错误。
EffectivePassword() ([]string, error)
// VeryfyPassword 验证密码是否在指定时间窗口内。
//
// 参数:
// - password:需要验证的密码字符串。
//
// 返回:
// - bool:如果密码有效,则返回 true,否则返回 false。
VeryfyPassword(password string) bool
// GenerateURL 生成对应的 URL 表示形式的字符串。
//
// 返回:
// - string:生成的 URL 字符串,可用于设置二维码等。
GenerateURL() string
}
OneTimePassword 定义了一次性密码的接口。
type OneTimePasswordOption ¶
type OneTimePasswordOption interface {
// contains filtered or unexported methods
}
OneTimePasswordOption OTP 算法实例化时需要的选项。
func WithPeriodSeconds ¶
func WithPeriodSeconds(periodSeconds int) OneTimePasswordOption
WithPeriodSeconds 返回密码的有效期(单位为秒)选项。
func WithWindowSize ¶
func WithWindowSize(windowSize int) OneTimePasswordOption
WithWindowSize 返回时间窗口选项。
type OneTimePasswordOptionFunc ¶
type OneTimePasswordOptionFunc func(*oneTimePassword)
OneTimePasswordOptionFunc OTP 算法实例化时需要的选项的函数表示形式,实现了 OneTimePasswordOption 接口。
Click to show internal directories.
Click to hide internal directories.