| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package utils
- import (
- "crypto/md5"
- "crypto/rand"
- "fmt"
- "math/big"
- "net/url"
- "os"
- "path/filepath"
- "regexp"
- "strconv"
- )
- // VerifyPhone 验证手机号是否正确
- func VerifyPhone(phone string) bool {
- // 示例:中国大陆手机号(1开头,11位)
- reg := regexp.MustCompile(`^1[3-9]\d{9}$`)
- return reg.MatchString(phone)
- }
- // GetFourRandomDigits 获取4位随机数字
- func GetFourRandomDigits() string {
- // 生成 [0, 8999] 的安全随机数
- n, err := rand.Int(rand.Reader, big.NewInt(9000))
- if err != nil {
- return ""
- }
- // 转换为 1000~9999 范围
- randomNum := n.Int64() + 1000
- return strconv.FormatInt(randomNum, 10)
- }
- // Md5Sign 使用MD5加密
- func Md5Sign(str string) string {
- data := []byte(str) //切片
- has := md5.Sum(data)
- md5str := fmt.Sprintf("%x", has) //将[]byte转成16进制
- return md5str
- }
- // UrlEncode
- func UrlEncode(str string) string {
- return url.QueryEscape(str)
- }
- // LoadPrivateKeyWithPath 通过私钥的文件路径内容加载私钥
- func LoadPrivateKeyWithPath(path string) (privateKey string) {
- absPath, _ := filepath.Abs(path)
- privateKeyBytes, err := os.ReadFile(absPath)
- if err != nil {
- return ""
- }
- return string(privateKeyBytes)
- }
- // LoadPublicKeyWithPath 通过公钥的文件路径加载公钥
- func LoadPublicKeyWithPath(path string) (publicKey string) {
- absPath, _ := filepath.Abs(path)
- publicKeyBytes, err := os.ReadFile(absPath)
- if err != nil {
- return ""
- }
- return string(publicKeyBytes)
- }
|