From 79209ec625fa175e50a137413ada6ab2f73cbadf Mon Sep 17 00:00:00 2001 From: gouguoyin <245629560@qq.com> Date: Wed, 27 Nov 2024 16:47:10 +0800 Subject: [PATCH] Unified error handling --- aes.go | 4 ++-- aes_test.go | 26 ++++++++++---------------- blowfish.go | 4 ++-- blowfish_test.go | 20 ++++++++------------ decoder.go | 2 ++ des.go | 4 ++-- des_test.go | 22 ++++++++-------------- dongle.go | 2 +- ed25519.go | 4 ++-- ed25519_test.go | 15 +++++++-------- morse.go | 4 ++-- morse_test.go | 4 ++-- rc4.go | 4 ++-- rc4_test.go | 5 ++--- rsa.go | 3 ++- rsa_test.go | 4 ---- tea.go | 4 ++-- tea_test.go | 10 ++++------ 18 files changed, 60 insertions(+), 81 deletions(-) diff --git a/aes.go b/aes.go index 887faaf..81d3169 100644 --- a/aes.go +++ b/aes.go @@ -24,13 +24,14 @@ func (e AesError) IvError() error { return fmt.Errorf("aes: invalid iv, the iv size must be 16 bytes") } +var aesError = NewAesError() + // ByAes encrypts by aes. func (e Encrypter) ByAes(c *Cipher) Encrypter { if len(e.src) == 0 || e.Error != nil { return e } block, err := aes.NewCipher(c.key) - aesError := AesError{} if err != nil { e.Error = aesError.KeyError() return e @@ -53,7 +54,6 @@ func (d Decrypter) ByAes(c *Cipher) Decrypter { return d } block, err := aes.NewCipher(c.key) - aesError := AesError{} if err != nil { d.Error = aesError.KeyError() return d diff --git a/aes_test.go b/aes_test.go index 0281365..afa768e 100755 --- a/aes_test.go +++ b/aes_test.go @@ -170,48 +170,42 @@ func TestAes_Decrypt_Bytes(t *testing.T) { func TestAes_Key_Error(t *testing.T) { key, iv := []byte("xxxx"), aesIV - err := NewAesError() e := Encrypt.FromString("hello world").ByAes(getCipher(CBC, PKCS7, key, iv)) - assert.Equal(t, err.KeyError(), e.Error) + assert.Equal(t, aesError.KeyError(), e.Error) d := Decrypt.FromHexString("c1e9b4529aac9793010f4677f6358efe").ByAes(getCipher(CBC, PKCS7, key, iv)) - assert.Equal(t, err.KeyError(), d.Error) + assert.Equal(t, aesError.KeyError(), d.Error) } func TestAes_IV_Error(t *testing.T) { - err := NewAesError() key, iv := aesKey, []byte("xxxx") e := Encrypt.FromString("hello world").ByAes(getCipher(OFB, PKCS7, key, iv)) - assert.Equal(t, err.IvError(), e.Error) + assert.Equal(t, aesError.IvError(), e.Error) d := Decrypt.FromHexString("c1e9b4529aac9793010f4677f6358efec1e9b4529aac9793010f4677f6358efe").ByAes(getCipher(CBC, PKCS7, aesKey, iv)) - assert.Equal(t, err.IvError(), d.Error) + assert.Equal(t, aesError.IvError(), d.Error) } func TestAes_Src_Error(t *testing.T) { - err := NewAesError() - e := Encrypt.FromString("hello world").ByAes(getCipher(CFB, No, aesKey, aesIV)) - assert.Equal(t, err.SrcError(), e.Error) + assert.Equal(t, aesError.SrcError(), e.Error) d := Decrypt.FromHexString("68656c6c6f20776f726c64").ByAes(getCipher(CBC, No, aesKey, aesIV)) - assert.Equal(t, err.SrcError(), d.Error) + assert.Equal(t, aesError.SrcError(), d.Error) } func TestAes_Decoding_Error(t *testing.T) { - err := NewDecodeError() - d1 := Decrypt.FromHexString("xxxx").ByAes(getCipher(CTR, Zero, aesKey, aesIV)) - assert.Equal(t, err.ModeError("hex"), d1.Error) + assert.Equal(t, decodeError.ModeError("hex"), d1.Error) d2 := Decrypt.FromHexBytes([]byte("xxxx")).ByAes(getCipher(CTR, Zero, aesKey, aesIV)) - assert.Equal(t, err.ModeError("hex"), d2.Error) + assert.Equal(t, decodeError.ModeError("hex"), d2.Error) d3 := Decrypt.FromBase64String("xxxxxx").ByAes(getCipher(CFB, PKCS7, aesKey, aesIV)) - assert.Equal(t, err.ModeError("base64"), d3.Error) + assert.Equal(t, decodeError.ModeError("base64"), d3.Error) d4 := Decrypt.FromBase64Bytes([]byte("xxxxxx")).ByAes(getCipher(CFB, PKCS7, aesKey, aesIV)) - assert.Equal(t, err.ModeError("base64"), d4.Error) + assert.Equal(t, decodeError.ModeError("base64"), d4.Error) } // gets Cipher instance. diff --git a/blowfish.go b/blowfish.go index b54c6a6..7e60f5b 100644 --- a/blowfish.go +++ b/blowfish.go @@ -25,12 +25,13 @@ func (e BlowfishError) IvError(iv []byte) error { return fmt.Errorf("blowfish: invalid iv size %d, the iv size must be 8 bytes", len(iv)) } +var blowfishError = NewBlowfishError() + // ByBlowfish encrypts by blowfish. func (e Encrypter) ByBlowfish(c *Cipher) Encrypter { if len(e.src) == 0 || e.Error != nil { return e } - blowfishError := BlowfishError{} block, err := blowfish.NewCipher(c.key) if err != nil { e.Error = blowfishError.KeyError(c.key) @@ -53,7 +54,6 @@ func (d Decrypter) ByBlowfish(c *Cipher) Decrypter { if len(d.src) == 0 || d.Error != nil { return d } - blowfishError := NewBlowfishError() block, err := blowfish.NewCipher(c.key) if err != nil { d.Error = blowfishError.KeyError(c.key) diff --git a/blowfish_test.go b/blowfish_test.go index 709c050..05c4598 100755 --- a/blowfish_test.go +++ b/blowfish_test.go @@ -169,14 +169,13 @@ func TestBlowfish_Decrypt_Bytes(t *testing.T) { } func TestBlowfish_Key_Error(t *testing.T) { - err := NewBlowfishError() key, iv := []byte(""), blowfishIV e := Encrypt.FromString("hello world").ByBlowfish(getCipher(CBC, PKCS7, key, iv)) - assert.Equal(t, err.KeyError([]byte("")), e.Error) + assert.Equal(t, blowfishError.KeyError([]byte("")), e.Error) d := Decrypt.FromHexString("c1e9b4529aac9793010f4677f6358efe").ByBlowfish(getCipher(CBC, PKCS7, key, iv)) - assert.Equal(t, err.KeyError([]byte("")), d.Error) + assert.Equal(t, blowfishError.KeyError([]byte("")), d.Error) } func TestBlowfish_IV_Error(t *testing.T) { @@ -190,24 +189,21 @@ func TestBlowfish_IV_Error(t *testing.T) { } func TestBlowfish_Src_Error(t *testing.T) { - err := NewBlowfishError() e := Encrypt.FromString("hello world").ByBlowfish(getCipher(CFB, No, blowfishKey, blowfishIV)) - assert.Equal(t, err.SrcError(), e.Error) + assert.Equal(t, blowfishError.SrcError(), e.Error) d := Decrypt.FromHexString("68656c6c6f20776f726c64").ByBlowfish(getCipher(CBC, No, blowfishKey, blowfishIV)) - assert.Equal(t, err.SrcError(), d.Error) + assert.Equal(t, blowfishError.SrcError(), d.Error) } func TestBlowfish_Decoding_Error(t *testing.T) { - err := NewDecodeError() - d1 := Decrypt.FromHexString("xxxx").ByBlowfish(getCipher(CTR, Zero, blowfishKey, blowfishIV)) - assert.Equal(t, err.ModeError("hex"), d1.Error) + assert.Equal(t, decodeError.ModeError("hex"), d1.Error) d2 := Decrypt.FromHexBytes([]byte("xxxx")).ByBlowfish(getCipher(CTR, Zero, blowfishKey, blowfishIV)) - assert.Equal(t, err.ModeError("hex"), d2.Error) + assert.Equal(t, decodeError.ModeError("hex"), d2.Error) d3 := Decrypt.FromBase64String("xxxxxx").ByBlowfish(getCipher(CFB, PKCS7, blowfishKey, blowfishIV)) - assert.Equal(t, err.ModeError("base64"), d3.Error) + assert.Equal(t, decodeError.ModeError("base64"), d3.Error) d4 := Decrypt.FromBase64Bytes([]byte("xxxxxx")).ByBlowfish(getCipher(CFB, PKCS7, blowfishKey, blowfishIV)) - assert.Equal(t, err.ModeError("base64"), d4.Error) + assert.Equal(t, decodeError.ModeError("base64"), d4.Error) } diff --git a/decoder.go b/decoder.go index fbb0c28..2825f62 100644 --- a/decoder.go +++ b/decoder.go @@ -17,6 +17,8 @@ func (e DecodeError) ModeError(mode string) error { return fmt.Errorf("decode: invalid decoding mode, the src can't be decoded by %s", mode) } +var decodeError = NewDecodeError() + // newDecoder returns a new Decoder instance. func newDecoder() Decoder { return Decoder{} diff --git a/des.go b/des.go index d65783f..521af03 100644 --- a/des.go +++ b/des.go @@ -24,12 +24,13 @@ func (e DesError) IvError() error { return fmt.Errorf("des: invalid iv, the iv size must be 8 bytes") } +var desError = NewDesError() + // ByDes encrypts by des. func (e Encrypter) ByDes(c *Cipher) Encrypter { if len(e.src) == 0 || e.Error != nil { return e } - desError := NewDesError() block, err := des.NewCipher(c.key) if err != nil { e.Error = desError.KeyError() @@ -52,7 +53,6 @@ func (d Decrypter) ByDes(c *Cipher) Decrypter { if len(d.src) == 0 || d.Error != nil { return d } - desError := NewDesError() block, err := des.NewCipher(c.key) if err != nil { d.Error = desError.KeyError() diff --git a/des_test.go b/des_test.go index 964c42d..4612098 100755 --- a/des_test.go +++ b/des_test.go @@ -170,42 +170,36 @@ func TestDes_Decrypt_Bytes(t *testing.T) { func TestDes_Key_Error(t *testing.T) { key, iv := []byte("xxxx"), desIV - err := NewDesError() e := Encrypt.FromString("hello world").ByDes(getCipher(CBC, PKCS7, key, iv)) - assert.Equal(t, err.KeyError(), e.Error) + assert.Equal(t, desError.KeyError(), e.Error) d := Decrypt.FromHexString("0b2a92e81fb49ce1a43266aacaea7b81").ByDes(getCipher(CBC, PKCS7, key, iv)) - assert.Equal(t, err.KeyError(), d.Error) + assert.Equal(t, desError.KeyError(), d.Error) } func TestDes_IV_Error(t *testing.T) { key, iv := desKey, []byte("xxxx") - err := NewDesError() e := Encrypt.FromString("hello world").ByDes(getCipher(OFB, PKCS7, key, iv)) - assert.Equal(t, err.IvError(), e.Error) + assert.Equal(t, desError.IvError(), e.Error) d := Decrypt.FromHexString("0b2a92e81fb49ce1a43266aacaea7b81").ByDes(getCipher(CBC, PKCS7, key, iv)) - assert.Equal(t, err.IvError(), d.Error) + assert.Equal(t, desError.IvError(), d.Error) } func TestDes_Src_Error(t *testing.T) { - err := NewDesError() - e := Encrypt.FromString("hello world").ByDes(getCipher(CFB, No, desKey, desIV)) - assert.Equal(t, err.SrcError(), e.Error) + assert.Equal(t, desError.SrcError(), e.Error) d := Decrypt.FromHexString("68656c6c6f20776f726c64").ByDes(getCipher(CBC, No, desKey, desIV)) - assert.Equal(t, err.SrcError(), d.Error) + assert.Equal(t, desError.SrcError(), d.Error) } func TestDes_Decoding_Error(t *testing.T) { - err := NewDecodeError() - d1 := Decrypt.FromHexBytes([]byte("xxxx")).ByDes(getCipher(CTR, Zero, desKey, desIV)) - assert.Equal(t, err.ModeError("hex"), d1.Error) + assert.Equal(t, decodeError.ModeError("hex"), d1.Error) d2 := Decrypt.FromBase64Bytes([]byte("xxxxxx")).ByDes(getCipher(CFB, PKCS7, desKey, desIV)) - assert.Equal(t, err.ModeError("base64"), d2.Error) + assert.Equal(t, decodeError.ModeError("base64"), d2.Error) } diff --git a/dongle.go b/dongle.go index 457189e..ad3ccb7 100644 --- a/dongle.go +++ b/dongle.go @@ -13,7 +13,7 @@ import ( ) // Version current version -const Version = "1.0.1" +const Version = "1.0.2" // dongle defines a dongle struct. type dongle struct { diff --git a/ed25519.go b/ed25519.go index d61cc97..3f88f6a 100644 --- a/ed25519.go +++ b/ed25519.go @@ -32,6 +32,8 @@ const ( Base64 encodingMode = "base64" ) +var ed25519Error = NewEd25519Error() + // ByEd25519 signs by ed25519. func (s Signer) ByEd25519(privateKey []byte, mode encodingMode) Signer { if len(s.src) == 0 || s.Error != nil { @@ -42,7 +44,6 @@ func (s Signer) ByEd25519(privateKey []byte, mode encodingMode) Signer { s.Error = err return s } - ed25519Error := NewEd25519Error() if len(pri) != ed25519.PrivateKeySize { s.Error = ed25519Error.PrivateKeyError() return s @@ -61,7 +62,6 @@ func (v Verifier) ByEd25519(publicKey []byte, mode encodingMode) Verifier { v.Error = err return v } - ed25519Error := NewEd25519Error() if len(pub) != ed25519.PublicKeySize { v.Error = ed25519Error.PublicKeyError() return v diff --git a/ed25519_test.go b/ed25519_test.go index 99aaab8..5b6779a 100755 --- a/ed25519_test.go +++ b/ed25519_test.go @@ -95,13 +95,13 @@ func TestEd25519_Empty(t *testing.T) { func TestEd25519_PrivateKey_Error(t *testing.T) { key := []byte("xxxx") s := Sign.FromString(ed25519Input).ByEd25519(key, Raw) - assert.Equal(t, NewEd25519Error().PrivateKeyError(), s.Error) + assert.Equal(t, ed25519Error.PrivateKeyError(), s.Error) } func TestEd25519_PublicKey_Error(t *testing.T) { key := []byte("xxxx") s := Verify.FromBase64String("", ed25519Input).ByEd25519(key, Raw) - assert.Equal(t, NewEd25519Error().PublicKeyError(), s.Error) + assert.Equal(t, ed25519Error.PublicKeyError(), s.Error) } func TestEd25519_Signature_Error(t *testing.T) { @@ -109,20 +109,19 @@ func TestEd25519_Signature_Error(t *testing.T) { publicKey, _, _ = ed25519.GenerateKey(nil) s := Verify.FromRawString("xxxx", ed25519Input).ByEd25519(publicKey, Raw) - assert.Equal(t, NewEd25519Error().SignatureError(), s.Error) + assert.Equal(t, ed25519Error.SignatureError(), s.Error) } func TestEd25519_Decoding_Error(t *testing.T) { Key := []byte("xxxxxx") - err := NewDecodeError() s1 := Sign.FromString(ed25519Input).ByEd25519(Key, Hex) - assert.Equal(t, err.ModeError("hex"), s1.Error) + assert.Equal(t, decodeError.ModeError("hex"), s1.Error) v1 := Verify.FromHexString("68656c6c6f20776f726c64", ed25519Input).ByEd25519(Key, Hex) - assert.Equal(t, err.ModeError("hex"), v1.Error) + assert.Equal(t, decodeError.ModeError("hex"), v1.Error) s2 := Sign.FromString(ed25519Input).ByEd25519(Key, Base64) - assert.Equal(t, err.ModeError("base64"), s2.Error) + assert.Equal(t, decodeError.ModeError("base64"), s2.Error) v2 := Verify.FromBase64String("aGVsbG8gd29ybGQ=", ed25519Input).ByEd25519(Key, Base64) - assert.Equal(t, err.ModeError("base64"), v2.Error) + assert.Equal(t, decodeError.ModeError("base64"), v2.Error) } diff --git a/morse.go b/morse.go index a63a33d..6bac099 100644 --- a/morse.go +++ b/morse.go @@ -21,6 +21,8 @@ func (e MorseError) DecodeError() error { return fmt.Errorf("morse: failed to decode src") } +var morseError = NewMorseError() + // ByMorse encodes by morse. func (e Encoder) ByMorse(separator ...string) Encoder { if len(e.src) == 0 || e.Error != nil { @@ -31,7 +33,6 @@ func (e Encoder) ByMorse(separator ...string) Encoder { } dst, err := morse.Encode(e.src, separator[0]) if err != nil { - morseError := MorseError{} e.Error = morseError.SrcError() return e } @@ -49,7 +50,6 @@ func (d Decoder) ByMorse(separator ...string) Decoder { } dst, err := morse.Decode(d.src, separator[0]) if err != nil { - morseError := MorseError{} d.Error = morseError.DecodeError() return d } diff --git a/morse_test.go b/morse_test.go index 0de4528..99b2cab 100644 --- a/morse_test.go +++ b/morse_test.go @@ -62,10 +62,10 @@ func TestMorse_Decode_Bytes(t *testing.T) { func TestMorse_Src_Error(t *testing.T) { e := Encode.FromString("hello world").ByMorse() - assert.Equal(t, NewMorseError().SrcError(), e.Error) + assert.Equal(t, morseError.SrcError(), e.Error) } func TestMorse_Decoding_Error(t *testing.T) { e := Decode.FromString("hello world").ByMorse() - assert.Equal(t, NewMorseError().DecodeError(), e.Error) + assert.Equal(t, morseError.DecodeError(), e.Error) } diff --git a/rc4.go b/rc4.go index 2666739..d3103f0 100644 --- a/rc4.go +++ b/rc4.go @@ -16,12 +16,13 @@ func (e Rc4Error) KeyError() error { return fmt.Errorf("rc4: invalid key, the key at least 1 byte and at most 256 bytes") } +var rc4Error = NewRc4Error() + // ByRc4 encrypts by rc4. func (e Encrypter) ByRc4(key []byte) Encrypter { if len(e.src) == 0 || e.Error != nil { return e } - rc4Error := Rc4Error{} cipher, err := rc4.NewCipher(key) if err != nil { e.Error = rc4Error.KeyError() @@ -38,7 +39,6 @@ func (d Decrypter) ByRc4(key []byte) Decrypter { if len(d.src) == 0 || d.Error != nil { return d } - rc4Error := Rc4Error{} cipher, err := rc4.NewCipher(key) if err != nil { d.Error = rc4Error.KeyError() diff --git a/rc4_test.go b/rc4_test.go index 877a82b..9010ff2 100755 --- a/rc4_test.go +++ b/rc4_test.go @@ -76,11 +76,10 @@ func TestRc4_Decrypt_Bytes(t *testing.T) { func TestRc4_Key_Error(t *testing.T) { key := []byte("") - err := NewRc4Error() e := Encrypt.FromString("hello go").ByRc4(key) - assert.Equal(t, err.KeyError(), e.Error) + assert.Equal(t, rc4Error.KeyError(), e.Error) d := Decrypt.FromRawString("hello go").ByRc4(key) - assert.Equal(t, err.KeyError(), d.Error) + assert.Equal(t, rc4Error.KeyError(), d.Error) } diff --git a/rsa.go b/rsa.go index 61c5207..078029d 100644 --- a/rsa.go +++ b/rsa.go @@ -34,6 +34,8 @@ func (e RsaError) PrivateKeyError() error { return fmt.Errorf("rsa: invalid private key, please make sure the private key is valid") } +var rsaError = NewRsaError() + // ByRsa encrypts by rsa with public key or private key. func (e Encrypter) ByRsa(rsaKey []byte) Encrypter { if len(e.src) == 0 || e.Error != nil { @@ -59,7 +61,6 @@ func (d Decrypter) ByRsa(rsaKey []byte) Decrypter { keyPair := rsa.NewKeyPair() keyPair.SetPublicKey(rsaKey) keyPair.SetPrivateKey(rsaKey) - if keyPair.IsPublicKey() { d.dst, d.Error = keyPair.DecryptByPublicKey(d.src) return d diff --git a/rsa_test.go b/rsa_test.go index f079b46..55a7415 100644 --- a/rsa_test.go +++ b/rsa_test.go @@ -163,8 +163,6 @@ func TestRsa_PublicKey_Error(t *testing.T) { xxxx -----END PUBLIC KEY-----` - rsaError, decodeError := NewRsaError(), NewDecodeError() - e1 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa(invalidRsaKey) assert.Equal(t, rsaError.PublicKeyError(), e1.Error) e2 := Encrypt.FromBytes([]byte(rsaInput)).ByRsa([]byte(invalidPublicKey)) @@ -193,8 +191,6 @@ xxxx e := Encrypt.FromBytes([]byte(rsaInput)).ByRsa(pkcs1PublicKey) - rsaError := NewRsaError() - d1 := Decrypt.FromHexBytes(e.ToHexBytes()).ByRsa(invalidRsaKey) assert.Equal(t, rsaError.PrivateKeyError(), d1.Error) d2 := Decrypt.FromBase64Bytes(e.ToBase64Bytes()).ByRsa(invalidPrivateKey) diff --git a/tea.go b/tea.go index 46fc4da..58d9bca 100644 --- a/tea.go +++ b/tea.go @@ -22,6 +22,8 @@ func (e TeaError) RoundsError() error { return fmt.Errorf("tea: invalid rounds, the rounds must be even") } +var teaError = NewTeaError() + // ByTea encrypts by tea. func (e Encrypter) ByTea(key []byte, rounds ...int) Encrypter { if len(e.src) == 0 || e.Error != nil { @@ -31,7 +33,6 @@ func (e Encrypter) ByTea(key []byte, rounds ...int) Encrypter { // 64 is the standard number of rounds in tea. rounds = []int{64} } - teaError := NewTeaError() if rounds[0]&1 != 0 { e.Error = teaError.RoundsError() return e @@ -63,7 +64,6 @@ func (d Decrypter) ByTea(key []byte, rounds ...int) Decrypter { // 64 is the standard number of rounds in tea. rounds = []int{64} } - teaError := TeaError{} if rounds[0]&1 != 0 { d.Error = teaError.RoundsError() return d diff --git a/tea_test.go b/tea_test.go index 29b91c1..d833ccd 100755 --- a/tea_test.go +++ b/tea_test.go @@ -91,22 +91,20 @@ func TestTea_Decrypt_Bytes(t *testing.T) { func TestTea_Key_Error(t *testing.T) { key := []byte("xxx") - err := NewTeaError() e := Encrypt.FromString("hello go").ByTea(key, 8) - assert.Equal(t, err.KeyError(key), e.Error) + assert.Equal(t, teaError.KeyError(key), e.Error) d := Decrypt.FromRawString("hello go").ByTea(key, 8) - assert.Equal(t, err.KeyError(key), d.Error) + assert.Equal(t, teaError.KeyError(key), d.Error) } func TestTea_Rounds_Error(t *testing.T) { key := []byte("0123456789abcdefghijklmn") - err := NewTeaError() e := Encrypt.FromString("hello go").ByTea(key, 1) - assert.Equal(t, err.RoundsError(), e.Error) + assert.Equal(t, teaError.RoundsError(), e.Error) d := Decrypt.FromRawString("hello go").ByTea(key, 1) - assert.Equal(t, err.RoundsError(), d.Error) + assert.Equal(t, teaError.RoundsError(), d.Error) }