This commit is contained in:
number571 2024-10-31 00:51:05 +07:00
parent 3a47173b06
commit 6f7c5c7d1b
6 changed files with 99 additions and 13 deletions

View File

@ -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
<!-- ... -->

View File

@ -4,5 +4,12 @@
```bash
usage:
go run .
go run . [-seed]
```
## Example
```bash
go run . -seed
<priv-key-seed>
```

View File

@ -1,13 +1,33 @@
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)
}
@ -15,3 +35,55 @@ func main() {
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)
}

View File

@ -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{"
@ -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]),
@ -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
}
@ -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
}

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="code lines: 12073"><title>code lines: 12073</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="96" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="57" height="20" fill="#555"/><rect x="57" width="40" height="20" fill="#4682b4"/><rect width="96" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="295" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">code lines</text><text x="295" y="140" transform="scale(.1)" fill="#fff" textLength="510">code lines</text><text aria-hidden="true" x="770" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="320">12073</text><text x="770" y="140" transform="scale(.1)" fill="#fff" textLength="320">12073</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="96" height="20" role="img" aria-label="code lines: 12543"><title>code lines: 12543</title><linearGradient id="s" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="r"><rect width="96" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#r)"><rect width="57" height="20" fill="#555"/><rect x="57" width="40" height="20" fill="#4682b4"/><rect width="96" height="20" fill="url(#s)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="295" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="510">code lines</text><text x="295" y="140" transform="scale(.1)" fill="#fff" textLength="510">code lines</text><text aria-hidden="true" x="770" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="320">12543</text><text x="770" y="140" transform="scale(.1)" fill="#fff" textLength="320">12543</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -7,7 +7,7 @@
>
<g>
<rect x="20.000000" y="20.000000" width="988.000000" height="600.000000" style="fill: rgb(3, 112, 59);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<rect x="20.000000" y="20.000000" width="988.000000" height="600.000000" style="fill: rgb(3, 113, 60);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<text
data-notex="1"
@ -20,7 +20,7 @@
<g>
<rect x="28.000000" y="41.600000" width="960.635147" height="570.400000" style="fill: rgb(3, 112, 59);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<rect x="28.000000" y="41.600000" width="960.635147" height="570.400000" style="fill: rgb(3, 113, 60);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<text
data-notex="1"
@ -91,7 +91,7 @@
<g>
<rect x="36.000000" y="63.200000" width="533.660779" height="540.800000" style="fill: rgb(3, 114, 60);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<rect x="36.000000" y="63.200000" width="533.660779" height="540.800000" style="fill: rgb(4, 115, 61);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<text
data-notex="1"
@ -335,7 +335,7 @@
<g>
<rect x="400.281194" y="473.560591" width="108.240891" height="122.439409" style="fill: rgb(0, 104, 55);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<rect x="400.281194" y="473.560591" width="108.240891" height="122.439409" style="fill: rgb(5, 117, 62);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<text
data-notex="1"
@ -367,7 +367,7 @@
<g>
<rect x="44.000000" y="436.202342" width="348.281194" height="159.797658" style="fill: rgb(14, 131, 69);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<rect x="44.000000" y="436.202342" width="348.281194" height="159.797658" style="fill: rgb(16, 135, 71);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<text
data-notex="1"
@ -658,7 +658,7 @@
<g>
<rect x="408.281194" y="495.160591" width="92.240891" height="66.909275" style="fill: rgb(0, 104, 55);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<rect x="408.281194" y="495.160591" width="92.240891" height="66.909275" style="fill: rgb(8, 122, 64);opacity:1;fill-opacity:1.00;stroke:rgb(128,128,128);stroke-width:1px;stroke-opacity:1.00;" />
<text
data-notex="1"

Before

Width:  |  Height:  |  Size: 29 KiB

After

Width:  |  Height:  |  Size: 29 KiB