fix: resolve mnd and nestif linter errors
- Define base85 constants (base85ChunkSize, base85DigitCount, base85Base) - Replace magic numbers in base85 encoding logic with named constants - Add nolint:nestif comments for legitimate nested conditionals: - crypto.go: Clear separation between secret generation vs retrieval - secrets.go: Separate JSON and table output formatting logic - vault.go: Separate JSON and text output formatting logic
This commit is contained in:
parent
93a32217e0
commit
54fce0f187
@ -74,7 +74,7 @@ func (cli *Instance) Encrypt(secretName, inputFile, outputFile string) error {
|
|||||||
return fmt.Errorf("failed to check if secret exists: %w", err)
|
return fmt.Errorf("failed to check if secret exists: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !exists {
|
if !exists { //nolint:nestif // Clear conditional logic for secret generation vs retrieval
|
||||||
// Secret doesn't exist, generate new age key and store it
|
// Secret doesn't exist, generate new age key and store it
|
||||||
identity, err := age.GenerateX25519Identity()
|
identity, err := age.GenerateX25519Identity()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -214,7 +214,7 @@ func (cli *Instance) ListSecrets(cmd *cobra.Command, jsonOutput bool, filter str
|
|||||||
filteredSecrets = secrets
|
filteredSecrets = secrets
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput { //nolint:nestif // Separate JSON and table output formatting logic
|
||||||
// For JSON output, get metadata for each secret
|
// For JSON output, get metadata for each secret
|
||||||
secretsWithMetadata := make([]map[string]interface{}, 0, len(filteredSecrets))
|
secretsWithMetadata := make([]map[string]interface{}, 0, len(filteredSecrets))
|
||||||
|
|
||||||
|
@ -98,7 +98,7 @@ func (cli *Instance) ListVaults(cmd *cobra.Command, jsonOutput bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput { //nolint:nestif // Separate JSON and text output formatting logic
|
||||||
// Get current vault name for context
|
// Get current vault name for context
|
||||||
currentVault := ""
|
currentVault := ""
|
||||||
if currentVlt, err := vault.GetCurrentVault(cli.fs, cli.stateDir); err == nil {
|
if currentVlt, err := vault.GetCurrentVault(cli.fs, cli.stateDir); err == nil {
|
||||||
|
@ -355,24 +355,30 @@ func encodeBase85WithRFC1924Charset(data []byte) string {
|
|||||||
// RFC1924 character set
|
// RFC1924 character set
|
||||||
charset := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
|
charset := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"
|
||||||
|
|
||||||
|
const (
|
||||||
|
base85ChunkSize = 4 // Process 4 bytes at a time
|
||||||
|
base85DigitCount = 5 // Each chunk produces 5 digits
|
||||||
|
base85Base = 85 // Base85 encoding uses base 85
|
||||||
|
)
|
||||||
|
|
||||||
// Pad data to multiple of 4
|
// Pad data to multiple of 4
|
||||||
padded := make([]byte, ((len(data)+3)/4)*4)
|
padded := make([]byte, ((len(data)+base85ChunkSize-1)/base85ChunkSize)*base85ChunkSize)
|
||||||
copy(padded, data)
|
copy(padded, data)
|
||||||
|
|
||||||
var buf strings.Builder
|
var buf strings.Builder
|
||||||
buf.Grow(len(padded) * 5 / 4) // Each 4 bytes becomes 5 Base85 characters
|
buf.Grow(len(padded) * base85DigitCount / base85ChunkSize) // Each 4 bytes becomes 5 Base85 characters
|
||||||
|
|
||||||
// Process in 4-byte chunks
|
// Process in 4-byte chunks
|
||||||
for i := 0; i < len(padded); i += 4 {
|
for i := 0; i < len(padded); i += base85ChunkSize {
|
||||||
// Convert 4 bytes to uint32 (big-endian)
|
// Convert 4 bytes to uint32 (big-endian)
|
||||||
chunk := binary.BigEndian.Uint32(padded[i : i+4])
|
chunk := binary.BigEndian.Uint32(padded[i : i+base85ChunkSize])
|
||||||
|
|
||||||
// Convert to 5 base-85 digits
|
// Convert to 5 base-85 digits
|
||||||
digits := make([]byte, 5)
|
digits := make([]byte, base85DigitCount)
|
||||||
for j := 4; j >= 0; j-- {
|
for j := base85DigitCount - 1; j >= 0; j-- {
|
||||||
idx := chunk % 85
|
idx := chunk % base85Base
|
||||||
digits[j] = charset[idx]
|
digits[j] = charset[idx]
|
||||||
chunk /= 85
|
chunk /= base85Base
|
||||||
}
|
}
|
||||||
|
|
||||||
buf.Write(digits)
|
buf.Write(digits)
|
||||||
|
Loading…
Reference in New Issue
Block a user