summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-11-12 14:55:52 +0100
committerDimitri Sokolyuk <demon@dim13.org>2017-11-12 14:55:52 +0100
commit9ee9178c22088df86da2b24cdefa826bf87983ed (patch)
tree121964a2c2a33a390855e12f28b94e619f37cb54
parentfe383e3ee08b10dbb31025eb15d6d2d82cf15aef (diff)
PrivateKey
-rw-r--r--go/diffie-hellman/diffie_hellman.go10
1 files 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 {