Sha256: 8b9e2c9c20b1796f340a575dcac1ad86d61073ce58392a0302e05feef1b9301e

Contents?: true

Size: 701 Bytes

Versions: 1

Compression:

Stored size: 701 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 = comparison
      @expected = expected
    end

    sig { returns(T::Boolean) }
    def call
      return false if @comparison.length != @expected.length

      result = 0

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

      result.zero?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eml-2.0.0 lib/eml/lib/constant_time_compare.rb