Skip to content

Instantly share code, notes, and snippets.

@leyafo
Last active October 21, 2023 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leyafo/1687d5121c43a2618c1c8a03612f10a6 to your computer and use it in GitHub Desktop.
Save leyafo/1687d5121c43a2618c1c8a03612f10a6 to your computer and use it in GitHub Desktop.
ed25519 between golang and nodejs.
package main
import (
"crypto/ed25519"
"crypto/rand"
"encoding/base64"
"fmt"
)
func main() {
// Generate a new private/public key pair
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
fmt.Println("Error generating key:", err)
return
}
// Convert the public and private keys to Base64
publicKeyBase64 := base64.StdEncoding.EncodeToString(publicKey)
privateKeyBase64 := base64.StdEncoding.EncodeToString(privateKey)
fmt.Println("Public Key:", publicKeyBase64)
fmt.Println("Private Key:", privateKeyBase64)
// Sign a message using the private key
message := "Hello, world!"
signature := ed25519.Sign(privateKey, []byte(message))
signatureBase64 := base64.StdEncoding.EncodeToString(signature)
fmt.Println("Signature:", signatureBase64)
}
function verifySignature() {
const publicKeyBase64 = 'NIziXC5F+9yRYq78dGR7imyLdJ/4z7LL/unVNrX9Pmg='
const signatureBase64 = 'wQ48ufSSXzL8zt2OMPs4HNVQZZ5NJ6j0/QrBGx8JlRcZbuu+aHPxPujowX5Uuuky4ONIsvm//Tt4WWrU8yY1AQ=='
const message = 'Hello, world!';
const key = Buffer.concat([
Buffer.from('302a300506032b6570032100', 'hex'), // Static value
Buffer.from(publicKeyBase64, 'base64'),
])
const verifyKey = crypto.createPublicKey({
format: 'der',
type: 'spki',
key,
})
// const publicKey = Buffer.from(publicKeyBase64, 'base64');
const signatureData = Buffer.from(signatureBase64, 'base64');
return crypto.verify(null, Buffer.from(message), verifyKey, signatureData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment