From b59a5ef85cfb883ccd1518d78b620a7494070934 Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 10 May 2022 19:10:34 +0800 Subject: [PATCH] fix --- crypto/EasyCrypt.go | 51 +++++++++++++++++++++++------------------ strings/Strings.go | 4 ++-- strings/Strings_test.go | 4 ++-- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/crypto/EasyCrypt.go b/crypto/EasyCrypt.go index 5cc18f7..3502163 100644 --- a/crypto/EasyCrypt.go +++ b/crypto/EasyCrypt.go @@ -3,47 +3,54 @@ package part import ( "bytes" "errors" - p "github.com/qydysky/part" + + ps "github.com/qydysky/part/strings" ) -func Encrypt(source,pubKey []byte) ([]byte,error) { +func Encrypt(source, pubKey []byte) ([]byte, error) { var c Crypto - if e := c.GetPKIXPubKey(pubKey);e != nil{return []byte{},e} + if e := c.GetPKIXPubKey(pubKey); e != nil { + return []byte{}, e + } - key := p.Stringf().Rand(2,32) + key := ps.Rand(ps.UppNumber, 32) var g Gcm - if e := g.Init(key);e != nil {return []byte{},e} + 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 + 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 + return append(S_key, append([]byte(` `), S_body...)...), nil } } -func Decrypt(source,priKey []byte) ([]byte,error) { +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`) + 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} + 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 + 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 + return body, nil } -} \ No newline at end of file +} diff --git a/strings/Strings.go b/strings/Strings.go index 6838d2a..285e715 100644 --- a/strings/Strings.go +++ b/strings/Strings.go @@ -10,8 +10,8 @@ import ( const ( Number RandType = 0 - lowNumber RandType = 1 - uppNumber RandType = 2 + LowNumber RandType = 1 + UppNumber RandType = 2 ) type RandType int diff --git a/strings/Strings_test.go b/strings/Strings_test.go index 2d070f9..547194b 100644 --- a/strings/Strings_test.go +++ b/strings/Strings_test.go @@ -6,8 +6,8 @@ import ( func Test_Rand(t *testing.T) { t.Log(Rand(Number, 14)) - t.Log(Rand(lowNumber, 14)) - t.Log(Rand(uppNumber, 14)) + t.Log(Rand(LowNumber, 14)) + t.Log(Rand(UppNumber, 14)) } func Test_UnescapeUnicode(t *testing.T) { -- 2.39.2