Skip to content

Commit

Permalink
refactor: Replace elseif chains with switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
lubux committed Dec 7, 2023
1 parent 13f4eeb commit 57f73cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ linters-settings:
statements: 80
cyclop:
# the minimal code complexity to report
max-complexity: 22
max-complexity: 23
gocognit:
min-complexity: 45

Expand All @@ -19,7 +19,6 @@ issues:
- GetJsonSHA256Fingerprints should be GetJSONSHA256Fingerprints
- ST1003 # CamelCase variables; see constants/cipher.go
- missing output for example, go test can't validate it
- ifElseChain
- variable 'hasExpiredEntity' is only used in the if-statement
exclude-rules:
- path: crypto/crypto_example_test.go
Expand Down
12 changes: 6 additions & 6 deletions crypto/decryption_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ func (dh *decryptionHandle) DecryptDetached(pgpMessage []byte, encryptedDetached
// DecryptSessionKey decrypts an encrypted session key.
// To decrypted a session key, the decryption handle must contain either a decryption key or a password.
func (dh *decryptionHandle) DecryptSessionKey(keyPackets []byte) (sk *SessionKey, err error) {
if len(dh.Passwords) > 0 {
switch {
case len(dh.Passwords) > 0:
for _, passwordCandidate := range dh.Passwords {
sk, err = decryptSessionKeyWithPassword(keyPackets, passwordCandidate)
if err == nil {
return
return sk, nil
}
}
return
} else if dh.DecryptionKeyRing != nil {
return nil, err
case dh.DecryptionKeyRing != nil:
return decryptSessionKey(dh.DecryptionKeyRing, keyPackets)
} else {
return nil, errors.New("gopenpgp: no decryption key or password provided")
}
return nil, errors.New("gopenpgp: no decryption key or password provided")
}

// ClearPrivateParams clears all private key material contained in EncryptionHandle from memory.
Expand Down
18 changes: 9 additions & 9 deletions crypto/encryption_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ func (eh *encryptionHandle) Encrypt(message []byte) (*PGPMessage, error) {
func (eh *encryptionHandle) EncryptSessionKey(sessionKey *SessionKey) ([]byte, error) {
config := eh.profile.EncryptionConfig()
config.Time = NewConstantClock(eh.clock().Unix())
if eh.Password != nil {
switch {
case eh.Password != nil:
return encryptSessionKeyWithPassword(sessionKey, eh.Password, config)
} else if eh.Recipients != nil || eh.HiddenRecipients != nil {
case eh.Recipients != nil || eh.HiddenRecipients != nil:
encryptionTimeOverride := config.Now()
if eh.encryptionTimeOverride != nil {
encryptionTimeOverride = eh.encryptionTimeOverride()
}
return encryptSessionKey(eh.Recipients, eh.HiddenRecipients, sessionKey, encryptionTimeOverride, config)
} else {
return nil, errors.New("gopenpgp: no password or recipients in encryption handle")
}
return nil, errors.New("gopenpgp: no password or recipients in encryption handle")
}

// --- Helper methods on encryption handle
Expand Down Expand Up @@ -224,8 +224,8 @@ func (eh *encryptionHandle) encryptingWriters(keys, data, detachedSignature Writ
keys = data
}
}
if eh.Recipients.CountEntities() > 0 ||
eh.HiddenRecipients.CountEntities() > 0 {
switch {
case eh.Recipients.CountEntities() > 0 || eh.HiddenRecipients.CountEntities() > 0:
// Encrypt towards recipients
if !eh.DetachedSignature {
// Signature is inside the ciphertext.
Expand All @@ -234,21 +234,21 @@ func (eh *encryptionHandle) encryptingWriters(keys, data, detachedSignature Writ
// Encrypted detached signature separate from the ciphertext.
messageWriter, err = eh.encryptSignDetachedStreamToRecipients(meta, detachedSignature, data, keys)
}
} else if eh.Password != nil {
case eh.Password != nil:
// Encrypt with a password
if !eh.DetachedSignature {
messageWriter, err = eh.encryptStreamWithPassword(keys, data, meta)
} else {
messageWriter, err = eh.encryptSignDetachedStreamToRecipients(meta, detachedSignature, data, keys)
}
} else if eh.SessionKey != nil {
case eh.SessionKey != nil:
// Encrypt towards session key
if !eh.DetachedSignature {
messageWriter, err = eh.encryptStreamWithSessionKey(data, meta)
} else {
messageWriter, err = eh.encryptSignDetachedStreamWithSessionKey(meta, detachedSignature, data)
}
} else {
default:
// No encryption material provided
err = errors.New("gopenpgp: no encryption key ring, session key, or password provided")
}
Expand Down

0 comments on commit 57f73cf

Please sign in to comment.