Skip to content

Commit

Permalink
Unified error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Nov 27, 2024
1 parent 468d007 commit 79209ec
Show file tree
Hide file tree
Showing 18 changed files with 60 additions and 81 deletions.
4 changes: 2 additions & 2 deletions aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 10 additions & 16 deletions aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions blowfish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
20 changes: 8 additions & 12 deletions blowfish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
4 changes: 2 additions & 2 deletions des.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
22 changes: 8 additions & 14 deletions des_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion dongle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,34 +95,33 @@ 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) {
var publicKey []byte
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)
}
4 changes: 2 additions & 2 deletions morse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions morse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions rc4.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions rc4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading

0 comments on commit 79209ec

Please sign in to comment.