Module: JOSE::JWA

Extended by:
JWA
Included in:
JWA
Defined in:
lib/jose/jwa.rb

Defined Under Namespace

Modules: AES_KW, ConcatKDF, Curve25519, Curve25519_RbNaCl, Curve25519_Ruby, Curve25519_Unsupported, Curve448, Curve448_Ruby, Curve448_Unsupported, Ed25519, Ed25519_RbNaCl, Ed448, PKCS1, PKCS7, SHA3, X25519, X25519_RbNaCl, X448 Classes: Edwards25519Point, Edwards448Point, EdwardsPoint, FieldElement

Constant Summary collapse

UCHAR_PACK =
'C*'.freeze
ZERO_PAD =
[0].pack('C').force_encoding('BINARY').freeze

Instance Method Summary collapse

Instance Method Details

#constant_time_compare(a, b) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/jose/jwa.rb', line 9

def constant_time_compare(a, b)
  return false if a.empty? || b.empty? || a.bytesize != b.bytesize
  l = a.unpack "C#{a.bytesize}"

  res = 0
  b.each_byte { |byte| res |= byte ^ l.shift }
  return res == 0
end

#exor(a, b) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/jose/jwa.rb', line 18

def exor(a, b)
  a = a.to_bn if a.respond_to?(:to_bn)
  b = b.to_bn if b.respond_to?(:to_bn)
  a = a.to_s(2) if a.is_a?(OpenSSL::BN)
  b = b.to_s(2) if b.is_a?(OpenSSL::BN)
  as = a.bytesize
  bs = b.bytesize
  a.ljust!(bs, ZERO_PAD) if as < bs
  b.ljust!(as, ZERO_PAD) if bs < as
  return OpenSSL::BN.new(a.unpack(UCHAR_PACK).zip(b.unpack(UCHAR_PACK)).map do |ac,bc|
    next (ac ^ bc)
  end.reverse.pack(UCHAR_PACK), 2)
end