Browse Source

Work on using returned bytes as crypto keys

pull/1782/head
Ethan Frey 7 years ago
parent
commit
8220d59178
2 changed files with 66 additions and 0 deletions
  1. +19
    -0
      nano/sign.go
  2. +47
    -0
      nano/sign_test.go

+ 19
- 0
nano/sign.go View File

@ -5,6 +5,7 @@ import (
"fmt"
"github.com/pkg/errors"
crypto "github.com/tendermint/go-crypto"
)
const (
@ -57,3 +58,21 @@ func parseDigest(resp []byte) (key, sig []byte, err error) {
sig = resp[len(separator):]
return key, sig, nil
}
func parseKey(data []byte) (key crypto.PubKey, err error) {
ed := crypto.PubKeyEd25519{}
if len(data) < len(ed) {
return key, errors.Errorf("Key length too short: %d", len(data))
}
copy(ed[:], data)
return ed.Wrap(), nil
}
func parseSig(data []byte) (key crypto.Signature, err error) {
ed := crypto.SignatureEd25519{}
if len(data) < len(ed) {
return key, errors.Errorf("Sig length too short: %d", len(data))
}
copy(ed[:], data)
return ed.Wrap(), nil
}

+ 47
- 0
nano/sign_test.go View File

@ -49,3 +49,50 @@ func TestParseDigest(t *testing.T) {
}
}
}
type cryptoCase struct {
msg string
key string
sig string
valid bool
}
func toBytes(c cryptoCase) (msg, key, sig []byte, err error) {
msg, err = hex.DecodeString(c.msg)
if err != nil {
return
}
key, err = hex.DecodeString(c.key)
if err != nil {
return
}
sig, err = hex.DecodeString(c.sig)
return
}
func TestCryptoConvert(t *testing.T) {
assert, require := assert.New(t), require.New(t)
cases := []cryptoCase{
{
msg: "00",
key: "04338EB1DD3CCDEE1F6FB586F66E640F56FFDD14537A3F0ED9EEEDF10B528FE4195FD17AC9EDAE9718A50196A1459E2434C1E53F1238F4CFDF177FAFBA8B39249B",
sig: "FFDEA42A699205B217004E7E2FFB884E174A548D644116F4B20469CBC32F60A9CB0EEB5BB6A7F266BD0F6A0A99A45B4F18F0F477AED7C854C404EF43530DAB00",
valid: true,
},
}
for i, tc := range cases {
msg, key, sig, err := toBytes(tc)
require.Nil(err, "%d: %+v", i, err)
pk, err := parseKey(key)
require.Nil(err, "%d: %+v", i, err)
psig, err := parseSig(sig)
require.Nil(err, "%d: %+v", i, err)
// how do i make this valid?
valid := pk.VerifyBytes(msg, psig)
assert.Equal(tc.valid, valid, "%d", i)
}
}

Loading…
Cancel
Save