summaryrefslogtreecommitdiff
path: root/go/diffie-hellman/diffie_hellman.go
blob: 715e200bb4b3b6fb9a2f5e24d2642153a2e8b4bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package diffiehellman

import (
	"crypto/rand"
	"math/big"
)

func PrivateKey(p *big.Int) *big.Int {
	two := big.NewInt(2)
	priv, _ := rand.Int(rand.Reader, new(big.Int).Sub(p, two))
	return priv.Add(priv, two)
}

func PublicKey(a, p *big.Int, g int64) *big.Int {
	return new(big.Int).Exp(big.NewInt(g), a, p)
}

func SecretKey(a, B, p *big.Int) *big.Int {
	return new(big.Int).Exp(B, a, p)
}

func NewPair(p *big.Int, g int64) (*big.Int, *big.Int) {
	priv := PrivateKey(p)
	pub := PublicKey(priv, p, g)
	return priv, pub
}