class DiffieHellman

This code exists in the public domain.

Attributes

e[R]
g[R]
p[R]
q[R]
x[R]

Public Class Methods

new(p, g, q) click to toggle source

p is the prime, g the generator and q order of the subgroup

# File lib/syndi/irc/sasl/diffie_hellman.rb, line 8
def initialize p, g, q
  @p = p
  @g = g
  @q = q
end

Public Instance Methods

generate(tries=16) click to toggle source

generate the [secret] random value and the public key

# File lib/syndi/irc/sasl/diffie_hellman.rb, line 15
def generate tries=16
  tries.times do
    @x = rand(@q)
    @e = self.g.mod_exp(@x, self.p)
    return @e if self.valid?
  end
  raise ArgumentError, "can't generate valid e"
end
secret(f) click to toggle source

compute the shared secret, given the public key

# File lib/syndi/irc/sasl/diffie_hellman.rb, line 30
def secret f
  f.mod_exp(self.x, self.p)
end
valid?(_e = self.e) click to toggle source

validate a public key

# File lib/syndi/irc/sasl/diffie_hellman.rb, line 25
def valid?(_e = self.e)
  _e and _e.between?(2, self.p-2) and _e.bits_set > 1
end