Ed25519 go
Jump to navigation
Jump to search
package main import ( "crypto/rand" "encoding/pem" // "encoding/hex" // b64 "encoding/base64" "io/ioutil" // "golang.org/x/crypto/ed25519" "crypto/ed25519" "golang.org/x/crypto/ssh" // "crypto/ssh" // b58 "encoding/base58" // b32 "encoding/base32" // "github.com/jeremybusk/edkey" "github.com/mikesmitty/edkey" b58 "github.com/akamensky/base58" // b58 "github.com/btcsuite/btcutil/base58" // b58 "github.com/jeremybusk/base58" "fmt" "reflect" ) func main() { // Generate a new private/public keypair for OpenSSH pkey, skey, _ := ed25519.GenerateKey(rand.Reader) b58pkey := b58.Encode(pkey) b58skey := b58.Encode(skey) dpkey, err := b58.Decode(b58pkey) dskey, err := b58.Decode(b58skey) // fmt.Println(err) // if err != nil { // } msg := []byte("test this") // fmsg := []byte("1test this") smsg := ed25519.Sign(dskey, msg) vmsg := ed25519.Verify(dpkey, msg, smsg) fmt.Println("signed msg", smsg) fmt.Println("verify msg", vmsg) fmt.Println("var1 = ", reflect.TypeOf(pkey)) fmt.Println(b58skey) fmt.Println(b58pkey) db58pkey, err := b58.Decode(b58pkey) db58skey, err := b58.Decode(b58skey) if err != nil { fmt.Println(err) } fmt.Println(db58skey) fmt.Println(db58pkey) // fmt.Println("jpubkey", jpubKey) // fmt.Println("jprivkey", jprivKey) // fmt.Println("jprivkey string", b58.Encode(jprivKey)) // fmt.Println("jprivkey string", b58.Encode(jprivKey)) return pubKey, privKey, _ := ed25519.GenerateKey(rand.Reader) publicKey, _ := ssh.NewPublicKey(pubKey) pemKey := &pem.Block{ Type: "OPENSSH PRIVATE KEY", Bytes: edkey.MarshalED25519PrivateKey(privKey), } privateKey := pem.EncodeToMemory(pemKey) authorizedKey := ssh.MarshalAuthorizedKey(publicKey) _ = ioutil.WriteFile("id_ed25519", privateKey, 0600) _ = ioutil.WriteFile("id_ed25519.pub", authorizedKey, 0644) }