Skip to content

Commit

Permalink
make ED25519PrivateKeyValue public, add a way to instantiate ed25519S…
Browse files Browse the repository at this point in the history
…igner from it (#213)

* make ED25519PrivateKeyValue public

* add a way to build a signer from the key value

* Update pkg/keys/ed25519.go

Co-authored-by: Ethan Lowman <53835328+ethan-lowman-dd@users.noreply.github.com>

Co-authored-by: Ethan Lowman <53835328+ethan-lowman-dd@users.noreply.github.com>
  • Loading branch information
arbll and ethan-lowman-dd committed Jan 27, 2022
1 parent b1554f8 commit 87caa18
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pkg/keys/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (e *ed25519Verifier) UnmarshalPublicKey(key *data.PublicKey) error {
return nil
}

type ed25519PrivateKeyValue struct {
type Ed25519PrivateKeyValue struct {
Public data.HexBytes `json:"public"`
Private data.HexBytes `json:"private"`
}
Expand Down Expand Up @@ -83,12 +83,21 @@ func GenerateEd25519Key() (*ed25519Signer, error) {
}, nil
}

func NewEd25519Signer(keyValue Ed25519PrivateKeyValue) *ed25519Signer {
return &ed25519Signer{
PrivateKey: ed25519.PrivateKey(data.HexBytes(keyValue.Private)),
keyType: data.KeyTypeEd25519,
keyScheme: data.KeySchemeEd25519,
keyAlgorithms: data.HashAlgorithms,
}
}

func (e *ed25519Signer) SignMessage(message []byte) ([]byte, error) {
return e.Sign(rand.Reader, message, crypto.Hash(0))
}

func (e *ed25519Signer) MarshalPrivateKey() (*data.PrivateKey, error) {
valueBytes, err := json.Marshal(ed25519PrivateKeyValue{
valueBytes, err := json.Marshal(Ed25519PrivateKeyValue{
Public: data.HexBytes([]byte(e.PrivateKey.Public().(ed25519.PublicKey))),
Private: data.HexBytes(e.PrivateKey),
})
Expand All @@ -104,7 +113,7 @@ func (e *ed25519Signer) MarshalPrivateKey() (*data.PrivateKey, error) {
}

func (e *ed25519Signer) UnmarshalPrivateKey(key *data.PrivateKey) error {
keyValue := &ed25519PrivateKeyValue{}
keyValue := &Ed25519PrivateKeyValue{}
if err := json.Unmarshal(key.Value, keyValue); err != nil {
return err
}
Expand Down

0 comments on commit 87caa18

Please sign in to comment.