diff options
author | Azul <azul@riseup.net> | 2012-10-04 13:08:21 +0200 |
---|---|---|
committer | Azul <azul@riseup.net> | 2012-10-04 13:08:21 +0200 |
commit | 0c70bc88f14f9cc92a98a902a99b88a9b1f672e6 (patch) | |
tree | 7e5a3fa7c863f6e8f1628a45e3d1cebc95cfb88e /lib/srp/util.rb | |
parent | 777254f7ba10a0dd8fbee433e6a631d96e9d76f0 (diff) |
using the SRP 6a algorithm for calculating M
Diffstat (limited to 'lib/srp/util.rb')
-rw-r--r-- | lib/srp/util.rb | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/lib/srp/util.rb b/lib/srp/util.rb index 50ff9bb..1e4beac 100644 --- a/lib/srp/util.rb +++ b/lib/srp/util.rb @@ -19,6 +19,10 @@ 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 = BIG_PRIME_N) r = 1 @@ -32,7 +36,7 @@ d15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e5 # Hashes the (long) int args def sha256_int(*args) - sha256_hex(*args.map{|a| a.to_s(16)}) + sha256_hex(*args.map{|a| "%02x" % a}) end # Hashes the hex args @@ -56,9 +60,18 @@ d15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e5 protected def calculate_multiplier - # GENERATOR hex needs to be prefixed with 0 so it's not "2" -> 32 - ghex = '0%x' % GENERATOR - sha256_hex(BIG_PRIME_N.to_s(16), ghex).hex + sha256_int(BIG_PRIME_N, GENERATOR).hex + end + + # 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 end |