Module: MTProto::Crypto::DHKeyExchange

Defined in:
lib/mtproto/crypto/dh_key_exchange.rb

Class Method Summary collapse

Class Method Details

.compute_auth_key(g_a_bytes, b, dh_prime_bytes) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mtproto/crypto/dh_key_exchange.rb', line 27

def compute_auth_key(g_a_bytes, b, dh_prime_bytes)
  g_a = OpenSSL::BN.new(g_a_bytes, 2)
  dh_prime = OpenSSL::BN.new(dh_prime_bytes, 2)

  auth_key_bn = g_a.mod_exp(b, dh_prime)
  auth_key_bn.to_s(2)
end

.generate_client_dh_params(g, dh_prime_bytes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mtproto/crypto/dh_key_exchange.rb', line 11

def generate_client_dh_params(g, dh_prime_bytes)
  dh_prime = OpenSSL::BN.new(dh_prime_bytes, 2)
  g_bn = OpenSSL::BN.new(g)

  b = generate_random_2048_bit_number
  g_b = g_bn.mod_exp(b, dh_prime)

  DHValidator.validate_g_a(g_b, dh_prime)

  {
    b: b,
    g_b: g_b,
    g_b_bytes: g_b.to_s(2)
  }
end

.generate_random_2048_bit_numberObject



35
36
37
38
39
40
41
# File 'lib/mtproto/crypto/dh_key_exchange.rb', line 35

def generate_random_2048_bit_number
  random_bytes = SecureRandom.random_bytes(256)
  random_bytes[0] = (random_bytes[0].ord | 0x80).chr

  bn = OpenSSL::BN.new(random_bytes, 2)
  bn
end