Some checks failed
Build docker and publish / linux (1.24.5) (push) Failing after 41s
新增随机种子生成与验证逻辑,包括: 1. 添加随机承诺生成接口 2. 实现抽奖执行与验证流程 3. 新增批量用户创建与删除功能 4. 添加抽奖收据记录表 5. 完善配置管理与错误码 新增测试用例验证随机算法正确性
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package activity
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
|
|
"bindbox-game/configs"
|
|
)
|
|
|
|
func masterKey() []byte {
|
|
s := configs.Get().Random.CommitMasterKey
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
b, err := hex.DecodeString(s)
|
|
if err != nil || len(b) == 0 {
|
|
return nil
|
|
}
|
|
return b
|
|
}
|
|
|
|
func deriveMask(key []byte, issueID int64, version int32) []byte {
|
|
m := hmac.New(sha256.New, key)
|
|
buf := make([]byte, 12)
|
|
binary.BigEndian.PutUint64(buf[:8], uint64(issueID))
|
|
binary.BigEndian.PutUint32(buf[8:12], uint32(version))
|
|
m.Write(buf)
|
|
sum := m.Sum(nil)
|
|
return sum[:32]
|
|
}
|
|
|
|
func maskSeed(master []byte, issueID int64, version int32) ([]byte, bool) {
|
|
key := masterKey()
|
|
if key == nil {
|
|
return master, false
|
|
}
|
|
ks := deriveMask(key, issueID, version)
|
|
out := make([]byte, len(master))
|
|
for i := range master {
|
|
out[i] = master[i] ^ ks[i%len(ks)]
|
|
}
|
|
return out, true
|
|
}
|
|
|
|
func unmaskSeed(enc []byte, issueID int64, version int32) []byte {
|
|
key := masterKey()
|
|
if key == nil {
|
|
return enc
|
|
}
|
|
ks := deriveMask(key, issueID, version)
|
|
out := make([]byte, len(enc))
|
|
for i := range enc {
|
|
out[i] = enc[i] ^ ks[i%len(ks)]
|
|
}
|
|
return out
|
|
} |