Created
September 20, 2017 21:53
-
-
Save esell/e23b311cd5a96645602ba5370e9e61f4 to your computer and use it in GitHub Desktop.
dh stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
"github.com/monnand/dhkx" | |
) | |
var bobKey *dhkx.DHKey | |
var aliceKey *dhkx.DHKey | |
// set random source for randomness | |
var s1 = rand.NewSource(time.Now().UnixNano()) | |
// create bob's key(s) | |
func genBob() []byte { | |
r1 := rand.New(s1) | |
g, _ := dhkx.GetGroup(0) | |
bobKey, _ = g.GeneratePrivateKey(r1) | |
pub := bobKey.Bytes() | |
return pub | |
} | |
// create alice's key(s) | |
func genAlice() []byte { | |
r1 := rand.New(s1) | |
g, _ := dhkx.GetGroup(0) | |
aliceKey, _ = g.GeneratePrivateKey(r1) | |
pub := aliceKey.Bytes() | |
return pub | |
} | |
// build bob's shared key | |
func decryptBob(aliceKey []byte) []byte { | |
g, _ := dhkx.GetGroup(0) | |
// Recover Alice's public key | |
alicePubKey := dhkx.NewPublicKey(aliceKey) | |
// Compute the key | |
k, _ := g.ComputeKey(alicePubKey, bobKey) | |
// Get the key in the form of []byte | |
key := k.Bytes() | |
return key | |
} | |
// build alice's share key | |
func decryptAlice(bobKey []byte) []byte { | |
g, _ := dhkx.GetGroup(0) | |
// Recover bob's public key | |
bobPubKey := dhkx.NewPublicKey(bobKey) | |
// Compute the key | |
k, _ := g.ComputeKey(bobPubKey, aliceKey) | |
// Get the key in the form of []byte | |
key := k.Bytes() | |
return key | |
} | |
func main() { | |
bobPubKey := genBob() | |
alicePubKey := genAlice() | |
if string(decryptBob(alicePubKey)) != string(decryptAlice(bobPubKey)) { | |
fmt.Println("oh no, bob and alice don't agree") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment