Initial commit

This commit is contained in:
Tony Blyler 2018-06-11 17:14:22 -04:00
commit 2c9c1d865c
6 changed files with 544 additions and 0 deletions

16
signer/cleartext.go Normal file
View file

@ -0,0 +1,16 @@
package signer
type ClearText struct {
}
func NewClearText() *ClearText {
return new(ClearText)
}
func (c *ClearText) Encode(rawMsg []byte) ([]byte, error) {
return rawMsg, nil
}
func (c *ClearText) Decode(encMsg []byte) ([]byte, error) {
return encMsg, nil
}

7
signer/cryptsigner.go Normal file
View file

@ -0,0 +1,7 @@
package signer
// Signer implements encoding and decoding methods for messages
type Signer interface {
Encode(rawMsg []byte) ([]byte, error)
Decode(encMsg []byte) ([]byte, error)
}

56
signer/secretbox.go Normal file
View file

@ -0,0 +1,56 @@
package signer
import (
"crypto/rand"
"errors"
"io"
"golang.org/x/crypto/nacl/secretbox"
)
const (
NonceLength = 24
KeyLength = 32
)
type Secretbox struct {
key [KeyLength]byte
}
func NewSecretBox(key [KeyLength]byte) *Secretbox {
return &Secretbox{
key: key,
}
}
func (s *Secretbox) Encode(rawMsg []byte) (encMsg []byte, err error) {
nonce, err := generateNonce()
if err != nil {
return
}
encMsg = secretbox.Seal(nonce[:], rawMsg, &nonce, &s.key)
return
}
func (s *Secretbox) Decode(encMsg []byte) (msg []byte, err error) {
if len(encMsg) < NonceLength {
err = errors.New("Invalid message length for decode")
return
}
var nonce [NonceLength]byte
copy(nonce[:], encMsg[:NonceLength])
msg, ok := secretbox.Open(nil, encMsg[NonceLength:], &nonce, &s.key)
if !ok {
err = errors.New("Failed to decode message")
}
return
}
func generateNonce() (nonce [NonceLength]byte, err error) {
_, err = io.ReadFull(rand.Reader, nonce[:])
return nonce, err
}