From d8eaeb939bd16899b388db27a719329cae6d5863 Mon Sep 17 00:00:00 2001 From: Stef Dunlap Date: Fri, 17 Jun 2022 16:49:30 -0400 Subject: [PATCH] Use maximum go routines to find valid spring83 key - calculate valid key suffix based on current year - update documentation --- README.md | 2 +- client/main.go | 58 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 7715c73..431445b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ If you have [modd]() installed, run `modd`. Alternatively, `go run server/main.g On first run, the client will generate a keypair for you according to the spring83 spec, and store it in `~/.config/spring83/key.pub` and `~/.config/spring83/key.priv`. -This key has to meet a certain specification, so it may take up to a half hour to generate on the first run. The code to generate it is totally unoptimized and can probably be sped up, if you want to be the one to do that, awesome. +This key has to meet a certain specification, so it may take some time to generate on the first run. `echo "testing" | go run client/main.go` diff --git a/client/main.go b/client/main.go index 181e61d..9d9018d 100644 --- a/client/main.go +++ b/client/main.go @@ -11,29 +11,51 @@ import ( "os" "os/user" "path/filepath" + "runtime" + "sync" "time" ) -func validKey() (ed25519.PublicKey, ed25519.PrivateKey) { - pub, priv, err := ed25519.GenerateKey(nil) - if err != nil { - panic(err) +func validKey() (foundPublicKey ed25519.PublicKey, foundPrivateKey ed25519.PrivateKey) { + + expiryYear := time.Now().Year() + 1 + keyEnd := fmt.Sprintf("ed%d", expiryYear) + nRoutines := runtime.NumCPU() - 1 + var waitGroup sync.WaitGroup + var once sync.Once + + fmt.Println(" - looking for a key that ends in", keyEnd) + fmt.Println(" - using", nRoutines, "cores") + + waitGroup.Add(nRoutines) + for i := 0; i < nRoutines; i++ { + go func(num int) { + for foundPublicKey == nil { + pub, priv, err := ed25519.GenerateKey(nil) + if err != nil { + panic(err) + } + + target, err := hex.DecodeString(keyEnd) + if err != nil { + panic(err) + } + + if bytes.Compare(pub[29:32], target) == 0 { + once.Do(func() { + fmt.Printf("%s\n", fmt.Sprintf("%x", pub)) + foundPublicKey = pub + foundPrivateKey = priv + }) + } + } + waitGroup.Done() + }(i) } - // TODO: generate a new key if necessary - target, err := hex.DecodeString("ed2023") - if err != nil { - panic(err) - } + waitGroup.Wait() - for bytes.Compare(pub[29:32], target) != 0 { - pub, priv, err = ed25519.GenerateKey(nil) - if err != nil { - panic(err) - } - } - fmt.Printf("%s\n", fmt.Sprintf("%x", pub)) - return pub, priv + return } func fileExists(name string) bool { @@ -72,7 +94,7 @@ func getKeys() (ed25519.PublicKey, ed25519.PrivateKey) { panic(err) } } else { - fmt.Printf("Generating key, give this a minute or two\n") + fmt.Printf("I am fishing in the sea of all possible keys for a valid spring83 key. This may take a bit...\n") pubkey, privkey = validKey() os.WriteFile(pubfile, pubkey, 0666)