feat: 益铸 EntitleForge SDK 独立仓库首个版本
sdk-ci / test (push) Failing after 8s

- 自主仓 git.wxccs.org/EntitleForge/EntitleForge 拆分(协议实现单一归属:
  license/crypto/ulid 随 SDK 迁出,服务端反向依赖本仓)
- sdk:Client 三模式(文件/远程/代理)、GRACE 状态机、查询 API(6.6ns/0 alloc)
- license:三段式编解码与严格解析;crypto:Ed25519 信任链 + Proxy 受约束证书
- 零第三方依赖(CI 有依赖闭包断言红线);go.mod 零 require
- CI:Gitea Actions(test + 零依赖断言 + 三平台交叉编译冒烟)
This commit is contained in:
iceking2nd committed 2026-11-09 15:42:22 +08:00
commit 81c7295893
32 files changed
+3940

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
// Package ulid 实现 ULID(Universally Unique Lexicographically Sortable
// Identifier)生成:48bit 毫秒时间戳 + 80bit CSPRNG 随机,Crockford Base32
// 编码为 26 字符。规范参考 github.com/ulid/spec,此处原生实现避免引入依赖。
//
// 全项目对外 ID(tenant/license/activation)统一使用本包。
package ulid
import (
"crypto/rand"
"fmt"
"sync"
"time"
)
const crockford = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
// entropyMu 串行化随机读取,降低同毫秒碰撞下的重试竞争(rand 本身线程安全)。
var entropyMu sync.Mutex
// New 生成 26 字符 ULID。字典序即时间序。
func New() (string, error) {
var b [16]byte
ms := uint64(time.Now().UnixMilli())
// 48bit 时间戳大端序占 b[0:6](大端序字节拆分的有意截断)
b[0] = byte(ms >> 40) //nolint:gosec // G115
b[1] = byte(ms >> 32) //nolint:gosec // G115
b[2] = byte(ms >> 24) //nolint:gosec // G115
b[3] = byte(ms >> 16) //nolint:gosec // G115
b[4] = byte(ms >> 8) //nolint:gosec // G115
b[5] = byte(ms) //nolint:gosec // G115
entropyMu.Lock()
_, err := rand.Read(b[6:])
entropyMu.Unlock()
if err != nil {
return "", fmt.Errorf("ulid: %w", err)
}
return encode(b), nil
}
// encode 把 16 字节按 5bit 一组编码为 26 字符。
// 130bit 流 = 2 个前导零位 + 128bit 数据(数据右对齐,无尾部填充)。
func encode(b [16]byte) string {
var out [26]byte
const lead = 2 // 130 - 128
for i := range out {
out[i] = crockford[extract(b, i*5-lead, 5)]
}
return string(out[:])
}
// extract 从字节数组的 bitPos 位起取 n 位(MSB 优先,允许跨字节)。
// bitPos 为负时按前导零位处理。
func extract(b [16]byte, bitPos, n int) byte {
var v byte
for j := range n {
p := bitPos + j
v <<= 1
if p >= 0 {
if byteIdx := p / 8; byteIdx < len(b) {
v |= (b[byteIdx] >> (7 - p%8)) & 1
}
}
}
return v
}