summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-30 13:31:28 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-30 13:31:28 +0200
commit51a16a7abbf34e01b9f669711f8eeef4b01f02f8 (patch)
tree7e9fb873bbb30449990690a9adc9036c62da9995
parent7ddc3c83dd47daf19222a64d20f278e299ec8c50 (diff)
Solve DH
-rw-r--r--go/diffie-hellman/diffie_hellman.go25
1 files 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
}