Skip to content

Commit

Permalink
fix(recovery): refine impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice52 committed Jan 14, 2024
1 parent db6ba3f commit 3c07008
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
8 changes: 4 additions & 4 deletions crypt/encryptor/default_encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (c *DefaultAES) GetConfig() config.Config {
}

func (c *DefaultAES) EncryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)
encrypted, err := c.Encrypt(message)
if err != nil {
return "", err
Expand All @@ -35,7 +35,7 @@ func (c *DefaultAES) EncryptWrapper(message string) (a string, err error) {
}

func (c *DefaultAES) Encrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

block, err := buildCipher(c.Password)
if err != nil {
Expand All @@ -57,7 +57,7 @@ func (c *DefaultAES) Encrypt(message string) (a string, err error) {
}

func (c *DefaultAES) DecryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)
if c.NeedDecrypt(message) {
s := len(c.Prefix)
e := len(message) - len(c.Suffix)
Expand All @@ -68,7 +68,7 @@ func (c *DefaultAES) DecryptWrapper(message string) (a string, err error) {
}

func (c *DefaultAES) Decrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

data, err := base64.StdEncoding.DecodeString(message)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions crypt/encryptor/encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type Encryptor interface {
DecryptWrapper(message string) (string, error)
}

func RecoveryPanicAsError(err error) {
defer func() {
func RecoveryPanicAsError() func(err error) {
return func(err error) {
if r := recover(); r != nil {
err = errors.New(fmt.Sprintf("recovered from panic: %v", r))
}
}()
}
}
8 changes: 4 additions & 4 deletions crypt/encryptor/pbe_aes_encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *PBEWithAES) GetConfig() config.Config {
}

func (c *PBEWithAES) EncryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

encrypted, err := c.Encrypt(message)
if err != nil {
Expand All @@ -40,7 +40,7 @@ func (c *PBEWithAES) EncryptWrapper(message string) (a string, err error) {
}

func (c *PBEWithAES) Encrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *PBEWithAES) Encrypt(message string) (a string, err error) {
}

func (c *PBEWithAES) DecryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

if c.NeedDecrypt(message) {
s := len(c.Prefix)
Expand All @@ -83,7 +83,7 @@ func (c *PBEWithAES) DecryptWrapper(message string) (a string, err error) {
}

func (c *PBEWithAES) Decrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize
Expand Down
2 changes: 1 addition & 1 deletion crypt/encryptor/pbe_aes_encryptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestAesEncryptor(t *testing.T) {

func TestAesDecryptWrapper(t *testing.T) {
encryptor := NewPBEWithAES(config.New())
decrypt, err := encryptor.DecryptWrapper("ENC(qMZtbG1zptyIVwwMERnR2eiBEeUufzsW6BPKQ+78kRJtr5IKhN0toIwvJY3QWipC)")
decrypt, err := encryptor.DecryptWrapper("ENC(+78kRJtr5IKhN0toIwvJY3QWipC)")
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions crypt/encryptor/pbe_des_encryptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewPBEWithDES(conf config.Config) *PBEWithDES {
}

func (c *PBEWithDES) EncryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

encrypted, err := c.Encrypt(message)
if err != nil {
Expand All @@ -38,7 +38,7 @@ func (c *PBEWithDES) EncryptWrapper(message string) (a string, err error) {
}

func (c *PBEWithDES) Encrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize
Expand Down Expand Up @@ -66,7 +66,7 @@ func (c *PBEWithDES) Encrypt(message string) (a string, err error) {
}

func (c *PBEWithDES) DecryptWrapper(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

if c.NeedDecrypt(message) {
s := len(c.Prefix)
Expand All @@ -78,7 +78,7 @@ func (c *PBEWithDES) DecryptWrapper(message string) (a string, err error) {
}

func (c *PBEWithDES) Decrypt(message string) (a string, err error) {
RecoveryPanicAsError(err)
defer RecoveryPanicAsError()(err)

saltGenerator, ivGenerator, password := c.SaltGenerator, c.IvGenerator, c.Password
_, _, koi, ab := c.Prefix, c.Suffix, c.keyObtainIterations, c.algorithmBlockSize
Expand Down
2 changes: 1 addition & 1 deletion viper/viper_jasypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func Unmarshal(v *viper.Viper, etor encryptor.Encryptor, rawVal any, opts ...viper.DecoderConfigOption) (err error) {
encryptor.RecoveryPanicAsError(err)
defer encryptor.RecoveryPanicAsError()(err)

jc := etor.GetConfig()
for _, k := range v.AllKeys() {
Expand Down

0 comments on commit 3c07008

Please sign in to comment.