Class: EML::ConstantTimeCompare

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/eml/lib/constant_time_compare.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(comparison, expected) ⇒ ConstantTimeCompare

Returns a new instance of ConstantTimeCompare.



14
15
16
17
# File 'lib/eml/lib/constant_time_compare.rb', line 14

def initialize(comparison, expected)
  @comparison = T.let(comparison, String)
  @expected = T.let(expected, String)
end

Class Method Details

.call(comparison, expected) ⇒ Object



9
10
11
# File 'lib/eml/lib/constant_time_compare.rb', line 9

def self.call(comparison, expected)
  new(comparison, expected).call
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/eml/lib/constant_time_compare.rb', line 20

def call
  return false if @comparison.length != @expected.length

  result = 0

  transposed = [@comparison.bytes, @expected.bytes].transpose
  Hash[transposed].each { |x, y| result |= x ^ y }

  result.zero?
end