1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package main
import (
"errors"
"os"
"path"
"path/filepath"
"strings"
)
var ErrNames = errors.New("please use naming scheme of keyname.pub and keyname.sec")
const (
extPub = ".pub"
extSec = ".sec"
extSig = ".sig"
verifyWith = "verify with "
)
func splitNameExt(fname string) (string, string) {
_, file := path.Split(fname)
ext := filepath.Ext(file)
return file[:len(file)-len(ext)], ext
}
func ValidateNames(pubFile, secFile string) error {
pubName, pubExt := splitNameExt(pubFile)
secName, secExt := splitNameExt(secFile)
if pubExt != extPub || secExt != extSec || pubName != secName {
return ErrNames
}
return nil
}
func PubName(secFile string) string {
ext := filepath.Ext(secFile)
return filepath.Base(secFile[:len(ext)-1] + extPub)
}
func SigName(msgFile string) string {
return msgFile + extSig
}
func CommentPubFile(comment string) string {
if strings.HasPrefix(comment, verifyWith) {
file := comment[len(verifyWith):]
if strings.HasSuffix(file, extPub) {
return FindFile(file)
}
}
return ""
}
func VerifyWith(secFile string) string {
return verifyWith + PubName(secFile)
}
var safePath = []string{
"/etc/signify",
"$HOME/.signify",
}
// FindFile locates keys in safe path. Falls back to current dir.
func FindFile(fname string) string {
for _, v := range safePath {
p := path.Join(os.Expand(v, os.Getenv), fname)
if _, err := os.Stat(p); err == nil {
return p
}
}
return fname
}
|