github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/tools/naclkey/naclkey.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 // naclkey generates NaCL signing keys. Use the -json flag 5 // to output JSON. 6 7 package main 8 9 import ( 10 "crypto/rand" 11 "encoding/hex" 12 "encoding/json" 13 "flag" 14 "fmt" 15 "os" 16 17 "github.com/keybase/go-crypto/ed25519" 18 ) 19 20 // to avoid importing all of libkb, some constants copied from 21 // libkb/constants.go here: 22 const ( 23 KeybaseKIDV1 = 0x01 24 KIDNaclEddsa = 0x20 25 IDSuffixKID = 0x0a 26 ) 27 28 var jsonOutput = flag.Bool("json", false, "output json") 29 30 func main() { 31 flag.Parse() 32 33 pub, priv, err := ed25519.GenerateKey(rand.Reader) 34 if err != nil { 35 fmt.Printf("Error generating keys: %s\n", err) 36 os.Exit(1) 37 } 38 39 pubkb := make([]byte, len(pub)+3) 40 pubkb[0] = KeybaseKIDV1 41 pubkb[1] = KIDNaclEddsa 42 copy(pubkb[2:], pub[:]) 43 pubkb[len(pubkb)-1] = IDSuffixKID 44 45 if *jsonOutput { 46 x := struct { 47 Public string 48 PublicKeybase string 49 Private string 50 }{ 51 Public: hex.EncodeToString(pub[:]), 52 PublicKeybase: hex.EncodeToString(pubkb), 53 Private: hex.EncodeToString(priv[:]), 54 } 55 j, err := json.MarshalIndent(x, "", " ") 56 if err != nil { 57 fmt.Printf("JSON marshal error: %s\n", err) 58 os.Exit(1) 59 } 60 fmt.Println(string(j)) 61 } else { 62 fmt.Printf("Public key: %x\n", pub) 63 fmt.Printf("Keybase public key: %x\n", pubkb) 64 fmt.Printf("Private key: %x\n", priv) 65 } 66 }