You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.6 KiB
Go

8 months ago
package utils
import (
6 months ago
"crypto"
8 months ago
"crypto/md5"
6 months ago
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
8 months ago
"encoding/hex"
6 months ago
"encoding/pem"
"errors"
8 months ago
"fmt"
8 months ago
"golang.org/x/crypto/bcrypt"
8 months ago
"io"
"net/http"
"net/url"
6 months ago
"os"
8 months ago
"strings"
)
func Md5(str string) string {
hash := md5.New()
hash.Write([]byte(str))
bytes := hash.Sum(nil)
md5Str := hex.EncodeToString(bytes)
return md5Str
}
func Sign(params url.Values, signKey string) string {
fmt.Println(params.Encode() + "&signkey=" + signKey)
return Md5(params.Encode() + "&signkey=" + signKey)
}
8 months ago
func PasswordHash(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes), err
}
func PasswordVerify(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
}
8 months ago
func Post(url string, params url.Values) {
client := &http.Client{}
body := strings.NewReader(params.Encode())
request, err := http.NewRequest("POST", url, body)
request.Header.Set("content-type", "application/x-www-form-urlencoded")
if err != nil {
panic(err)
}
response, err := client.Do(request)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(response.Body)
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
fmt.Println(string(bodyBytes))
}
func Post1(url string, params url.Values) {
client := &http.Client{}
//reqBodyBytes, _ := json.Marshal(params)
//body := strings.NewReader(string(reqBodyBytes))
body := strings.NewReader(params.Encode())
request, err := http.NewRequest("POST", url, body)
request.SetBasicAuth("AUOi0TCR3m9_LGyxPGo15X2Nvvd6D-l8WTgTBMj39XqyzuFTwTPT3zxU11o0tWT8ouGZ4kFQv2mtmK7f", "EI7JYZQUOl4emunnuOMhCbqKFVFMxcO4Qk9ZctlrUu4h1Q_Gv_i-YDMhHv5Rt2J74lbEEPiuHb8Ot6eo")
request.Header.Set("content-type", "application/json")
if err != nil {
panic(err)
}
response, err := client.Do(request)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(response.Body)
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
fmt.Println(string(bodyBytes))
}
func Post2(url string, params string, token string) {
client := &http.Client{}
body := strings.NewReader(params)
request, err := http.NewRequest("POST", url, body)
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Accept", "application/json")
request.Header.Set("Authorization", "Bearer "+token)
if err != nil {
panic(err)
}
response, err := client.Do(request)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(response.Body)
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
panic(err)
}
fmt.Println(string(bodyBytes))
}
6 months ago
func RSASign(data []byte, filename string) (string, error) {
// 1、选择hash算法对需要签名的数据进行hash运算
6 months ago
myHash := crypto.SHA1
6 months ago
hashInstance := myHash.New()
hashInstance.Write(data)
hashed := hashInstance.Sum(nil)
6 months ago
6 months ago
// 2、读取私钥文件解析出私钥对象
privateKey, err := ReadParsePrivateKey(filename)
6 months ago
fmt.Println("signErr1", err)
6 months ago
if err != nil {
return "", err
}
// 3、RSA数字签名参数是随机数、私钥对象、哈希类型、签名文件的哈希串生成bash64编码
bytes, err := rsa.SignPKCS1v15(rand.Reader, privateKey, myHash, hashed)
6 months ago
fmt.Println("signErr2", err)
6 months ago
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(bytes), nil
}
func RSAVerify(data []byte, base64Sig, filename string) error {
// 1、对base64编码的签名内容进行解码返回签名字节
bytes, err := base64.StdEncoding.DecodeString(base64Sig)
if err != nil {
return err
}
// 2、选择hash算法对需要签名的数据进行hash运算
6 months ago
myHash := crypto.SHA1
6 months ago
hashInstance := myHash.New()
hashInstance.Write(data)
hashed := hashInstance.Sum(nil)
// 3、读取公钥文件解析出公钥对象
publicKey, err := ReadParsePublicKey(filename)
if err != nil {
return err
}
// 4、RSA验证数字签名参数是公钥对象、哈希类型、签名文件的哈希串、签名后的字节
return rsa.VerifyPKCS1v15(publicKey, myHash, hashed, bytes)
}
// ReadParsePublicKey 读取公钥文件,解析公钥对象
func ReadParsePublicKey(filename string) (*rsa.PublicKey, error) {
// 1、读取公钥文件获取公钥字节
publicKeyBytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
// 2、解码公钥字节生成加密对象
block, _ := pem.Decode(publicKeyBytes)
if block == nil {
return nil, errors.New("公钥信息错误!")
}
// 3、解析DER编码的公钥生成公钥接口
publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
// 4、公钥接口转型成公钥对象
publicKey := publicKeyInterface.(*rsa.PublicKey)
return publicKey, nil
}
// ReadParsePrivateKey 读取私钥文件,解析出私钥对象
func ReadParsePrivateKey(filename string) (*rsa.PrivateKey, error) {
// 1、读取私钥文件获取私钥字节
privateKeyBytes, err := os.ReadFile(filename)
6 months ago
fmt.Println(string(privateKeyBytes))
6 months ago
if err != nil {
return nil, err
}
// 2、解码私钥字节生成加密对象
block, _ := pem.Decode(privateKeyBytes)
6 months ago
6 months ago
if block == nil {
return nil, errors.New("私钥信息错误!")
}
6 months ago
fmt.Println("blockType", block.Type)
6 months ago
// 3、解析DER编码的私钥生成私钥对象
6 months ago
if block.Type == "RSA PRIVATE KEY" {
return x509.ParsePKCS1PrivateKey(block.Bytes)
}
privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
rsaKey, ok := privateKey.(*rsa.PrivateKey)
if ok {
return rsaKey, nil
} else {
return nil, errors.New("私钥错误")
6 months ago
}
}