From 51a16a7abbf34e01b9f669711f8eeef4b01f02f8 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 30 Aug 2016 13:31:28 +0200 Subject: Solve DH --- go/diffie-hellman/diffie_hellman.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/go/diffie-hellman/diffie_hellman.go b/go/diffie-hellman/diffie_hellman.go index 27d810e..29f7c72 100644 --- a/go/diffie-hellman/diffie_hellman.go +++ b/go/diffie-hellman/diffie_hellman.go @@ -1,19 +1,26 @@ package diffiehellman -import "math/big" +import ( + "crypto/rand" + "math/big" +) -func PrivateKey(p *big.Int) (priv *big.Int) { - return new(big.Int) +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) (pub *big.Int) { - return new(big.Int) +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) (s *big.Int) { - return new(big.Int) +func SecretKey(a, B, p *big.Int) *big.Int { + return new(big.Int).Exp(B, a, p) } -func NewPair(p *big.Int, g int64) (a, A *big.Int) { - return new(big.Int), new(big.Int) +func NewPair(p *big.Int, g int64) (*big.Int, *big.Int) { + a := PrivateKey(p) + A := PublicKey(a, p, g) + return a, A } -- cgit v1.2.3