improved base57 decode and tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package csgo
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"regexp"
|
||||
@@ -12,13 +12,12 @@ import (
|
||||
var DICT = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789"
|
||||
var sharecodeRexEx = regexp.MustCompile("^CSGO(?:-?[\\w]{5}){5}$")
|
||||
|
||||
func DecodeSharecode(code string) (uint64, uint64, uint32, error) {
|
||||
func DecodeSharecode(code string) (uint64, uint64, uint16, error) {
|
||||
if !sharecodeRexEx.MatchString(code) {
|
||||
return 0, 0, 0, fmt.Errorf("not a CSGO sharecode: %s", code)
|
||||
}
|
||||
|
||||
cleanCode := strings.ReplaceAll(code, "CSGO", "")
|
||||
cleanCode = strings.ReplaceAll(cleanCode, "-", "")
|
||||
cleanCode := strings.ReplaceAll(strings.ReplaceAll(code, "CSGO", ""), "-", "")
|
||||
|
||||
chars := ReverseString(strings.Split(cleanCode, ""))
|
||||
bigInt := new(big.Int)
|
||||
@@ -29,24 +28,17 @@ func DecodeSharecode(code string) (uint64, uint64, uint32, error) {
|
||||
bigInt.Add(bigInt, big.NewInt(int64(strings.Index(DICT, c))))
|
||||
}
|
||||
|
||||
if bigInt.BitLen() > 144 {
|
||||
return 0, 0, 0, fmt.Errorf("invalid sharecode")
|
||||
}
|
||||
bytes := make([]byte, 18)
|
||||
bigInt.FillBytes(bytes)
|
||||
|
||||
matchId := new(big.Int)
|
||||
matchId.SetString(hex.EncodeToString(Reverse(bytes[0:8])), 16)
|
||||
outcomeId := new(big.Int)
|
||||
outcomeId.SetString(hex.EncodeToString(Reverse(bytes[8:16])), 16)
|
||||
tokenId := new(big.Int)
|
||||
tokenId.SetString(hex.EncodeToString(Reverse(bytes[16:18])), 16)
|
||||
matchId := binary.LittleEndian.Uint64(bytes[0:8])
|
||||
outcomeId := binary.LittleEndian.Uint64(bytes[8:16])
|
||||
tokenId := binary.LittleEndian.Uint16(bytes[16:18])
|
||||
|
||||
return matchId.Uint64(), outcomeId.Uint64(), uint32(tokenId.Uint64()), nil
|
||||
}
|
||||
|
||||
func Reverse(numbers []byte) []byte {
|
||||
for i, j := 0, len(numbers)-1; i < j; i, j = i+1, j-1 {
|
||||
numbers[i], numbers[j] = numbers[j], numbers[i]
|
||||
}
|
||||
return numbers
|
||||
return matchId, outcomeId, tokenId, nil
|
||||
}
|
||||
|
||||
func ReverseString(numbers []string) []string {
|
||||
|
Reference in New Issue
Block a user