aboutsummaryrefslogtreecommitdiff
path: root/file/names.go
blob: dd246179c49997e19e0fd1b77b79d958885b97cb (plain)
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
package file

import (
	"errors"
	"path/filepath"
	"strings"
)

var ErrNames = errors.New("please use naming scheme of keyname.pub and keyname.sec")

func Names(pubFile, encFile string) error {
	pubFile = filepath.Base(pubFile)
	encFile = filepath.Base(encFile)
	pubExt := filepath.Ext(pubFile)
	encExt := filepath.Ext(encFile)
	if pubExt != ".pub" || encExt != ".sec" || pubFile[:len(pubExt)-1] != encFile[:len(encExt)-1] {
		return ErrNames
	}
	return nil
}

func PubName(encFile string) string {
	ext := filepath.Ext(encFile)
	return filepath.Base(encFile[:len(ext)-1] + ".pub")
}

func SigName(msgFile string) string {
	return msgFile + ".sig"
}

const verifyWith = "verify with "

func PubFile(comment string) (string, bool) {
	if strings.HasPrefix(comment, verifyWith) {
		file := comment[len(verifyWith):]
		if strings.HasSuffix(file, ".pub") {
			return file, true
		}
	}
	return "", false
}

func VerifyWith(encFile string) string {
	return verifyWith + PubName(encFile)
}