Files
iceking2nd 20945777af
sdk-ci / test (push) Successful in 2m17s
sdk-release / release (push) Successful in 57s
sdk-release / cleanup (push) Successful in 5s
fix(sdk): 修复审计确认的 SDK-01~SDK-06/SDK-08 缺陷
- SDK-01:时钟回拨补偿只做一次(水位线跟随回拨后的时钟),避免 trustedNow 重复膨胀
- SDK-02:代理请求显式传 proxyAddr,消除运行期改写共享 opts 的数据竞争
- SDK-03:Decode 增加 256KB 结构层上限(与服务端 license 字节流上限对齐)
- SDK-04:修正 license key 熵注释为 80bit
- SDK-05:兼容服务端 version=0 空 CRL(携带吊销条目仍拒绝)
- SDK-06:Validate 状态显式枚举,UNKNOWN/空值给出明确 fail-closed 错误
- SDK-08:缓存密钥混合 0600 随机 secret(旧 HKDF(指纹) 缓存读取后自动迁移)
- SDK-07:README 记录 proxycert/Proxy 公钥旁路分发的设计信任假设
- 新增对应回归测试,go test -race -shuffle=on 全绿
2026-11-24 21:14:55 +08:00

44 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package license
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
)
// keyAlphabet 是 license key 分组字符集(Crockford 去除易混淆的 I L O U)。
const keyAlphabet = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
// keyGroups/keyGroupLen 定义 key 形态:ef-9K2M-3N4Q-5R6T-V8XA。
const (
keyGroups = 4
keyGroupLen = 4
)
// GenerateLicenseKey 生成客户可见的激活凭据(80bit CSPRNG 有效熵,Crockford 编码分组)。
// 16 字符 × 5bit = 80bit;等同"兑换码",只存在于服务端与传输中,不写入 license 文件。
func GenerateLicenseKey() (string, error) {
raw := make([]byte, keyGroups*keyGroupLen)
if _, err := rand.Read(raw); err != nil {
return "", fmt.Errorf("license key: %w", err)
}
// 每字符 5bit:4*4*5 = 80bit 有效熵(首字符仅取随机高 5bit 的低 31 值域)
groups := make([]string, keyGroups)
for g := range groups {
chars := make([]byte, keyGroupLen)
for i := range chars {
idx := raw[g*keyGroupLen+i] % byte(len(keyAlphabet))
chars[i] = keyAlphabet[idx]
}
groups[g] = string(chars)
}
return fmt.Sprintf("ef-%s-%s-%s-%s", groups[0], groups[1], groups[2], groups[3]), nil
}
// LicenseKeyHash 计算 key 的落库摘要(DB 泄露不等于泄露可用 key)。
func LicenseKeyHash(key string) string {
sum := sha256.Sum256([]byte(key))
return "sha256:" + hex.EncodeToString(sum[:])
}