Skip to content

Commit

Permalink
Export everything and write comments everywhere
Browse files Browse the repository at this point in the history
That means there must be a lot of tests because of #7.
  • Loading branch information
Oskar Sharipov committed Jul 28, 2022
1 parent b56a9d9 commit 3214c48
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
8 changes: 4 additions & 4 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func New(trustedDir string) Base {
return Base{trustedDir: trustedDir}
}

func (b *Base) ensureTrustedDir() error {
func (b *Base) EnsureTrustedDir() error {
err := os.MkdirAll(b.trustedDir, trustedDirPerm)
if err != nil && !os.IsExist(err) {
return err
Expand All @@ -36,7 +36,7 @@ func (b *Base) getKeyPath(keyID [8]byte) string {

// SearchTrustedPubKey returns public key and untrusted comment.
func (b *Base) SearchTrustedPubKey(sigFile string) (minisign.PublicKey, string, error) {
if err := b.ensureTrustedDir(); err != nil {
if err := b.EnsureTrustedDir(); err != nil {
return minisign.PublicKey{}, "", errors.New("minitrust: can't create trusted directory.")
}

Expand All @@ -45,7 +45,7 @@ func (b *Base) SearchTrustedPubKey(sigFile string) (minisign.PublicKey, string,
return minisign.PublicKey{}, "", err
}

key, comment, err := readKeyFile(b.getKeyPath(signature.KeyId))
key, comment, err := ReadKeyFile(b.getKeyPath(signature.KeyId))
if err != nil {
return minisign.PublicKey{}, "", err
}
Expand All @@ -54,7 +54,7 @@ func (b *Base) SearchTrustedPubKey(sigFile string) (minisign.PublicKey, string,
}

func (b *Base) AddTrustedPubKey(rawPubKey, comment string) error {
if err := b.ensureTrustedDir(); err != nil {
if err := b.EnsureTrustedDir(); err != nil {
return errors.New("minitrust: can't create trusted directory.")
}
if strings.Count(comment, "\n") != 0 {
Expand Down
2 changes: 1 addition & 1 deletion base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestEnsureTrustedDir(t *testing.T) {
filepath.Join(dir, "exists"),
} {
b := New(test)
if err := b.ensureTrustedDir(); err != nil {
if err := b.EnsureTrustedDir(); err != nil {
t.Fatal(err)
}
_, err := os.Stat(test)
Expand Down
4 changes: 2 additions & 2 deletions cmd/minitrust/minitrust.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func verify(trustedDir, file, sigFile string) error {
return nil
}

func outputFile(filePath string) error {
file, err := os.Open(filePath)
func outputFile(readFrom string) error {
file, err := os.Open(readFrom)
if err != nil {
return err
}
Expand Down
11 changes: 7 additions & 4 deletions pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

const commentPrefix = "untrusted comment: "

// EncodePublicKey returns base64-coded public key.
func EncodePublicKey(pk minisign.PublicKey) string {
var bin [42]byte
copy(bin[:2], pk.SignatureAlgorithm[:])
Expand All @@ -36,12 +37,14 @@ func EncodePublicKey(pk minisign.PublicKey) string {
return base64.StdEncoding.EncodeToString(bin[:])
}

// EncodeID returns hex-coded public key ID.
func EncodeID(keyId [8]byte) string {
le64ID := binary.LittleEndian.Uint64(keyId[:])
return strings.ToUpper(strconv.FormatUint(le64ID, 16))
}

func decodeKeyFileContent(in string) (minisign.PublicKey, string, error) {
// DecodeKeyFileContent parses `in` and returns PublicKey and untrusted comment.
func DecodeKeyFileContent(in string) (minisign.PublicKey, string, error) {
lines := strings.SplitN(in, "\n", 2)
if len(lines) < 2 || !strings.HasPrefix(lines[0], commentPrefix) {
return minisign.PublicKey{}, "", errors.New("minitrust: incomplete encoded public key.")
Expand All @@ -54,13 +57,13 @@ func decodeKeyFileContent(in string) (minisign.PublicKey, string, error) {
return key, comment, nil
}

// readKeyFile reads from keyPath and returns public key with untrusted comment.
func readKeyFile(keyPath string) (minisign.PublicKey, string, error) {
// ReadKeyFile reads from keyPath and returns public key with untrusted comment.
func ReadKeyFile(keyPath string) (minisign.PublicKey, string, error) {
content, err := ioutil.ReadFile(keyPath)
if os.IsNotExist(err) {
return minisign.PublicKey{}, "", errors.New("minitrust: public key doesn't exist in trusted directory.")
} else if err != nil {
return minisign.PublicKey{}, "", err
}
return decodeKeyFileContent(string(content))
return DecodeKeyFileContent(string(content))
}

0 comments on commit 3214c48

Please sign in to comment.