dhkx

package module
v0.0.0-...-9e5b033 Latest Latest
Warning

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

Go to latest
Published: May 22, 2018 License: Apache-2.0 Imports: 4 Imported by: 30

README

dhkx

Build Status

Diffie Hellman Key-exchange algorithm in Go

GoDoc

Documentation

Overview

This is an implementation of Diffie-Hellman Key Exchange algorithm. The algorithm is used to establish a shared key between two communication peers without sharing secrete information.

Typical process:

First, Alice and Bob should agree on which group to use. If you are not sure, choose group 14. GetGroup() will return the desired group by a given id. GetGroup(0) will return a default group, which is usually safe enough to use this group. It is totally safe to share the group's information.

NOTE: The code below will skip error-checking part for the sake of simplicity.

Here is the code on Alice's side:

// Get a group. Use the default one would be enough.
g, _ := GetGroup(0)

// Generate a private key from the group.
// Use the default random number generator.
priv, _ := g.GeneratePrivateKey(nil)

// Get the public key from the private key.
pub := priv.Bytes()

// Send the public key to Bob.
Send("Bob", pub)

// Receive a slice of bytes from Bob, which contains Bob's public key
b := Recv("Bob")

// Recover Bob's public key
bobPubKey := NewPublicKey(b)

// Compute the key
k, _ := group.ComputeKey(bobPubKey, priv)

// Get the key in the form of []byte
key := k.Bytes()

Similarly, here is the code on Bob's side:

// Get a group. Use the default one would be enough.
g, _ := GetGroup(0)

// Generate a private key from the group.
// Use the default random number generator.
priv, _ := g.GeneratePrivateKey(nil)

// Get the public key from the private key.
pub := priv.Bytes()

// Receive a slice of bytes from Alice, which contains Alice's public key
a := Recv("Alice")

// Send the public key to Alice.
Send("Alice", pub)

// Recover Alice's public key
alicePubKey := NewPublicKey(a)

// Compute the key
k, _ := group.ComputeKey(alicePubKey, priv)

// Get the key in the form of []byte
key := k.Bytes()

To this point, the variables ”key” on both Alice and Bob side are same. It could be used as the secrete key for the later communication.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DHGroup

type DHGroup struct {
	// contains filtered or unexported fields
}

func CreateGroup

func CreateGroup(prime, generator *big.Int) (group *DHGroup)

This function enables users to create their own custom DHGroup. Most users will not however want to use this function, and should prefer the use of GetGroup which supplies DHGroups defined in RFCs 2409 and 3526

WARNING! You should only use this if you know what you are doing. The behavior of the group returned by this function is not defined if prime is not in fact prime.

func GetGroup

func GetGroup(groupID int) (group *DHGroup, err error)

This function fetches a DHGroup by its ID as defined in either RFC 2409 or RFC 3526.

If you are unsure what to use use group ID 0 for a sensible default value

func (*DHGroup) ComputeKey

func (self *DHGroup) ComputeKey(pubkey *DHKey, privkey *DHKey) (key *DHKey, err error)

func (*DHGroup) G

func (self *DHGroup) G() *big.Int

func (*DHGroup) GeneratePrivateKey

func (self *DHGroup) GeneratePrivateKey(randReader io.Reader) (key *DHKey, err error)

func (*DHGroup) P

func (self *DHGroup) P() *big.Int

type DHKey

type DHKey struct {
	// contains filtered or unexported fields
}

func NewPublicKey

func NewPublicKey(s []byte) *DHKey

func (*DHKey) Bytes

func (self *DHKey) Bytes() []byte

func (*DHKey) IsPrivateKey

func (self *DHKey) IsPrivateKey() bool

func (*DHKey) String

func (self *DHKey) String() string

Jump to

Keyboard shortcuts

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