60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package coupon
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"math/rand"
|
|
"strings"
|
|
)
|
|
|
|
func DisolveCouponCode(code string) (round string, key string) {
|
|
var final []byte
|
|
for _, n := range strings.Split(code, "-") {
|
|
nb, err := hex.DecodeString(n)
|
|
if err != nil {
|
|
// 형식 오류
|
|
return "", ""
|
|
}
|
|
final = append(final, nb...)
|
|
}
|
|
|
|
if len(final) != 8 {
|
|
// 형식 오류
|
|
return "", ""
|
|
}
|
|
|
|
uid := final[4:]
|
|
left := binary.BigEndian.Uint16(uid[0:2])
|
|
right := binary.BigEndian.Uint16(uid[2:4])
|
|
|
|
final = final[0:4]
|
|
xor := binary.LittleEndian.Uint32(final)
|
|
roundhashnum := xor ^ (uint32(left) * uint32(right))
|
|
|
|
roundhash := make([]byte, 4)
|
|
binary.BigEndian.PutUint32(roundhash, roundhashnum)
|
|
|
|
round = hex.EncodeToString(roundhash)
|
|
key = hex.EncodeToString(uid)
|
|
|
|
return
|
|
}
|
|
|
|
func MakeCouponRoundHash(name string) (hash string, roundNumber uint32) {
|
|
m5 := md5.New()
|
|
m5.Write([]byte(name))
|
|
hashbt := m5.Sum(nil)
|
|
|
|
roundbt := make([]byte, 8)
|
|
copy(roundbt, hashbt[:8])
|
|
|
|
roundseed := int64(binary.BigEndian.Uint64(roundbt))
|
|
roundhash := make([]byte, 4)
|
|
rand.New(rand.NewSource(roundseed)).Read(roundhash)
|
|
roundNumber = binary.BigEndian.Uint32(roundhash)
|
|
hash = hex.EncodeToString(roundhash)
|
|
|
|
return
|
|
}
|