summaryrefslogtreecommitdiff
path: root/go/diffie-hellman/diffie_hellman.go
blob: 29f7c728b9001b58f52dfe6c01bfe71f892c7cc9 (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) {
	a := PrivateKey(p)
	A := PublicKey(a, p, g)
	return a, A
}