Skip to content

Commit

Permalink
tharvik's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaylor Bosson committed Oct 28, 2019
1 parent ffd8249 commit 81867ec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
16 changes: 9 additions & 7 deletions ciphersuite/ciphersuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"golang.org/x/xerrors"
)

var sizeLength = 32 / 8
// encodedNameLengthSize defines the size in bytes of the name length
// when marshaling cipher data.
var encodedNameLengthSize = 32 / 8

// Name is the type that can differentiate multiple ciphers.
type Name = string
Expand Down Expand Up @@ -91,7 +93,7 @@ func (d *CipherData) WriteTo(w io.Writer) (n int64, err error) {
// it can be serialized in format such as TOML.
func (d *CipherData) MarshalText() ([]byte, error) {
name := []byte(d.Name())
size := make([]byte, sizeLength)
size := make([]byte, encodedNameLengthSize)
binary.LittleEndian.PutUint32(size, uint32(len(name)))

// Buffer starts with the size of the cipher suite name, then the name
Expand All @@ -112,17 +114,17 @@ func (d *CipherData) UnmarshalText(text []byte) error {
return xerrors.Errorf("decoding hex: %v", err)
}

if len(buf) < sizeLength {
if len(buf) < encodedNameLengthSize {
return xerrors.Errorf("data is too small")
}

size := int(binary.LittleEndian.Uint32(buf[:sizeLength]))
if len(buf) < sizeLength+size {
size := int(binary.LittleEndian.Uint32(buf[:encodedNameLengthSize]))
if len(buf) < encodedNameLengthSize+size {
return xerrors.Errorf("data is too small")
}

d.CipherName = string(buf[sizeLength : sizeLength+size])
d.Data = buf[sizeLength+size:]
d.CipherName = string(buf[encodedNameLengthSize : encodedNameLengthSize+size])
d.Data = buf[encodedNameLengthSize+size:]
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion ciphersuite/ciphersuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestCipherData_UnmarshalText(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "data is too small")

err = data.UnmarshalText(buf[:sizeLength*2+2])
err = data.UnmarshalText(buf[:encodedNameLengthSize*2+2])
require.Error(t, err)
require.Contains(t, err.Error(), "data is too small")
}
Expand Down
7 changes: 3 additions & 4 deletions ciphersuite/ed25519.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ciphersuite

import (
"encoding/hex"
"io"

"golang.org/x/crypto/ed25519"
Expand All @@ -26,7 +25,7 @@ func (pk *Ed25519PublicKey) Name() Name {
}

func (pk *Ed25519PublicKey) String() string {
return hex.EncodeToString(pk.data)
return pk.Raw().String()
}

// Raw returns the raw public key.
Expand All @@ -50,7 +49,7 @@ func (sk *Ed25519SecretKey) Name() Name {
}

func (sk *Ed25519SecretKey) String() string {
return hex.EncodeToString(sk.data)
return sk.Raw().String()
}

// Raw returns the raw secret key.
Expand All @@ -69,7 +68,7 @@ func (sig *Ed25519Signature) Name() Name {
}

func (sig *Ed25519Signature) String() string {
return hex.EncodeToString(sig.data)
return sig.Raw().String()
}

// Raw returns the raw signature.
Expand Down
6 changes: 3 additions & 3 deletions ciphersuite/ed25519_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestEd25519PublicKey(t *testing.T) {
publicKey := &Ed25519PublicKey{data: pk}
require.Equal(t, Ed25519CipherSuiteName, publicKey.Name())
require.NotNil(t, publicKey.Raw())
require.Equal(t, ed25519.PublicKeySize, len(publicKey.String())/2)
require.NotNil(t, publicKey.String())

suite := NewEd25519CipherSuite()
publicKey2, err := suite.PublicKey(publicKey.Raw())
Expand Down Expand Up @@ -43,7 +43,7 @@ func TestEd25519SecretKey(t *testing.T) {

require.Equal(t, Ed25519CipherSuiteName, secretKey.Name())
require.NotNil(t, secretKey.Raw())
require.Equal(t, ed25519.PrivateKeySize, len(secretKey.String())/2)
require.NotNil(t, secretKey.String())
}

func TestEd25519Signature(t *testing.T) {
Expand All @@ -54,7 +54,7 @@ func TestEd25519Signature(t *testing.T) {

require.Equal(t, Ed25519CipherSuiteName, signature.Name())
require.NotNil(t, signature.Raw())
require.Equal(t, ed25519.SignatureSize, len(signature.String())/2)
require.NotNil(t, signature.String())
}

func TestEd25519CipherSuite_BasicUsage(t *testing.T) {
Expand Down

0 comments on commit 81867ec

Please sign in to comment.