summaryrefslogtreecommitdiffstats
path: root/src/dh.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/dh.rs')
-rw-r--r--src/dh.rs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/dh.rs b/src/dh.rs
new file mode 100644
index 0000000..24c2277
--- /dev/null
+++ b/src/dh.rs
@@ -0,0 +1,28 @@
+use num_bigint::RandBigInt;
+
+pub struct DHKeyPair {
+ pub p: num_bigint::BigUint,
+ pub g: num_bigint::BigUint,
+ pub pubkey: num_bigint::BigUint,
+ privkey: num_bigint::BigUint,
+}
+
+impl DHKeyPair {
+ pub fn new(p: num_bigint::BigUint, g: num_bigint::BigUint) -> DHKeyPair {
+ let privkey = rand::thread_rng().gen_biguint_below(&p);
+ let pubkey = g.modpow(&privkey, &p);
+ DHKeyPair {
+ p,
+ g,
+ pubkey,
+ privkey,
+ }
+ }
+
+ pub fn key_exchange(
+ &self,
+ other_pubkey: &num_bigint::BigUint,
+ ) -> num_bigint::BigUint {
+ other_pubkey.modpow(&self.privkey, &self.p)
+ }
+}