uniugm/admin/lib/cdkey/cdkey.go
2025-05-07 15:03:19 +08:00

125 lines
3.2 KiB
Go
Raw 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 cdkey
/*
* 奖励码模块从神魔大陆java代码移植过来的
*/
func GenerateAll(batch int, totalCount int) []string {
keyList := make([]string, 0, totalCount)
for i := 0; i < totalCount; i++ {
codeKey := GenerateOne(batch, totalCount, i)
keyList = append(keyList, codeKey)
}
return keyList
}
func GenerateOne(batch int, totalCount int, curNo int) string {
base := MAXNUM / totalCount
numId := curNo
num := base + numId*base
mainKeyBitSet := newBitmap(MAIN_KEY_LENGTH, 0)
numKeyIndex := int((num + batch) % NUM_KEY_LENGTH_BINARY)
temp := newEncode(int64(num), numKeyIndex)
for i := 0; i < MAIN_KEY_NUM_LENGTH; i++ {
mainKeyBitSet.Set(i, (temp&1) == 1)
temp >>= 1
}
for i := 0; i < NUM_KEY_LENGTH; i++ {
mainKeyBitSet.Set(i+MAIN_KEY_NUM_LENGTH, toNumBitSet[numKeyIndex].Get(i))
}
var beCheckNum int64
if mainKeyBitSet.toInt64() > 0 {
beCheckNum = mainKeyBitSet.toInt64()
} else {
beCheckNum = -mainKeyBitSet.toInt64()
}
checkNum := int32(beCheckNum % 32)
checkNumBitSet := toBitSet32[checkNum]
for i := 0; i < CHECK_LENGTH; i++ {
mainKeyBitSet.Set(i+MAIN_KEY_NUM_LENGTH+NUM_KEY_LENGTH, checkNumBitSet.Get(i))
}
batchBitSet := newBitmap(BATCH_LENGTH, 0)
temp = int64(batch)
for i := 0; i < BATCH_LENGTH; i++ {
batchBitSet.Set(i, (temp&1) == 1)
temp >>= 1
}
batchKeyIndex := (num + batch) % BATCH_KEY_LENGTH_BINARY
batchStr := base32Encode(fakeRC4Encode(batchBitSet, batchKey[batchKeyIndex]))
temp1 := fakeRC4Encode(mainKeyBitSet, getMainKey(batchBitSet))
mainKeyStr := base32Encode(temp1)
cdKeyType := 1
typeIndex := (num + cdKeyType) % TYPE_LENGTH_BINARY
codeKey := mainKeyStr + batchStr + string(toBase32[batchKeyIndex]) + string(toBase32[typeIndex])
return shuffleCDKey(codeKey, batchKeyIndex)
}
type CDKeyInfo struct {
Batch int
Num int64
}
func DecodeCDKey(cdKey string) (*CDKeyInfo, bool) {
if len(cdKey) != CDKEY_LENGTH {
return nil, false
}
index := toBase32CToI[rune(cdKey[SHUFFLE_INDEX])]
cdKey1 := recoverCDKey(cdKey[0:SHUFFLE_INDEX], index)
if cdKey1 == "" {
return nil, false
}
typeCheck, find := toBase32CToI[rune(cdKey1[TYPE_INDEX])]
if !find {
return nil, false
}
batchKeyIndex, find := toBase32CToI[rune(cdKey1[BATCH_NUM_BEGIN])]
if !find {
return nil, false
}
if !(batchKeyIndex == index) {
return nil, false
}
batch := base32Decode(cdKey1[BATCH_BEGIN:BATCH_END])
if batch == nil {
return nil, false
}
batchBitSet := fakeRC4Encode(batch, batchKey[batchKeyIndex])
if batchBitSet == nil {
return nil, false
}
mainKey := base32Decode(cdKey1[MAIN_KEY_BEGIN:MAIN_KEY_END])
if mainKey == nil {
return nil, false
}
mainKey2 := getMainKey(batchBitSet)
if mainKey2 == nil {
return nil, false
}
mainKeyBitSet := fakeRC4Encode(mainKey, mainKey2)
if mainKeyBitSet == nil {
return nil, false
}
if !(check(mainKeyBitSet)) {
return nil, false
}
numKeyIndex := getNumKeyIndex(mainKeyBitSet)
numInt := numEncode(mainKeyBitSet.toInt64()&intFormat, numKeyIndex)
batchInt := int(batchBitSet.toInt64() & intFormat)
if !(checkIndex(numInt, batchInt, batchKeyIndex, numKeyIndex)) {
return nil, false
}
cdkeyInfo := &CDKeyInfo{
Batch: batchInt,
Num: numInt,
// Type: typeCheck,
}
if typeCheck > 0 {
}
return cdkeyInfo, true
}