@@ -160,12 +160,7 @@ func (cmd *AddCmd) Run(config *Config) error {
160160 if generate {
161161 password , err = generatePassword (cmd .Pattern , cmd .Length )
162162 } else {
163- var passwordBytes []byte
164- passwordBytes , err = input .ReadNewPassword (config .Confirm )
165- if err == nil {
166- defer pago .Zero (passwordBytes )
167- password = string (passwordBytes )
168- }
163+ password , err = input .ReadNewPassword (config .Confirm )
169164 }
170165 }
171166 if err != nil {
@@ -318,40 +313,40 @@ func englishPlural(singular, plural string, count int) string {
318313}
319314
320315// decryptEntry decrypts a password entry, using the agent if available and configured.
321- func decryptEntry (agentExecutable string , agentExpire time.Duration , agentMemlock bool , agentSocket , identities , passwordStore , name string ) ([] byte , error ) {
316+ func decryptEntry (agentExecutable string , agentExpire time.Duration , agentMemlock bool , agentSocket , identities , passwordStore , name string ) (string , error ) {
322317 if agentSocket == "" {
323318 // Agent is disabled, decrypt directly.
324319 return crypto .DecryptEntry (identities , passwordStore , name )
325320 }
326321
327322 file , err := pago .EntryFile (passwordStore , name )
328323 if err != nil {
329- return nil , err
324+ return "" , err
330325 }
331326
332327 encryptedData , err := os .ReadFile (file )
333328 if err != nil {
334- return nil , fmt .Errorf ("failed to read password file: %v" , err )
329+ return "" , fmt .Errorf ("failed to read password file: %v" , err )
335330 }
336331
337332 if err := agent .Ping (agentSocket ); err != nil {
338333 // If ping fails, attempt to start the agent.
339334 identitiesText , err := crypto .DecryptIdentities (identities )
340335 if err != nil {
341- return nil , err
336+ return "" , err
342337 }
343338
344339 if err := agent .StartProcess (agentExecutable , agentExpire , agentMemlock , agentSocket , identitiesText ); err != nil {
345- return nil , fmt .Errorf ("failed to start agent: %v" , err )
340+ return "" , fmt .Errorf ("failed to start agent: %v" , err )
346341 }
347342 }
348343
349344 content , err := agent .Decrypt (agentSocket , encryptedData )
350345 if err != nil {
351- return nil , err
346+ return "" , err
352347 }
353348
354- return content , nil
349+ return string ( content ) , nil
355350}
356351
357352// isTOML returns whether content is a TOML entry.
@@ -383,12 +378,10 @@ func generateOTP(otpURL string) (string, error) {
383378// getPassword decrypts an entry and returns its content, or a specific key's
384379// value if it's a TOML entry.
385380func getPassword (agentExecutable string , agentExpire time.Duration , agentMemlock bool , agentSocket , identities , passwordStore , name , key string ) (string , error ) {
386- contentBytes , err := decryptEntry (agentExecutable , agentExpire , agentMemlock , agentSocket , identities , passwordStore , name )
381+ content , err := decryptEntry (agentExecutable , agentExpire , agentMemlock , agentSocket , identities , passwordStore , name )
387382 if err != nil {
388383 return "" , err
389384 }
390- defer pago .Zero (contentBytes )
391- content := string (contentBytes )
392385
393386 if ! isTOML (content ) {
394387 if key != "" {
@@ -606,7 +599,7 @@ func (cmd *EditCmd) Run(config *Config) error {
606599
607600 if entryExists (config .Store , name ) {
608601 // Decrypt the existing entry content.
609- contentBytes , err : = decryptEntry (
602+ content , err = decryptEntry (
610603 config .AgentExecutable ,
611604 config .Expire ,
612605 config .Memlock ,
@@ -618,8 +611,6 @@ func (cmd *EditCmd) Run(config *Config) error {
618611 if err != nil {
619612 return err
620613 }
621-
622- content = string (contentBytes )
623614 } else if ! cmd .Force {
624615 return fmt .Errorf ("entry doesn't exist: %v" , name )
625616 }
@@ -742,13 +733,12 @@ func (cmd *InitCmd) Run(config *Config) error {
742733 var buf bytes.Buffer
743734 armorWriter := armor .NewWriter (& buf )
744735
745- passwordBytes , err := input .ReadNewPassword (config .Confirm )
736+ password , err := input .ReadNewPassword (config .Confirm )
746737 if err != nil {
747738 return fmt .Errorf ("failed to read password: %v" , err )
748739 }
749- defer pago .Zero (passwordBytes )
750740
751- recip , err := age .NewScryptRecipient (string ( passwordBytes ) )
741+ recip , err := age .NewScryptRecipient (password )
752742 if err != nil {
753743 return fmt .Errorf ("failed to create scrypt recipient: %w" , err )
754744 }
@@ -964,16 +954,15 @@ func (cmd *RewrapCmd) Run(config *Config) error {
964954 return err
965955 }
966956
967- newPasswordBytes , err := input .ReadNewPassword (config .Confirm )
957+ newPassword , err := input .ReadNewPassword (config .Confirm )
968958 if err != nil {
969959 return err
970960 }
971- defer pago .Zero (newPasswordBytes )
972961
973962 var buf bytes.Buffer
974963 armorWriter := armor .NewWriter (& buf )
975964
976- recip , err := age .NewScryptRecipient (string ( newPasswordBytes ) )
965+ recip , err := age .NewScryptRecipient (newPassword )
977966 if err != nil {
978967 return fmt .Errorf ("failed to create scrypt recipient: %w" , err )
979968 }
@@ -1005,12 +994,10 @@ func (cmd *RewrapCmd) Run(config *Config) error {
1005994
1006995// getTOMLKeys decrypts a TOML entry and returns a sorted list of its keys.
1007996func getTOMLKeys (agentExecutable string , agentExpire time.Duration , agentMemlock bool , agentSocket , identities , passwordStore , name string ) ([]string , error ) {
1008- contentBytes , err := decryptEntry (agentExecutable , agentExpire , agentMemlock , agentSocket , identities , passwordStore , name )
997+ content , err := decryptEntry (agentExecutable , agentExpire , agentMemlock , agentSocket , identities , passwordStore , name )
1009998 if err != nil {
1010999 return nil , err
10111000 }
1012- defer pago .Zero (contentBytes )
1013- content := string (contentBytes )
10141001
10151002 if ! isTOML (content ) {
10161003 return nil , fmt .Errorf ("%q is not a TOML entry; cannot list keys" , name )
0 commit comments