Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
number571 committed Oct 30, 2024
1 parent 3a47173 commit 6f7c5c7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `cmd/tools/keysplit`: deleted
- `pkg/crypto/asymmetric`: append bench NewPrivKey
- `pkg/network/anonymity/queue`: delete GetRandQueuePeriod
- `cmd/tools/keygen`: add 'seed' param to generate private key

<!-- ... -->

Expand Down
9 changes: 8 additions & 1 deletion cmd/tools/keygen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,12 @@
```bash
usage:
go run .
go run . [-seed]
```

## Example

```bash
go run . -seed
<priv-key-seed>
```
74 changes: 73 additions & 1 deletion cmd/tools/keygen/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,89 @@
package main

import (
"bufio"
"flag"
"fmt"
"os"
"os/exec"
"runtime"

"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/crypto/keybuilder"
"github.com/number571/go-peer/pkg/crypto/random"
"github.com/number571/go-peer/pkg/encoding"
)

func main() {
priv := asymmetric.NewPrivKey()
seed := flag.Bool("seed", false, "set seed private key")
flag.Parse()

seedBytes := random.NewRandom().GetBytes(asymmetric.CKeySeedSize)
if *seed {
keyBuilder := keybuilder.NewKeyBuilder(0, []byte{})
seedBytes = keyBuilder.Build(readUntilEOL(), asymmetric.CKeySeedSize)
}

priv := asymmetric.NewPrivKeyFromSeed(seedBytes)
if err := os.WriteFile("seed.key", []byte(encoding.HexEncode(seedBytes)), 0o600); err != nil {
panic(err)
}
if err := os.WriteFile("priv.key", []byte(priv.ToString()), 0o600); err != nil {
panic(err)
}
if err := os.WriteFile("pub.key", []byte(priv.GetPubKey().ToString()), 0o600); err != nil {
panic(err)
}
}

func readUntilEOL() string {
var (
p = make([]byte, 0, 256)
b = make([]byte, 1)
)

if runtime.GOOS == "windows" {
res, _, err := bufio.NewReader(os.Stdin).ReadLine()
if err != nil {
panic(err)
}
return string(res)
}

if err := exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run(); err != nil {
panic(err)
}
if err := exec.Command("stty", "-F", "/dev/tty", "-echo").Run(); err != nil {
panic(err)
}
defer func() { _ = exec.Command("stty", "-F", "/dev/tty", "echo").Run() }()

for {
if _, err := os.Stdin.Read(b); err != nil {
panic(err)
}
if b[0] == '\n' { // <enter>
fmt.Println()
break
}
if b[0] == 127 { // <backspace>
if len(p) == 0 {
continue
}
fmt.Print("\r")
for i := 0; i < len(p); i++ {
fmt.Print(" ")
}
fmt.Print("\r")
for i := 0; i < len(p)-1; i++ {
fmt.Print("*")
}
p = p[:len(p)-1]
continue
}
fmt.Print("*")
p = append(p, b[0])
}

return string(p)
}
14 changes: 10 additions & 4 deletions pkg/crypto/asymmetric/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/number571/go-peer/pkg/encoding"
)

const (
CKeySeedSize = CKEMKeySeedSize + CDSAKeySeedSize
CPrivKeySize = CKEMPrivKeySize + CDSAPrivKeySize
CPubKeySize = CKEMPubKeySize + CDSAPubKeySize
)

const (
cPrivKeyPrefix = "PrivKey{"
cPubKeyPrefix = "PubKey{"
Expand All @@ -33,8 +39,8 @@ type sPubKey struct {
}

func NewPrivKeyFromSeed(pSeed []byte) IPrivKey {
if len(pSeed) != CKEMKeySeedSize+CDSAKeySeedSize {
panic("len(pSeed) != CKEMKeySeedSize+CDSAKeySeedSize")
if len(pSeed) != CKeySeedSize {
panic("len(pSeed) != CKeySeedSize")
}
return newPrivKey(
NewKEMPrivKeyFromSeed(pSeed[:CKEMKeySeedSize]),
Expand Down Expand Up @@ -79,7 +85,7 @@ func LoadPrivKey(pKeychain interface{}) IPrivKey {
panic("unknown type private key chain")
}

if len(keychainBytes) != (CKEMPrivKeySize + CDSAPrivKeySize) {
if len(keychainBytes) != CPrivKeySize {
return nil
}

Expand Down Expand Up @@ -135,7 +141,7 @@ func LoadPubKey(pKeychain interface{}) IPubKey {
panic("unknown type public key chain")
}

if len(keychainBytes) != (CKEMPubKeySize + CDSAPubKeySize) {
if len(keychainBytes) != CPubKeySize {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion test/result/badge_codelines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions test/result/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6f7c5c7

Please sign in to comment.