mirror of
https://github.com/fumiama/tienyik.git
synced 2026-06-12 19:58:37 +08:00
feat: add log & supporting packages
This commit is contained in:
29
api/auth/client.go
Normal file
29
api/auth/client.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/fumiama/tienyik/internal/hcli"
|
||||
"github.com/fumiama/tienyik/internal/hson"
|
||||
"github.com/fumiama/tienyik/internal/textio"
|
||||
)
|
||||
|
||||
type RequestNegotiationEncKey struct {
|
||||
CertData string `json:"certData"`
|
||||
CertType string `json:"certType"`
|
||||
Etype string `json:"etype"`
|
||||
}
|
||||
|
||||
type ResponseNegotiationEncKey struct {
|
||||
EncData string `json:"encData"`
|
||||
EncKey string `json:"encKey"`
|
||||
}
|
||||
|
||||
func NegotiationEncKey(r *RequestNegotiationEncKey) (*ResponseNegotiationEncKey, error) {
|
||||
resp, err := hcli.Post(textio.API(), bytes.NewReader(hson.Marshal(nil, r)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return hson.Unmarshal[*ResponseNegotiationEncKey](nil, resp.Body)
|
||||
}
|
||||
54
api/auth/client_test.go
Normal file
54
api/auth/client_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
|
||||
"github.com/fumiama/tienyik"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TestNegotiationEncKey(t *testing.T) {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
|
||||
k, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tyr := (*tienyik.RSA)(k)
|
||||
tyr.E = 0x010001
|
||||
|
||||
r, err := NegotiationEncKey(&RequestNegotiationEncKey{
|
||||
CertData: tyr.PublicKeyToSPKI(),
|
||||
CertType: tienyik.ETPYE_AES_CBC,
|
||||
Etype: tienyik.ETPYE_AES_CBC,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Logf("EncData: %s", r.EncData)
|
||||
t.Logf("EncKey: %s", r.EncKey)
|
||||
|
||||
v, err := base64.StdEncoding.DecodeString(r.EncKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
aesk, err := tyr.Decrypt(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(string(aesk))
|
||||
|
||||
v, err = base64.StdEncoding.DecodeString(r.EncData)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
v, err = tienyik.NewAES([]byte(aesk)).Decrypt(v)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(string(v))
|
||||
}
|
||||
Reference in New Issue
Block a user