Class: ElGamal::KeyPair

Inherits:
Object
  • Object
show all
Defined in:
lib/elgamal/key_pair.rb

Instance Method Summary collapse

Constructor Details

#initialize(public_key: nil, private_key: nil) ⇒ KeyPair

Returns a new instance of KeyPair.



5
6
7
8
# File 'lib/elgamal/key_pair.rb', line 5

def initialize(public_key: nil, private_key: nil)
	@public_key = public_key
	@private_key = private_key
end

Instance Method Details

#generate(bits: 20, a: nil) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/elgamal/key_pair.rb', line 10

def generate(bits: 20, a: nil)
	p = OpenSSL::BN::generate_prime(bits).to_i
	g = (rand * p).to_i
	a ||= (rand * (p - 1)).to_i + 1
	h = g.to_bn.mod_exp(a, p).to_i
	return ElGamal::PublicKey.new(public_p: p, public_g: g, public_h: h), 
		   ElGamal::PrivateKey.new(private_a: a, public_p: p)
end