aboutsummaryrefslogtreecommitdiff
path: root/sig
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-05-01 17:01:09 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-05-01 17:01:09 +0200
commit4cc92b5f712abe7b9522a48b701a7af90eaa134b (patch)
tree010870686ebd22488cfe1c7ac848188edb797ce5 /sig
parenteae17147e143094e48050494b2da570f42d21986 (diff)
Rename sig package
Diffstat (limited to 'sig')
-rw-r--r--sig/sig.go93
-rw-r--r--sig/sig_test.go20
2 files changed, 0 insertions, 113 deletions
diff --git a/sig/sig.go b/sig/sig.go
deleted file mode 100644
index f0e00bc..0000000
--- a/sig/sig.go
+++ /dev/null
@@ -1,93 +0,0 @@
-// Package sig implements signify file format
-package sig
-
-import (
- "bytes"
- "encoding/base64"
- "errors"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "strings"
-)
-
-var ErrComment = errors.New("expected untrusted comment")
-
-// Block represents a encoded signify key or signature
-//
-// The encoded form is:
-// untrusted comment: comment
-// base64-encoded key
-// optional message
-type Block struct {
- Comment string
- Bytes []byte
- Message []byte
-}
-
-const untrusted = "untrusted comment:"
-
-func Encode(w io.Writer, b *Block) error {
- fmt.Fprintln(w, untrusted, b.Comment)
- fmt.Fprintln(w, base64.StdEncoding.EncodeToString(b.Bytes))
- w.Write(b.Message)
- return nil
-}
-
-func EncodeToMemory(b *Block) []byte {
- buf := new(bytes.Buffer)
- Encode(buf, b)
- return buf.Bytes()
-}
-
-func Decode(data []byte) (*Block, error) {
- r := bytes.NewBuffer(data)
- comment, err := r.ReadString('\n')
- if err != nil {
- return nil, err
- }
- if !strings.HasPrefix(comment, untrusted) {
- return nil, ErrComment
- }
- raw, err := r.ReadString('\n')
- if err != nil {
- return nil, err
- }
- b, err := base64.StdEncoding.DecodeString(raw)
- if err != nil {
- return nil, err
- }
- message, err := ioutil.ReadAll(r)
- if err != nil {
- return nil, err
- }
- return &Block{
- Comment: strings.TrimSpace(comment[len(untrusted):]),
- Bytes: b,
- Message: message,
- }, nil
-}
-
-func DecodeFile(fname string) (*Block, error) {
- body, err := ioutil.ReadFile(fname)
- if err != nil {
- return nil, err
- }
- return Decode(body)
-}
-
-const (
- SecMode os.FileMode = 0600
- PubMode os.FileMode = 0644
- SigMode os.FileMode = 0644
-)
-
-func EncodeFile(fname string, perm os.FileMode, b *Block) error {
- fd, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
- if err != nil {
- return err
- }
- defer fd.Close()
- return Encode(fd, b)
-}
diff --git a/sig/sig_test.go b/sig/sig_test.go
deleted file mode 100644
index 06be3c0..0000000
--- a/sig/sig_test.go
+++ /dev/null
@@ -1,20 +0,0 @@
-package sig
-
-import (
- "bytes"
- "testing"
-)
-
-func TestSig(t *testing.T) {
- b := &Block{
- Comment: "comment",
- Bytes: []byte{'t', 'e', 's', 't'},
- }
- b2, err := Decode(EncodeToMemory(b))
- if err != nil {
- t.Error(err)
- }
- if b.Comment != b2.Comment || !bytes.Equal(b.Bytes, b2.Bytes) {
- t.Errorf("got %v, want %v", b2, b)
- }
-}