Module: HPKE::Util

Included in:
HPKE, Context, DHKEM, HKDF
Defined in:
lib/hpke/util.rb

Instance Method Summary collapse

Instance Method Details

#i2osp(n, w) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'lib/hpke/util.rb', line 2

def i2osp(n, w)
  # check n > 0 and n < 256 ** w
  ret = []
  for i in 0..(w - 1)
    ret[w - (i + 1)] = n % 256
    n = n >> 8
  end
  ret.map(&:chr).join
end

#os2ip(x) ⇒ Object



12
13
14
# File 'lib/hpke/util.rb', line 12

def os2ip(x)
  x.bytes.reduce { |a, b| a * 256 + b }
end

#xor(a, b) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/hpke/util.rb', line 16

def xor(a, b)
  if a.bytesize != b.bytesize
    return false
  end
  c = ""
  for i in 0 .. (a.bytesize - 1)
    c += (a.bytes[i] ^ b.bytes[i]).chr
  end
  c
end