Files
BitSrunLoginGo/util/util.go
2021-09-19 22:47:53 +08:00

48 lines
919 B
Go

package util
import (
"crypto/md5"
"crypto/sha1"
"errors"
"fmt"
"io"
"regexp"
)
func Search(reg string, content string) (string, error) {
r := regexp.MustCompile(reg)
if r == nil {
return "", errors.New("解析正则表达式失败")
}
if s := r.FindStringSubmatch(content); len(s) < 2 {
return "", errors.New("无匹配")
} else {
return s[1], nil
}
}
func GetIp(body string) (string, error) {
return Search("id=\"user_ip\" value=\"(.*?)\"", body)
}
func GetToken(body string) (string, error) {
return Search("\"challenge\":\"(.*?)\"", body)
}
func GetResult(body string) (string, error) {
return Search("\"error\":\"(.+?)\"", body)
}
func Md5(content string) string {
w := md5.New()
_, _ = io.WriteString(w, content)
return fmt.Sprintf("%x", w.Sum(nil))
}
func Sha1(content string) string {
h := sha1.New()
h.Write([]byte(content))
bs := h.Sum(nil)
return fmt.Sprintf("%x\n", bs)
}