쿠폰 헬퍼 함수 추가
This commit is contained in:
54
coupon/helper.go
Normal file
54
coupon/helper.go
Normal file
@ -0,0 +1,54 @@
|
||||
package coupon
|
||||
|
||||
import (
|
||||
"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) {
|
||||
roundbt := make([]byte, 8)
|
||||
copy(roundbt, []byte(strings.ToLower(name)))
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user