passgo/impl_shared.go
2019-09-10 14:50:56 -04:00

77 lines
1.7 KiB
Go

//+build darwin !android,linux
package passgo
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path"
"strings"
"sync"
)
func (s *Store) nativeDecrypt(name string) (string, error) {
fmt.Println("calling gpg -d")
file := path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, ""))
if _, err := os.Stat(file); os.IsNotExist(err) {
return "", fmt.Errorf("Not in password store.")
}
output, err := exec.Command("gpg", "-d", file).Output()
if err != nil {
return "", fmt.Errorf("Error running gpg: %s", err)
}
return string(output[:len(output)-1]), nil
}
func (s *Store) nativeEncrypt(pw string) ([]byte, error) {
if s.Id == "" {
return nil, fmt.Errorf("No ID")
}
fmt.Printf("Calling gpg -e -r %s\n", s.Id)
cmd := exec.Command("gpg", "-e", "-r", s.Id)
cmd.Stdin = strings.NewReader(pw + "\n")
output, err := cmd.Output()
if err != nil {
fmt.Printf("Error running GPG: %s\n", err)
return nil, err
}
fmt.Println("success")
return output, nil
}
var (
idMux sync.Mutex
cachedIdentities []string
)
func nativeIdentities() ([]string, error) {
idMux.Lock()
defer idMux.Unlock()
if cachedIdentities != nil {
ret := make([]string, len(cachedIdentities))
copy(ret, cachedIdentities)
return ret, nil
}
ret := make([]string, 0)
fmt.Println("calling gpg")
output, err := exec.Command("gpg", "--list-secret-keys", "--with-colons").Output()
if err != nil {
return nil, fmt.Errorf("Error running gpg: %s", err)
}
scanner := bufio.NewScanner(bytes.NewBuffer(output))
for scanner.Scan() {
fs := strings.Split(scanner.Text(), ":")
if fs[0] == "uid" {
fmt.Printf("%s: %s\n", fs[0], fs[9])
ret = append(ret, fs[9])
}
}
if len(ret) > 0 {
cachedIdentities = ret
}
return ret, nil
}