diff --git a/utils/verify_code.go b/utils/verify_code.go new file mode 100644 index 0000000..7f3d7c4 --- /dev/null +++ b/utils/verify_code.go @@ -0,0 +1,36 @@ +package utils + +import ( + "math/rand" + "time" + "unsafe" +) + +const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + +var src = rand.NewSource(time.Now().UnixNano()) + +const ( + // 6 bits to represent a letter index + letterIdBits = 6 + // All 1-bits as many as letterIdBits + letterIdMask = 1<= 0; { + if remain == 0 { + cache, remain = src.Int63(), letterIdMax + } + if idx := int(cache & letterIdMask); idx < len(letters) { + b[i] = letters[idx] + i-- + } + cache >>= letterIdBits + remain-- + } + return *(*string)(unsafe.Pointer(&b)) +}