Skip to content

Commit

Permalink
INTERFACE UPDATED
Browse files Browse the repository at this point in the history
  • Loading branch information
mkawserm committed Mar 17, 2020
1 parent 3cb0de5 commit 1745e10
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 26 deletions.
3 changes: 2 additions & 1 deletion doc_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tordn_test

import (
"bytes"
"fmt"
"github.com/mkawserm/tordn"
)
Expand All @@ -24,7 +25,7 @@ func ExampleV3_GenerateTORDomainName() {
}

secretKey := []byte("eipuopkyhrisvmrlbghubnlxunzkwoij")
publicKey1, privateKey1, onionAddress1, err1 := v3domainName.GenerateTORDomainName(secretKey)
publicKey1, privateKey1, onionAddress1, err1 := v3domainName.GenerateTORDomainName(bytes.NewReader(secretKey))
if err1 == nil {
fmt.Printf("Public Key:")
fmt.Println(publicKey1)
Expand Down
4 changes: 3 additions & 1 deletion tordn.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package tordn

import "io"

// TORDomainNameGenerator basic interface
type TORDomainNameGenerator interface {

// GenerateTORDomainName generates tor domain using the secret key.
//
// Implementations must handle nil secretKey
GenerateTORDomainName(secretKey []byte) (publicKey []byte, privateKey []byte, onionAddress []byte, err error)
GenerateTORDomainName(secretKey io.Reader) (publicKey []byte, privateKey []byte, onionAddress []byte, err error)
}
27 changes: 6 additions & 21 deletions v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,21 @@ package tordn

import (
"crypto/ed25519"
"errors"
"io"
)

var ErrSecretKeyLengthMismatch = errors.New("secret key length must be 32 bytes")

// V3 TOR address generator
type V3 struct {
}

// GenerateTORDomainName implements TORDomainNameGenerator interface
//
// Generate tor v3 domain name using the secretKey. If the secretKey is empty then default ed25519.GenerateKey
// function will be used otherwise ed25519.NewKeyFromSeed will be used to generate the public key.
// If provided the length of the secretKey must be 32
func (g *V3) GenerateTORDomainName(secretKey []byte) (publicKey []byte, privateKey []byte, onionAddress []byte, err error) {
// Generate tor v3 domain name using the secretKey.
func (g *V3) GenerateTORDomainName(secretKey io.Reader) (publicKey []byte, privateKey []byte, onionAddress []byte, err error) {
// Generate key pair
if secretKey == nil {
publicKey, privateKey, err = ed25519.GenerateKey(nil)

if err != nil {
return nil, nil, nil, err
}
} else {
if len(secretKey) != 32 {
return nil, nil, nil, ErrSecretKeyLengthMismatch
}

privateKey = ed25519.NewKeyFromSeed(secretKey)
publicKey = make([]byte, 32)
copy(publicKey, privateKey[32:])
publicKey, privateKey, err = ed25519.GenerateKey(secretKey)
if err != nil {
return nil, nil, nil, err
}

onionAddress = MakeV3OnionAddressWithExtension(publicKey)
Expand Down
8 changes: 5 additions & 3 deletions v3_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tordn

import (
"bytes"
"io"
"testing"
)

Expand All @@ -25,7 +27,7 @@ var torDomainList = []struct {
func TestV3_GenerateTORDomainName(t *testing.T) {
v3 := &V3{}
for _, d := range torDomainList {
_, _, foundOnionAddress, _ := v3.GenerateTORDomainName(d.secretKey)
_, _, foundOnionAddress, _ := v3.GenerateTORDomainName(bytes.NewReader(d.secretKey))
if d.expectedOnionAddress != string(foundOnionAddress) {
t.Errorf("Invalid onion address. Expected: [%v] Found: [%v]", d.expectedOnionAddress, foundOnionAddress)
}
Expand All @@ -47,8 +49,8 @@ func TestV3_GenerateTORDomainName(t *testing.T) {
}

{
_, _, _, err := v3.GenerateTORDomainName([]byte("1"))
if err != ErrSecretKeyLengthMismatch {
_, _, _, err := v3.GenerateTORDomainName(bytes.NewReader([]byte("1")))
if err != io.ErrUnexpectedEOF {
t.Errorf("Unexpected error %v", err)
}
}
Expand Down

0 comments on commit 1745e10

Please sign in to comment.