Sha256: 59b65681130c4bb87aa1cb816a0c58e07a2a2030fa55863debddd1fdafa83c4a

Contents?: true

Size: 464 Bytes

Versions: 1

Compression:

Stored size: 464 Bytes

Contents

module SecureCompare
  require "securecompare/version"

  # constant-time comparison algorithm to prevent timing attacks; borrowed from ActiveSupport::MessageVerifier
  def secure_compare(a, b)
    return false unless a.bytesize == b.bytesize

    l = a.unpack("C#{a.bytesize}")

    res = 0
    b.each_byte { |byte| res |= byte ^ l.shift }
    res == 0
  end
  module_function :secure_compare

  class << self
    alias_method :compare, :secure_compare
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
securecompare-1.0.0 lib/securecompare.rb