Skip to content

Commit

Permalink
change: from "golang.org/x/crypto/ed25519" to "crypto/ed25519"
Browse files Browse the repository at this point in the history
change: when creating private key is to NOT provide a rand.Reader, and allow ed25519.GenerateKey() decide what reader to use, this enables and fixes #67
  • Loading branch information
aricart committed Nov 25, 2024
1 parent c865baf commit 60e175c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
16 changes: 5 additions & 11 deletions keypair.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018-2022 The NATS Authors
// Copyright 2018-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -15,10 +15,9 @@ package nkeys

import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"io"

"golang.org/x/crypto/ed25519"
)

// kp is the internal struct for a kepypair using seed.
Expand All @@ -31,25 +30,20 @@ const seedLen = 32

// CreatePair will create a KeyPair based on the rand entropy and a type/prefix byte.
func CreatePair(prefix PrefixByte) (KeyPair, error) {
return CreatePairWithRand(prefix, rand.Reader)
return CreatePairWithRand(prefix, nil)
}

// CreatePair will create a KeyPair based on the rand reader and a type/prefix byte. rand can be nil.
func CreatePairWithRand(prefix PrefixByte, rr io.Reader) (KeyPair, error) {
if prefix == PrefixByteCurve {
return CreateCurveKeysWithRand(rr)
}
if rr == nil {
rr = rand.Reader
}
var rawSeed [seedLen]byte

_, err := io.ReadFull(rr, rawSeed[:])
_, priv, err := ed25519.GenerateKey(rr)
if err != nil {
return nil, err
}

seed, err := EncodeSeed(prefix, rawSeed[:])
seed, err := EncodeSeed(prefix, priv.Seed())
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions nkeys_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The NATS Authors
// Copyright 2018-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -15,14 +15,13 @@ package nkeys

import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"io"
"os"
"regexp"
"testing"

"golang.org/x/crypto/ed25519"
)

func TestVersion(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions public.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 The NATS Authors
// Copyright 2018-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -14,10 +14,9 @@
package nkeys

import (
"crypto/ed25519"
"crypto/rand"
"io"

"golang.org/x/crypto/ed25519"
)

// A KeyPair from a public key capable of verifying only.
Expand Down
8 changes: 5 additions & 3 deletions xkeys.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022-2023 The NATS Authors
// Copyright 2022-2024 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -15,6 +15,7 @@ package nkeys

import (
"bytes"
"crypto/ed25519"
"crypto/rand"
"encoding/binary"
"io"
Expand All @@ -40,17 +41,18 @@ type ckp struct {

// CreateCurveKeys will create a Curve typed KeyPair.
func CreateCurveKeys() (KeyPair, error) {
return CreateCurveKeysWithRand(rand.Reader)
return CreateCurveKeysWithRand(nil)
}

// CreateCurveKeysWithRand will create a Curve typed KeyPair
// with specified rand source.
func CreateCurveKeysWithRand(rr io.Reader) (KeyPair, error) {
var kp ckp
_, err := io.ReadFull(rr, kp.seed[:])
_, priv, err := ed25519.GenerateKey(rr)
if err != nil {
return nil, err
}
kp.seed = [curveKeyLen]byte(priv.Seed())

Check failure on line 55 in xkeys.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest, true)

cannot convert priv.Seed() (value of type []byte) to type [32]byte

Check failure on line 55 in xkeys.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest, true)

cannot convert priv.Seed() (value of type []byte) to type [32]byte

Check failure on line 55 in xkeys.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest, true)

cannot convert priv.Seed() (value of type []byte) to type [32]byte

Check failure on line 55 in xkeys.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest, true)

cannot convert priv.Seed() (value of type []byte) to type [32]byte
return &kp, nil
}

Expand Down

0 comments on commit 60e175c

Please sign in to comment.