File: fixed_length_secure_compare.rb

package info (click to toggle)
ruby-android-key-attestation 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 216 kB
  • sloc: ruby: 352; makefile: 7; sh: 4
file content (21 lines) | stat: -rw-r--r-- 629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# frozen_string_literal: true

require "openssl"

module AndroidKeyAttestation
  module FixedLengthSecureCompare
    unless OpenSSL.singleton_class.method_defined?(:fixed_length_secure_compare)
      refine OpenSSL.singleton_class do
        def fixed_length_secure_compare(a, b) # rubocop:disable Naming/UncommunicativeMethodParamName
          raise ArgumentError, "inputs must be of equal length" unless a.bytesize == b.bytesize

          # borrowed from Rack::Utils
          l = a.unpack("C*")
          r, i = 0, -1
          b.each_byte { |v| r |= v ^ l[i += 1] }
          r == 0
        end
      end
    end
  end
end