From 9ee9178c22088df86da2b24cdefa826bf87983ed Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 12 Nov 2017 14:55:52 +0100 Subject: PrivateKey --- go/diffie-hellman/diffie_hellman.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/go/diffie-hellman/diffie_hellman.go b/go/diffie-hellman/diffie_hellman.go index 78c8e56..0e3df06 100644 --- a/go/diffie-hellman/diffie_hellman.go +++ b/go/diffie-hellman/diffie_hellman.go @@ -5,11 +5,15 @@ import ( "math/big" ) +var one = big.NewInt(1) + // 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) + priv, _ := rand.Int(rand.Reader, p) // [0..p) + if priv.Cmp(one) <= 0 { + return PrivateKey(p) + } + return priv } func PublicKey(a, p *big.Int, g int64) *big.Int { -- cgit v1.2.3