| 42 | } |
| 43 | |
| 44 | func runEncrypt() { |
| 45 | // Check if env var is already set — skip interactive prompt |
| 46 | if os.Getenv("TRYSSH_MASTER_KEY") != "" { |
| 47 | executeEncrypt() |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | // Interactive: prompt for password twice |
| 52 | pwd1, err := readPasswordFn("Enter master password: ") |
| 53 | if err != nil { |
| 54 | fatalFn("Failed to read password:", err) |
| 55 | return |
| 56 | } |
| 57 | if len(pwd1) == 0 { |
| 58 | fatalFn("Password cannot be empty") |
| 59 | return |
| 60 | } |
| 61 | if len(pwd1) < 4 { |
| 62 | fatalFn("Password must be at least 4 characters") |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | pwd2, err := readPasswordFn("Confirm master password: ") |
| 67 | if err != nil { |
| 68 | fatalFn("Failed to read password:", err) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | if string(pwd1) != string(pwd2) { |
| 73 | fatalFn("Passwords do not match") |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | // Set env var so GetMasterKey picks it up without another prompt |
| 78 | pwdStr := string(pwd1) |
| 79 | for i := range pwd1 { |
| 80 | pwd1[i] = 0 |
| 81 | } |
| 82 | for i := range pwd2 { |
| 83 | pwd2[i] = 0 |
| 84 | } |
| 85 | if err := os.Setenv("TRYSSH_MASTER_KEY", pwdStr); err != nil { |
| 86 | fatalFn("Failed to set master key:", err) |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | executeEncrypt() |
| 91 | } |
| 92 | |
| 93 | // executeEncrypt loads the config, encrypts plaintext passwords, and writes back. |
| 94 | func executeEncrypt() { |