ecdh25519

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ecdh25519 implements the curve25519 diffie-hellman protocol. See https://cr.yp.to/ecdh.html and https://www.ietf.org/rfc/rfc7748.html.

Index

Examples

Constants

View Source
const (
	// PublicKeySize is the size, in bytes, of public keys as used in this package.
	PublicKeySize = 32
	// PrivateKeySize is the size, in bytes, of private keys as used in this package.
	PrivateKeySize = 32
)

Variables

View Source
var (
	ErrBadPrivateKeyLength = errors.New("ecdh25519: bad private key length")
	ErrBadPublicKeyLength  = errors.New("ecdh25519: bad public key length")
)

Functions

func GenerateKeyPair

func GenerateKeyPair(rand io.Reader) (PublicKey, PrivateKey, error)

GenerateKeyPair generates a public/private key pair using entropy from rand. If rand is nil, crypto/rand.Reader will be used.

Example
package main

import (
	"bytes"
	"crypto/rand"
	"fmt"

	"github.com/adnsio/ecdh/ecdh25519"
)

func main() {
	alicePublicKey, alicePrivateKey, err := ecdh25519.GenerateKeyPair(rand.Reader)
	if err != nil {
		panic(err)
	}

	bobPublicKey, bobPrivateKey, err := ecdh25519.GenerateKeyPair(rand.Reader)
	if err != nil {
		panic(err)
	}

	aliceSharedSecret, err := ecdh25519.GenerateSharedSecret(alicePrivateKey, bobPublicKey)
	if err != nil {
		panic(err)
	}

	bobSharedSecret, err := ecdh25519.GenerateSharedSecret(bobPrivateKey, alicePublicKey)
	if err != nil {
		panic(err)
	}

	if bytes.Equal(aliceSharedSecret, bobSharedSecret) {
		fmt.Printf("shared secrets are equal")
	}

}
Output:

shared secrets are equal

func GenerateSharedSecret

func GenerateSharedSecret(privateKey PrivateKey, publicKey PublicKey) ([]byte, error)

GenerateSharedSecret generates a shared secret by using someone else's public key.

Types

type PrivateKey

type PrivateKey []byte

PrivateKey is the type of ecdh25519 private keys.

func (PrivateKey) PublicKey

func (p PrivateKey) PublicKey() (PublicKey, error)

PublicKey returns the PublicKey corresponding to the PrivateKey.

type PublicKey

type PublicKey []byte

PublicKey is the type of ecdh25519 public keys.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL