124 lines
2.7 KiB
Go
124 lines
2.7 KiB
Go
package handwork
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"math"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"golang.org/x/crypto/argon2"
|
|
)
|
|
|
|
type HandResponse struct {
|
|
Type string `json:"type"`
|
|
Key string `json:"key"`
|
|
Secret string `json:"secret"`
|
|
}
|
|
|
|
func GenerateRandomString() string {
|
|
randomBytes := make([]byte, 16)
|
|
_, err := io.ReadFull(rand.Reader, randomBytes)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
encoded := base64.RawURLEncoding.EncodeToString(randomBytes)
|
|
result := encoded[:int(math.Min(float64(len(encoded)), 16))]
|
|
return result
|
|
}
|
|
|
|
func HashPassword(password string) (string, error) {
|
|
timeCost := uint32(5)
|
|
memoryCost := uint32(64 * 1024)
|
|
threads := uint8(8)
|
|
keyLength := uint32(48)
|
|
salt := make([]byte, 20)
|
|
if _, err := io.ReadFull(rand.Reader, salt); err != nil {
|
|
return "", err
|
|
}
|
|
hash := argon2.IDKey([]byte(password), salt, timeCost, memoryCost, threads, keyLength)
|
|
hashedHex := hex.EncodeToString(hash)
|
|
return fmt.Sprintf("%s:%s", hex.EncodeToString(salt), hashedHex), nil
|
|
}
|
|
|
|
func Handback(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Println("收到介入请求")
|
|
|
|
var ctx = context.Background()
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: memoryconf.Ip + memoryconf.Port,
|
|
Password: "",
|
|
DB: 0,
|
|
})
|
|
|
|
_, eror := client.Ping(context.Background()).Result()
|
|
if eror != nil {
|
|
fmt.Println("redis连接失败")
|
|
handresponse := HandResponse{
|
|
Type: "waiting",
|
|
Key: conf.Key,
|
|
}
|
|
|
|
hashjson, err := json.Marshal(handresponse)
|
|
if err != nil {
|
|
fmt.Println("返回请求失败")
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(hashjson)
|
|
return
|
|
}
|
|
|
|
handkey := GenerateRandomString()
|
|
|
|
error := client.Set(ctx, conf.Key, handkey, 300*time.Second)
|
|
if error != nil {
|
|
fmt.Println("写入失败")
|
|
handresponse := HandResponse{
|
|
Type: "waiting",
|
|
Key: conf.Key,
|
|
}
|
|
hashjson, err := json.Marshal(handresponse)
|
|
if err != nil {
|
|
fmt.Println("返回请求失败")
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(hashjson)
|
|
return
|
|
}
|
|
|
|
hashedPassword, err := HashPassword(handkey)
|
|
if err != nil {
|
|
fmt.Println("加密失败:", err)
|
|
handresponse := HandResponse{
|
|
Type: "waiting",
|
|
Key: conf.Key,
|
|
}
|
|
hashjson, err := json.Marshal(handresponse)
|
|
if err != nil {
|
|
fmt.Println("返回请求失败")
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(hashjson)
|
|
return
|
|
}
|
|
fmt.Println("加密后的哈希:", hashedPassword)
|
|
|
|
handresponse := HandResponse{
|
|
Type: "waiting",
|
|
Key: conf.Key,
|
|
Secret: handkey,
|
|
}
|
|
hashjson, err := json.Marshal(handresponse)
|
|
if err != nil {
|
|
fmt.Println("返回请求失败")
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Write(hashjson)
|
|
}
|