From ea8ae4d02c7da1ab3470a0bc9b7470015775d046 Mon Sep 17 00:00:00 2001 From: mountain Date: Fri, 25 Aug 2023 10:55:01 +0900 Subject: [PATCH] =?UTF-8?q?=EC=BF=A0=ED=8F=B0=20=ED=97=AC=ED=8D=BC=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coupon/helper.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 coupon/helper.go diff --git a/coupon/helper.go b/coupon/helper.go new file mode 100644 index 0000000..768fe7b --- /dev/null +++ b/coupon/helper.go @@ -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 +}