summaryrefslogtreecommitdiff
path: root/go/diffie-hellman/diffie_hellman.go
blob: 78c8e56f22bd09ade2eb6f4bfd236831eabed563 (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
27
package diffiehellman

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

// PrivateKey greater then 1 and less then p
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
}