Sha256: 88c741971632ba23cf71cf27babaaf21f3ca62e228a9bf8577874e15463ca6b4

Contents?: true

Size: 751 Bytes

Versions: 2

Compression:

Stored size: 751 Bytes

Contents

# typed: false
# 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

2 entries across 2 versions & 1 rubygems

Version Path
eml-3.0.0 lib/eml/lib/constant_time_compare.rb
eml-2.2.0 lib/eml/lib/constant_time_compare.rb