decryptConfig decrypts all encrypted fields in the config using the master key. Only prompts for the master password if encrypted content is detected.
(c *MainConfig)
| 67 | // decryptConfig decrypts all encrypted fields in the config using the master key. |
| 68 | // Only prompts for the master password if encrypted content is detected. |
| 69 | func decryptConfig(c *MainConfig) error { |
| 70 | hasEncrypted := false |
| 71 | for _, pwd := range c.Main.Passwords { |
| 72 | if utils.IsEncrypted(pwd) { |
| 73 | hasEncrypted = true |
| 74 | break |
| 75 | } |
| 76 | } |
| 77 | if !hasEncrypted { |
| 78 | for _, s := range c.ServerLists { |
| 79 | if utils.IsEncrypted(s.Password) { |
| 80 | hasEncrypted = true |
| 81 | break |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | if !hasEncrypted { |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | key, err := utils.GetMasterKey() |
| 90 | if err != nil || key == nil { |
| 91 | return fmt.Errorf("encrypted fields found but no master key provided") |
| 92 | } |
| 93 | |
| 94 | for i, pwd := range c.Main.Passwords { |
| 95 | if utils.IsEncrypted(pwd) { |
| 96 | decrypted, err := utils.Decrypt(pwd, key) |
| 97 | if err != nil { |
| 98 | return fmt.Errorf("failed to decrypt password[%d]: %w", i, err) |
| 99 | } |
| 100 | c.Main.Passwords[i] = decrypted |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | for i := range c.ServerLists { |
| 105 | if utils.IsEncrypted(c.ServerLists[i].Password) { |
| 106 | decrypted, err := utils.Decrypt(c.ServerLists[i].Password, key) |
| 107 | if err != nil { |
| 108 | return fmt.Errorf("failed to decrypt server cache password[%d]: %w", i, err) |
| 109 | } |
| 110 | c.ServerLists[i].Password = decrypted |
| 111 | } |
| 112 | } |
| 113 | return nil |
| 114 | } |
| 115 | |
| 116 | // encryptConfigForSave creates a copy with encrypted passwords for saving to disk. |
| 117 | // Uses the cached master key only — does not prompt interactively. |