5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-28 17:31:37 +00:00
yggdrasil-go/cmd/genkeys/main.go

79 lines
1.7 KiB
Go
Raw Normal View History

2017-12-29 04:16:20 +00:00
/*
This file generates crypto keys.
It prints out a new set of keys each time if finds a "better" one.
By default, "better" means a higher NodeID (-> higher IP address).
2019-11-29 09:45:02 +00:00
This is because the IP address format can compress leading 1s in the address, to increase the number of ID bits in the address.
2017-12-29 04:16:20 +00:00
If run with the "-sig" flag, it generates signing keys instead.
A "better" signing key means one with a higher TreeID.
This only matters if it's high enough to make you the root of the tree.
*/
package main
import (
2021-05-10 10:58:06 +00:00
"crypto/ed25519"
"encoding/hex"
"fmt"
"net"
"runtime"
"github.com/yggdrasil-network/yggdrasil-go/src/address"
)
2017-12-29 04:16:20 +00:00
2018-02-21 15:57:03 +00:00
type keySet struct {
2021-05-10 10:58:06 +00:00
priv ed25519.PrivateKey
pub ed25519.PublicKey
2018-02-21 15:57:03 +00:00
}
2017-12-29 04:16:20 +00:00
func main() {
2018-02-21 15:57:03 +00:00
threads := runtime.GOMAXPROCS(0)
2021-05-10 10:58:06 +00:00
var currentBest ed25519.PublicKey
2018-02-21 15:57:03 +00:00
newKeys := make(chan keySet, threads)
for i := 0; i < threads; i++ {
2021-05-10 10:58:06 +00:00
go doKeys(newKeys)
2018-02-21 15:57:03 +00:00
}
for {
newKey := <-newKeys
2021-05-10 10:58:06 +00:00
if isBetter(currentBest, newKey.pub) || len(currentBest) == 0 {
currentBest = newKey.pub
fmt.Println("-----")
fmt.Println("Priv:", hex.EncodeToString(newKey.priv))
fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
addr := address.AddrForKey(newKey.pub)
fmt.Println("IP:", net.IP(addr[:]).String())
2018-02-21 15:57:03 +00:00
}
2018-01-04 22:37:51 +00:00
}
2017-12-29 04:16:20 +00:00
}
2021-05-10 10:58:06 +00:00
func isBetter(oldPub, newPub ed25519.PublicKey) bool {
for idx := range oldPub {
if newPub[idx] < oldPub[idx] {
return true
2018-01-04 22:37:51 +00:00
}
2021-05-10 10:58:06 +00:00
if newPub[idx] > oldPub[idx] {
break
2018-01-04 22:37:51 +00:00
}
}
2021-05-10 10:58:06 +00:00
return false
2017-12-29 04:16:20 +00:00
}
2021-05-10 10:58:06 +00:00
func doKeys(out chan<- keySet) {
bestKey := make(ed25519.PublicKey, ed25519.PublicKeySize)
for idx := range bestKey {
bestKey[idx] = 0xff
2018-01-04 22:37:51 +00:00
}
for {
2021-05-10 10:58:06 +00:00
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
panic(err)
2018-02-21 15:57:03 +00:00
}
2021-05-10 10:58:06 +00:00
if !isBetter(bestKey, pub) {
2018-01-04 22:37:51 +00:00
continue
}
2021-05-10 10:58:06 +00:00
bestKey = pub
out <- keySet{priv, pub}
2018-01-04 22:37:51 +00:00
}
2017-12-29 04:16:20 +00:00
}