package apis

import (
    "bytes"
    "github.com/gin-gonic/gin"
    "math/rand"
    "net/http"
    "time"
)

func GenRandomString(c *gin.Context) {
    c.String(http.StatusOK, gen(15))
}

func RandomGetSomeChar(str string,l int) string {
    bytes := []byte(str)
    result := []byte{}
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    for i := 0; i < l; i++ {
        result = append(result, bytes[r.Intn(len(bytes))])
    }
    return string(result)
}

func  gen(l int) string {
    base_num := "023456789"
    base_char_low := "abcdefghijkmnpqrstuvwxyz"
    base_char_high := "ABCDEFGHJKLMNOPQRSTUVWXYZ"
    base_sign_spec := "_"
    str := "023456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ_"
    var stringBuilder bytes.Buffer
    stringBuilder.WriteString(RandomGetSomeChar(base_sign_spec,1))
    stringBuilder.WriteString(RandomGetSomeChar(base_num,1))
    stringBuilder.WriteString(RandomGetSomeChar(base_char_low,1))
    stringBuilder.WriteString(RandomGetSomeChar(base_char_high,1))
    stringBuilder.WriteString(RandomGetSomeChar(str,l-4))
    str_result:=stringBuilder.String()
    arr_result:=[]byte(str_result)
    for i:=0;i<4;i++{
        num := rand.Intn(l-1)
        arr_result[i],arr_result[num]=arr_result[num],arr_result[i]
    }
    return string(arr_result)
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code