list := strings.Split(Cookies, `; `)
for _,v := range list {
s := strings.SplitN(v, "=", 2)
+ if len(s) != 2 {continue}
o[s[0]] = s[1]
}
return
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`)}
{
--- /dev/null
+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
--- /dev/null
+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
--- /dev/null
+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
--- /dev/null
+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