auth

package
v0.0.1-beta001 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	USER_ID       = contextKey("x-user-id")
	USER_NAME     = contextKey("x-user-name")
	AUTH_ERROR    = contextKey("x-auth-error")
	ACCESS_TOKEN  = contextKey("access-token")
	REFRESH_TOKEN = contextKey("refresh-token")
)

Variables

This section is empty.

Functions

func CheckPassword

func CheckPassword(hashedPassword, password string) error

CheckPassword 比对哈希 @param hashedPassword 存储的哈希 @param password 输入的密码 @return error 错误

func GenerateRandomString

func GenerateRandomString(length int) (string, error)

generateRandomString 生成随机字符串 length: 返回字符串长度

func GetUserID

func GetUserID(ctx context.Context) string

func GetUserName

func GetUserName(ctx context.Context) string

func HashPassword

func HashPassword(password string) (string, error)

HashPassword 加盐哈希 @param password 输入的密码 @return string 存储的哈希 @return error 错误

func NewContext

func NewContext(ctx context.Context, userID, userName string) context.Context

func SetUserID

func SetUserID(ctx context.Context, userID string) context.Context

func SetUserName

func SetUserName(ctx context.Context, userName string) context.Context

Types

type Sonyflake

type Sonyflake struct {
	// contains filtered or unexported fields
}

Sonyflake 是基于Sonyflake算法的分布式ID生成器 特点:

  • 分布式:支持在多个节点上生成唯一ID
  • 趋势递增:生成的ID总体趋势是递增的
  • 高性能:无锁设计,生成速度快
  • 时间有序:ID中包含时间戳信息,可按时间排序

ID结构:1位保留 + 39位时间戳 + 8位机器ID + 16位序列号 适用于分布式系统、数据库主键等场景

func NewSonyflake

func NewSonyflake() *Sonyflake

NewSonyflake 创建并返回一个新的Sonyflake实例 使用默认配置初始化Sonyflake生成器 默认配置使用内网IP地址作为机器ID的生成依据 返回:初始化好的Sonyflake实例指针

func (*Sonyflake) GenerateBase62

func (s *Sonyflake) GenerateBase62() (string, error)

生成的string递增

func (*Sonyflake) GenerateID

func (s *Sonyflake) GenerateID() (uint64, error)

GenerateID 生成一个唯一的分布式ID 如果ID生成过程中发生错误,会触发panic 返回:64位的唯一ID

func (*Sonyflake) GenerateIDString

func (s *Sonyflake) GenerateIDString() (string, error)

不保证递增

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL