Class: OpenID::DiffieHellman

Inherits:
Object
  • Object
show all
Defined in:
lib/openid/dh.rb

Overview

Encapsulates a Diffie-Hellman key exchange. This class is used internally by both the consumer and server objects.

Read more about Diffie-Hellman on wikipedia: en.wikipedia.org/wiki/Diffie-Hellman

Constant Summary collapse

@@default_mod =

From the OpenID specification

155172898181473697471232257763715539915724801966915404479707795314057629378541917580651227423698188993727816152646631438561595825688188889951272158842675419950341258706556549803580104870537681476726513255747040765857479291291572334510643245094715007229621094194349783925984760375594985848253359305585439638443
@@default_gen =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modulus = nil, generator = nil, priv = nil) ⇒ DiffieHellman

Returns a new instance of DiffieHellman.



26
27
28
29
30
# File 'lib/openid/dh.rb', line 26

def initialize(modulus=nil, generator=nil, priv=nil)
  @modulus = modulus.nil? ? @@default_mod : modulus
  @generator = generator.nil? ? @@default_gen : generator
  set_private(priv.nil? ? OpenID::CryptUtil.rand(@modulus-2) + 1 : priv)
end

Instance Attribute Details

#generatorObject (readonly)

Returns the value of attribute generator.



18
19
20
# File 'lib/openid/dh.rb', line 18

def generator
  @generator
end

#modulusObject (readonly)

Returns the value of attribute modulus.



18
19
20
# File 'lib/openid/dh.rb', line 18

def modulus
  @modulus
end

#publicObject (readonly)

Returns the value of attribute public.



18
19
20
# File 'lib/openid/dh.rb', line 18

def public
  @public
end

Class Method Details

.from_defaultsObject

A new DiffieHellman object, using the modulus and generator from the OpenID specification



22
23
24
# File 'lib/openid/dh.rb', line 22

def DiffieHellman.from_defaults
  DiffieHellman.new(@@default_mod, @@default_gen)
end

Instance Method Details

#get_shared_secret(composite) ⇒ Object



32
33
34
# File 'lib/openid/dh.rb', line 32

def get_shared_secret(composite)
  DiffieHellman.powermod(composite, @private, @modulus)
end

#using_default_values?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/openid/dh.rb', line 43

def using_default_values?
  @generator == @@default_gen && @modulus == @@default_mod
end

#xor_secret(algorithm, composite, secret) ⇒ Object



36
37
38
39
40
41
# File 'lib/openid/dh.rb', line 36

def xor_secret(algorithm, composite, secret)
  dh_shared = get_shared_secret(composite)
  packed_dh_shared = OpenID::CryptUtil.num_to_binary(dh_shared)
  hashed_dh_shared = algorithm.call(packed_dh_shared)
  return DiffieHellman.strxor(secret, hashed_dh_shared)
end