From: qydysky Date: Thu, 4 Mar 2021 20:45:05 +0000 (+0800) Subject: 108 X-Git-Tag: v0.4.11 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=671ea8423946a5a14657e9d14f01809fd583a9e2;p=part%2F.git 108 --- diff --git a/Reqf.go b/Reqf.go index af92265..f8dc9f7 100644 --- a/Reqf.go +++ b/Reqf.go @@ -216,6 +216,7 @@ func Cookies_String_2_Map(Cookies string) (o map[string]string) { list := strings.Split(Cookies, `; `) for _,v := range list { s := strings.SplitN(v, "=", 2) + if len(s) != 2 {continue} o[s[0]] = s[1] } return diff --git a/crypto/Crypto_test.go b/crypto/Crypto_test.go index 304e919..2339d77 100644 --- a/crypto/Crypto_test.go +++ b/crypto/Crypto_test.go @@ -2,7 +2,7 @@ package part import "testing" -func Test(t *testing.T){ +func Test_Crypto(t *testing.T){ var k Crypto if k.PubLoad() || k.PriLoad() {t.Error(`Keystatus not PublicKeyNoLoad`)} { diff --git a/crypto/EasyCrypt.go b/crypto/EasyCrypt.go new file mode 100644 index 0000000..5cc18f7 --- /dev/null +++ b/crypto/EasyCrypt.go @@ -0,0 +1,49 @@ +package part + +import ( + "bytes" + "errors" + p "github.com/qydysky/part" +) + +func Encrypt(source,pubKey []byte) ([]byte,error) { + var c Crypto + if e := c.GetPKIXPubKey(pubKey);e != nil{return []byte{},e} + + key := p.Stringf().Rand(2,32) + + var g Gcm + if e := g.Init(key);e != nil {return []byte{},e} + + if S_body,e := g.Encrypt(source);e != nil{ + return []byte{},e + } else if S_key,e := c.GetEncrypt([]byte(key));e != nil { + return []byte{},e + } else { + return append(S_key,append([]byte(` `),S_body...)...),nil + } +} + +func Decrypt(source,priKey []byte) ([]byte,error) { + var loc = -1 + if loc = bytes.Index(source, []byte(` `));loc == -1{ + return []byte{},errors.New(`not easyCrypt type`) + } + + S_key := source[:loc] + S_body := source[loc+2:] + + var c Crypto + if e := c.GetPKCS1PriKey(priKey);e != nil{return []byte{},e} + + var g Gcm + if key,e := c.GetDecrypt(S_key);e != nil { + return []byte{},e + } else if e := g.Init(string(key));e != nil { + return []byte{},e + } else if body,e := g.Decrypt(S_body);e != nil{ + return []byte{},e + } else { + return body,nil + } +} \ No newline at end of file diff --git a/crypto/EasyCrypt_test.go b/crypto/EasyCrypt_test.go new file mode 100644 index 0000000..88d99ac --- /dev/null +++ b/crypto/EasyCrypt_test.go @@ -0,0 +1,21 @@ +package part + +import ( + "testing" +) + +func Test_EasyCrypt(t *testing.T) { + + priKey,_ := FileLoad(`private.pem`) + pubKey,_ := FileLoad(`public.pem`) + + if sc,e := Encrypt([]byte(`asdfasdfasdf`),pubKey);e != nil { + t.Error(e) + } else if s,e := Decrypt(sc,priKey);e != nil { + t.Error(e) + } else { + if string(s) != `asdfasdfasdf` { + t.Error(`not match`) + } + } +} \ No newline at end of file diff --git a/crypto/Gcm.go b/crypto/Gcm.go new file mode 100644 index 0000000..9362e22 --- /dev/null +++ b/crypto/Gcm.go @@ -0,0 +1,45 @@ +package part + +import ( + "crypto/aes" + "crypto/cipher" + "crypto/rand" + "encoding/hex" + "io" + "errors" +) + +type Gcm struct { + aead *cipher.AEAD +} + +func (t *Gcm) Init(key string) (error) { + tkey,_ := hex.DecodeString(key) + block, err := aes.NewCipher(tkey[:32]) + if err != nil { + return err + } + + if aesgcm, err := cipher.NewGCM(block);err != nil { + return err + } else { + t.aead = &aesgcm + } + return nil +} + +func (t *Gcm) Encrypt(source []byte) (r []byte,e error) { + if t.aead == nil {return []byte{},errors.New(`Encrypt not init`)} + + nonce := make([]byte, 12) + if _, err := io.ReadFull(rand.Reader, nonce); err != nil { + return []byte{},err + } + + return append(nonce,(*t.aead).Seal(nil, nonce, source, nil)...),nil +} + +func (t *Gcm) Decrypt(source []byte) (r []byte,e error) { + if t.aead == nil {return []byte{},errors.New(`Decrypt not init`)} + return (*t.aead).Open(nil, source[:12], source[12:], nil) +} \ No newline at end of file diff --git a/crypto/Gcm_test.go b/crypto/Gcm_test.go new file mode 100644 index 0000000..2c3b3eb --- /dev/null +++ b/crypto/Gcm_test.go @@ -0,0 +1,26 @@ +package part + +import ( + "testing" +) + +func Test_GCM(t *testing.T){ + var s []byte + { + var gcm Gcm + if e := gcm.Init(`sdifw023jfa;oo`);e != nil{ + t.Error(e) + } else if s,e = gcm.Encrypt([]byte(`12345`));e != nil{ + t.Error(e) + } + } + + var gcm Gcm + if e := gcm.Init(`sdifw023jfa;oo`);e != nil{ + t.Error(e) + } else if ss,e := gcm.Decrypt(s);e != nil{ + t.Error(e) + } else { + if string(ss) != `12345` {t.Error(string(ss))} + } +} \ No newline at end of file