]> 127.0.0.1 Git - part/.git/commitdiff
108 v0.4.11
authorqydysky <qydysky@foxmail.com>
Thu, 4 Mar 2021 20:45:05 +0000 (04:45 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 4 Mar 2021 20:45:05 +0000 (04:45 +0800)
Reqf.go
crypto/Crypto_test.go
crypto/EasyCrypt.go [new file with mode: 0644]
crypto/EasyCrypt_test.go [new file with mode: 0644]
crypto/Gcm.go [new file with mode: 0644]
crypto/Gcm_test.go [new file with mode: 0644]

diff --git a/Reqf.go b/Reqf.go
index af92265a94c3b4c122c406704231e3a19d5bc0b4..f8dc9f7e4f9967b81f1a7c895cf6bce25da79b6e 100644 (file)
--- 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
index 304e91998b5d00426f484103a404fff8d75d5416..2339d779b698078fc3a1dd1c75906c5b99289400 100644 (file)
@@ -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 (file)
index 0000000..5cc18f7
--- /dev/null
@@ -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 (file)
index 0000000..88d99ac
--- /dev/null
@@ -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 (file)
index 0000000..9362e22
--- /dev/null
@@ -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 (file)
index 0000000..2c3b3eb
--- /dev/null
@@ -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