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

155_172_898_181_473_697_471_232_257_763_715_539_915_724_801_966_915_404_479_707_795_314_057_629_378_541_917_580_651_227_423_698_188_993_727_816_152_646_631_438_561_595_825_688_188_889_951_272_158_842_675_419_950_341_258_706_556_549_803_580_104_870_537_681_476_726_513_255_747_040_765_857_479_291_291_572_334_510_643_245_094_715_007_229_621_094_194_349_783_925_984_760_375_594_985_848_253_359_305_585_439_638_443
@@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.



24
25
26
27
28
# File 'lib/openid/dh.rb', line 24

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.



16
17
18
# File 'lib/openid/dh.rb', line 16

def generator
  @generator
end

#modulusObject (readonly)

Returns the value of attribute modulus.



16
17
18
# File 'lib/openid/dh.rb', line 16

def modulus
  @modulus
end

#publicObject (readonly)

Returns the value of attribute public.



16
17
18
# File 'lib/openid/dh.rb', line 16

def public
  @public
end

Class Method Details

.from_defaultsObject

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



20
21
22
# File 'lib/openid/dh.rb', line 20

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

Instance Method Details

#get_shared_secret(composite) ⇒ Object



30
31
32
# File 'lib/openid/dh.rb', line 30

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

#using_default_values?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/openid/dh.rb', line 41

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

#xor_secret(algorithm, composite, secret) ⇒ Object



34
35
36
37
38
39
# File 'lib/openid/dh.rb', line 34

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)
  DiffieHellman.strxor(secret, hashed_dh_shared)
end