summaryrefslogtreecommitdiff
path: root/lib/srp/util.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/srp/util.rb')
-rw-r--r--lib/srp/util.rb47
1 files changed, 25 insertions, 22 deletions
diff --git a/lib/srp/util.rb b/lib/srp/util.rb
index bf4c248..1e4beac 100644
--- a/lib/srp/util.rb
+++ b/lib/srp/util.rb
@@ -10,7 +10,7 @@ module SRP
115b8b692e0e045692cf280b436735c77a5a9e8a9e7ed56c965f87db5b2a2ece3
EOS
- BIG_PRIME_N = <<-EOS # 1024 bits modulus (N)
+ BIG_PRIME_N = <<-EOS.split.join.hex # 1024 bits modulus (N)
eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c25657
6d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089da
d15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e5
@@ -19,8 +19,12 @@ d15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e5
EOS
GENERATOR = 2 # g
+ def hn_xor_hg
+ byte_xor_hex(sha256_int(BIG_PRIME_N), sha256_int(GENERATOR))
+ end
+
# a^n (mod m)
- def modpow(a, n, m)
+ def modpow(a, n, m = BIG_PRIME_N)
r = 1
while true
r = r * a % m if n[0] == 1
@@ -30,8 +34,15 @@ d15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e5
end
end
- def sha256_hex(h)
- Digest::SHA2.hexdigest([h].pack('H*'))
+ # Hashes the (long) int args
+ def sha256_int(*args)
+ sha256_hex(*args.map{|a| "%02x" % a})
+ end
+
+ # Hashes the hex args
+ def sha256_hex(*args)
+ h = args.join('')
+ sha256_str([h].pack('H*'))
end
def sha256_str(s)
@@ -43,34 +54,26 @@ d15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e5
end
def multiplier
- return "c46d46600d87fef149bd79b81119842f3c20241fda67d06ef412d8f6d9479c58".hex % PRIME_N
@k ||= calculate_multiplier
end
protected
def calculate_multiplier
- n = PRIME_N
- g = GENERATOR
- nhex = '%x' % [n]
- nlen = nhex.length + (nhex.length.odd? ? 1 : 0 )
- ghex = '%x' % [g]
- hashin = '0' * (nlen - nhex.length) + nhex \
- + '0' * (nlen - ghex.length) + ghex
- sha256_hex(hashin).hex % n
+ sha256_int(BIG_PRIME_N, GENERATOR).hex
end
- def calculate_m(aa, bb, s)
- hashin = '%x%x%x' % [aa, bb, s]
- sha256_str(hashin).hex
+ # turn two hex strings into byte arrays and xor them
+ #
+ # returns byte array
+ def byte_xor_hex(a, b)
+ a = [a].pack('H*')
+ b = [b].pack('H*')
+ a.bytes.each_with_index.map do |a_byte, i|
+ (a_byte ^ (b[i] || 0)).chr
+ end.join
end
- def calculate_u(aa, bb, n)
- nlen = 2 * ((('%x' % [n]).length * 4 + 7) >> 3)
- aahex = '%x' % [aa]
- bbhex = '%x' % [bb]
- return sha256_str("%x%x" % [aa, bb]).hex
- end
end
end