Documentation
¶
Index ¶
- Constants
- Variables
- func EncryptLic(appInfoFile, key string)deprecated
- func EncryptLicSigned(appInfoFile, output, privateKeyPEM string) error
- func EncryptLicSignedUniversal(appInfoFile, output, privateKeyPEM string) error
- func EncryptLicToFile(appInfoFile, output, key string) error
- func GenerateSigningKeyPair() (privatePEM, publicPEM string, err error)
- func GetLicenseInfoFormatted(licenseFile, key string) (string, error)
- func GetLicenseInfoSignedFormatted(licFile, publicKeyPEM string) (string, error)
- func SetClockStatePath(path string)
- func ValidAppLic(appInfoFile, key string) (res bool, err error)
- func ValidAppLicSigned(licFile, publicKeyPEM string) (bool, error)
- type AppLicenseInfo
- type LicenseDisplayInfo
Constants ¶
const (
// LicenseAlgEd25519 v2 许可证使用的签名算法
LicenseAlgEd25519 = "Ed25519"
)
Variables ¶
var ( // ErrLicenseExpired 许可证已超过授权有效期 ErrLicenseExpired = errors.New("许可证已过期") // ErrDeviceMismatch 许可证绑定的设备与当前设备不匹配 ErrDeviceMismatch = errors.New("许可证不适用于此设备") )
var ( // ErrInvalidSignature 许可证签名验证失败(内容被篡改或公钥不匹配) ErrInvalidSignature = errors.New("许可证签名验证失败,文件可能被篡改") // ErrUnsupportedLicense 许可证算法或版本不受支持 ErrUnsupportedLicense = errors.New("许可证算法或版本不受支持") // ErrUniversalRequiresExpiry 通用许可证缺少到期时间(禁止签发永久通用许可证) ErrUniversalRequiresExpiry = errors.New("通用许可证必须设置到期时间") )
var ErrClockRollback = errors.New("检测到系统时间回拨,请校正系统时间后重试")
ErrClockRollback 检测到系统时间相对上次验证记录明显回拨
Functions ¶
func EncryptLic
deprecated
func EncryptLic(appInfoFile, key string)
EncryptLic 根据应用信息的配置文件生成license授权文件(固定输出 app.lic)。
Deprecated: 该函数固定输出 app.lic,且失败时以 log.Fatal 终止进程, 不适合在库代码中使用。请改用 EncryptLicToFile(可自定义输出路径并返回 error)。 对于新签发的许可证,建议使用 Ed25519 签名格式 EncryptLicSigned。
func EncryptLicSigned ¶
EncryptLicSigned 读取 JSON 授权配置文件,用 Ed25519 私钥对其签名并生成 v2 许可证文件。 签名覆盖配置文件的原始字节,验证端以文件内嵌的 payload 为准,不依赖重新序列化。
func EncryptLicSignedUniversal ¶ added in v1.2.1
EncryptLicSignedUniversal 签发通用许可证(ObjUUID 留空 = 不绑定设备,任何机器可验签)。 通用许可证不绑定设备、一旦外泄即全网可用,因此禁止签发永久通用许可证: LimitedTime 必填,到期后必须重新签发。调用方可在其之上叠加更严格的产品规则 (如有效期上限),本库只保证「通用必有过期时间」这一安全底线。 通用许可证不做设备绑定判定,验证侧请配合 ParseSignedLicenseFile/Content 实现自定义校验;通过 ValidAppLicSigned 验证会因 ErrDeviceMismatch 被拒绝。
func EncryptLicToFile ¶
EncryptLicToFile 根据应用信息的配置文件生成license授权文件,输出到指定路径
func GenerateSigningKeyPair ¶
GenerateSigningKeyPair 生成一对 Ed25519 密钥,返回 PEM 编码(PKCS#8 私钥、PKIX 公钥)。 私钥必须由签发方妥善保管,公钥内嵌到客户端用于验证许可证。
func GetLicenseInfoFormatted ¶
GetLicenseInfoFormatted 获取格式化的许可证信息字符串,用于直接显示(v1 AES对称加密格式)
func GetLicenseInfoSignedFormatted ¶
GetLicenseInfoSignedFormatted 获取 v2 签名许可证的格式化文本信息,用于直接显示。
func SetClockStatePath ¶ added in v1.2.1
func SetClockStatePath(path string)
SetClockStatePath 设置防回拨状态文件路径。 须在任何许可证验证之前调用(进程初始化期设置一次);非并发安全。 典型场景:容器部署(HOME 可能缺失导致默认路径为空、防护静默禁用)时 将状态文件指向持久卷;传空字符串为显式禁用回拨防护。
func ValidAppLic ¶
ValidAppLic 验证应用许可证(v1 AES对称加密格式)
func ValidAppLicSigned ¶
ValidAppLicSigned 验证 v2 签名许可证:先验签,再校验设备UUID与到期时间。
Types ¶
type AppLicenseInfo ¶
type AppLicenseInfo struct {
AppName string //应用名称
AppCompany string //应用发布的公司
AppUUID string //此次发布应用的UUID
ObjUUID string //目标设备的UUID
AuthorizedName string //授权名称
LimitedTime string //到期日期
LicenseID string //许可证ID(可选扩展字段)
LicenseQuantity int //许可证数量(可选扩展字段)
}
func ParseSignedLicenseContent ¶ added in v1.2.1
func ParseSignedLicenseContent(content []byte, publicKeyPEM string) (*AppLicenseInfo, error)
ParseSignedLicenseContent 验签并解析 v2 许可证内容([]byte), 不做设备/到期/回拨判定——供调用方实现自定义分级校验 (如通用授权跳过设备绑定、内嵌字符串授权等场景)。
func ParseSignedLicenseFile ¶ added in v1.2.1
func ParseSignedLicenseFile(licFile, publicKeyPEM string) (*AppLicenseInfo, error)
ParseSignedLicenseFile 验签并解析 v2 许可证文件, 不做设备/到期/回拨判定,用途同 ParseSignedLicenseContent。
type LicenseDisplayInfo ¶
type LicenseDisplayInfo struct {
AppName string `json:"app_name"` // 应用名称
AppCompany string `json:"app_company"` // 应用发布公司
AppUUID string `json:"app_uuid"` // 应用UUID
ObjUUID string `json:"obj_uuid"` // 目标设备UUID
AuthorizedName string `json:"authorized_name"` // 授权名称
LimitedTime string `json:"limited_time"` // 到期日期
LicenseID string `json:"license_id"` // 许可证ID(如果存在)
LicenseQuantity int `json:"license_quantity"` // 许可证数量(如果存在)
Status string `json:"status"` // 许可证状态:valid, expired, invalid
DaysRemaining int `json:"days_remaining"` // 剩余天数
IsValid bool `json:"is_valid"` // 是否有效
ErrorMessage string `json:"error_message"` // 错误信息(如果有)
}
LicenseDisplayInfo 用于显示的许可证信息结构
func GetLicenseInfo ¶
func GetLicenseInfo(licenseFile, key string) (*LicenseDisplayInfo, error)
GetLicenseInfo 获取许可证文件的详细信息,用于界面显示(v1 AES对称加密格式)
func GetLicenseInfoSigned ¶
func GetLicenseInfoSigned(licFile, publicKeyPEM string) (*LicenseDisplayInfo, error)
GetLicenseInfoSigned 获取 v2 签名许可证的详细信息,用于界面显示。 与 GetLicenseInfo 一样以结构体承载验证结果,验证过程本身出错时才返回 error。