Module: RubyHome::SRP

Defined in:
lib/ruby_home-srp.rb,
lib/ruby_home-srp/version.rb

Defined Under Namespace

Classes: Client, Verifier

Constant Summary collapse

VERSION =
'1.2.1'

Class Method Summary collapse

Class Method Details

.calc_H_AMK(xaa, xmm, xkk, n, g) ⇒ Object

H(A, M, K)



67
68
69
70
# File 'lib/ruby_home-srp.rb', line 67

def calc_H_AMK(xaa, xmm, xkk, n, g)
  hashin = [xaa, xmm, xkk].join()
  sha512_hex(hashin).hex % n
end

.calc_k(n, g) ⇒ Object

Multiplier parameter k = H(N, g) (in SRP-6a)



39
40
41
# File 'lib/ruby_home-srp.rb', line 39

def calc_k(n, g)
  H(n, n, g)
end

.calc_M(username, xsalt, xaa, xbb, xkk, n, g) ⇒ Object

M = H(H(N) xor H(g), H(I), s, A, B, K)



56
57
58
59
60
61
62
63
64
# File 'lib/ruby_home-srp.rb', line 56

def calc_M(username, xsalt, xaa, xbb, xkk, n, g)
  hn = sha512_hex(n.to_hex_string).hex
  hg = sha512_hex(g).hex
  hxor = (hn ^ hg).to_hex_string
  hi = sha512_str(username)

  hashin = [hxor, hi, xsalt, xaa, xbb, xkk].join
  sha512_hex(hashin).hex % n
end

.calc_u(xaa, xbb, n) ⇒ Object

Random scrambling parameter u = H(A, B)



51
52
53
# File 'lib/ruby_home-srp.rb', line 51

def calc_u(xaa, xbb, n)
  H(n, xaa, xbb)
end

.calc_x(username, password, salt) ⇒ Object

Private key (derived from username, raw password and salt) x = H(salt || H(username || ‘:’ || password))



45
46
47
# File 'lib/ruby_home-srp.rb', line 45

def calc_x(username, password, salt)
  sha512_hex(salt + sha512_str([username, password].join(':'))).hex
end

.H(n, *a) ⇒ Object

hashing function with padding. Input is prefixed with 0 to meet N hex width.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_home-srp.rb', line 24

def H(n, *a)
  nlen = 2 * (((n.to_hex_string).length * 4 + 7) >> 3)
  hashin = a.map {|s|
    next unless s
    shex = s.class == String ? s : s.to_hex_string
    if shex.length > nlen
      raise 'Bit width does not match - client uses different prime'
    end
    '0' * (nlen - shex.length) + shex
  }.join('')
  sha512_hex(hashin).hex % n
end

.Ng(group) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby_home-srp.rb', line 72

def Ng(group)
  case group
  when 3072
    @N = %w{
      FFFFFFFF FFFFFFFF C90FDAA2 2168C234 C4C6628B 80DC1CD1 29024E08
      8A67CC74 020BBEA6 3B139B22 514A0879 8E3404DD EF9519B3 CD3A431B
      302B0A6D F25F1437 4FE1356D 6D51C245 E485B576 625E7EC6 F44C42E9
      A637ED6B 0BFF5CB6 F406B7ED EE386BFB 5A899FA5 AE9F2411 7C4B1FE6
      49286651 ECE45B3D C2007CB8 A163BF05 98DA4836 1C55D39A 69163FA8
      FD24CF5F 83655D23 DCA3AD96 1C62F356 208552BB 9ED52907 7096966D
      670C354E 4ABC9804 F1746C08 CA18217C 32905E46 2E36CE3B E39E772C
      180E8603 9B2783A2 EC07A28F B5C55DF0 6F4C52C9 DE2BCBF6 95581718
      3995497C EA956AE5 15D22618 98FA0510 15728E5A 8AAAC42D AD33170D
      04507A33 A85521AB DF1CBA64 ECFB8504 58DBEF0A 8AEA7157 5D060C7D
      B3970F85 A6E1E4C7 ABF5AE8C DB0933D7 1E8C94E0 4A25619D CEE3D226
      1AD2EE6B F12FFA06 D98A0864 D8760273 3EC86A64 521F2B18 177B200C
      BBE11757 7A615D6C 770988C0 BAD946E2 08E24FA0 74E5AB31 43DB5BFC
      E0FD108E 4B82D120 A93AD2CA FFFFFFFF FFFFFFFF
    }.join.hex
    @g = '05'
  else
    raise NotImplementedError
  end
  return [@N, @g]
end

.sha512_hex(h) ⇒ Object



14
15
16
# File 'lib/ruby_home-srp.rb', line 14

def sha512_hex(h)
  OpenSSL::Digest::SHA512.hexdigest([h].pack('H*'))
end

.sha512_str(s) ⇒ Object



18
19
20
# File 'lib/ruby_home-srp.rb', line 18

def sha512_str(s)
  OpenSSL::Digest::SHA512.hexdigest(s)
end