Module: SRP::Util

Included in:
Client, Session
Defined in:
lib/srp/util.rb

Constant Summary collapse

PRIME_N =

constants both sides know in this case taken from srp-js

<<-EOS.split.join.hex
115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3
EOS
BIG_PRIME_N =

1024 bits modulus (N)

<<-EOS.split.join.hex # 1024 bits modulus (N)
eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c25657
6d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089da
d15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e5
7ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9
GENERATOR =

g

2

Instance Method Summary collapse

Instance Method Details

#bigrand(bytes) ⇒ Object



52
53
54
# File 'lib/srp/util.rb', line 52

def bigrand(bytes)
  OpenSSL::Random.random_bytes(bytes).unpack("H*")[0]
end

#hn_xor_hgObject



22
23
24
# File 'lib/srp/util.rb', line 22

def hn_xor_hg
  byte_xor_hex(sha256_int(BIG_PRIME_N), sha256_int(GENERATOR))
end

#modpow(a, n, m = BIG_PRIME_N) ⇒ Object

a^n (mod m)



27
28
29
30
31
32
33
34
35
# File 'lib/srp/util.rb', line 27

def modpow(a, n, m = BIG_PRIME_N)
  r = 1
  while true
    r = r * a % m if n[0] == 1
    n >>= 1
    return r if n == 0
    a = a * a % m
  end
end

#multiplierObject



56
57
58
# File 'lib/srp/util.rb', line 56

def multiplier
  @muliplier ||= calculate_multiplier
end

#sha256_hex(*args) ⇒ Object

Hashes the hex args



43
44
45
46
# File 'lib/srp/util.rb', line 43

def sha256_hex(*args)
  h = args.map{|a| a.length.odd? ? "0#{a}" : a }.join('')
  sha256_str([h].pack('H*'))
end

#sha256_int(*args) ⇒ Object

Hashes the (long) int args



38
39
40
# File 'lib/srp/util.rb', line 38

def sha256_int(*args)
  sha256_hex(*args.map{|a| "%02x" % a})
end

#sha256_str(s) ⇒ Object



48
49
50
# File 'lib/srp/util.rb', line 48

def sha256_str(s)
  Digest::SHA2.hexdigest(s)
end