Sha256: 96dddab02cdb6be114267216cbc39d9babee08a4a596e3c1955d5a5245716ece

Contents?: true

Size: 752 Bytes

Versions: 9

Compression:

Stored size: 752 Bytes

Contents

# typed: strict
# frozen_string_literal: true

module EML
  class ConstantTimeCompare
    extend T::Sig

    sig { params(comparison: String, expected: String).returns(T::Boolean) }
    def self.call(comparison, expected)
      new(comparison, expected).call
    end

    sig { params(comparison: String, expected: String).void }
    def initialize(comparison, expected)
      @comparison = T.let(comparison, String)
      @expected = T.let(expected, String)
    end

    sig { returns(T::Boolean) }
    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
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
eml-2.1.8 lib/eml/lib/constant_time_compare.rb
eml-2.1.7 lib/eml/lib/constant_time_compare.rb
eml-2.1.6 lib/eml/lib/constant_time_compare.rb
eml-2.1.5 lib/eml/lib/constant_time_compare.rb
eml-2.1.4 lib/eml/lib/constant_time_compare.rb
eml-2.1.3 lib/eml/lib/constant_time_compare.rb
eml-2.1.2 lib/eml/lib/constant_time_compare.rb
eml-2.1.1 lib/eml/lib/constant_time_compare.rb
eml-2.1.0 lib/eml/lib/constant_time_compare.rb