쿠폰 헬퍼 함수 추가

This commit is contained in:
2023-08-25 10:55:01 +09:00
parent 8173216e95
commit ea8ae4d02c

54
coupon/helper.go Normal file
View 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
}