package file import ( "errors" "os" "os/user" "path" "path/filepath" "strings" ) var ErrNames = errors.New("please use naming scheme of keyname.pub and keyname.sec") const ( PubExt = ".pub" EncExt = ".sec" SigExt = ".sig" ) func splitNameExt(fname string) (string, string) { _, file := path.Split(fname) ext := filepath.Ext(file) return file[:len(file)-len(ext)], ext } func CheckNames(pubFile, encFile string) error { pubName, pubExt := splitNameExt(pubFile) encName, encExt := splitNameExt(encFile) if pubExt != PubExt || encExt != EncExt || pubName != encName { return ErrNames } return nil } func PubName(encFile string) string { ext := filepath.Ext(encFile) return filepath.Base(encFile[:len(ext)-1] + PubExt) } func SigName(msgFile string) string { return msgFile + SigExt } const verifyWith = "verify with " func PubFile(comment string) (string, bool) { if strings.HasPrefix(comment, verifyWith) { file := comment[len(verifyWith):] if strings.HasSuffix(file, PubExt) { return FindFile(file), true } } return "", false } func VerifyWith(encFile string) string { return verifyWith + PubName(encFile) } var safePath = []string{ "/etc/signify", "~/.signify", ".", // redundant, but keep it here for clarity } func FindFile(fname string) string { usr, _ := user.Current() for _, v := range safePath { p := path.Join(v, fname) if p[0] == '~' { p = path.Join(usr.HomeDir, p[1:]) } if _, err := os.Stat(p); err == nil { return p } } return fname }