diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-04-17 01:21:15 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-04-17 01:21:15 -0400 |
commit | df9d05b74ef79671d4421bf6134b204fdc7a5ee3 (patch) | |
tree | cfabac77b2f35e0460485b3f8de1a1fc8184d7fa /src/dh.rs | |
parent | 06188a1e0ba1c03ac223afb59b62fd6fcf928bb0 (diff) | |
download | matasano-df9d05b74ef79671d4421bf6134b204fdc7a5ee3.tar.gz matasano-df9d05b74ef79671d4421bf6134b204fdc7a5ee3.zip |
problem 33
Diffstat (limited to 'src/dh.rs')
-rw-r--r-- | src/dh.rs | 28 |
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) + } +} |