Merge pull request #40429 from natematykiewicz/openssl_fixed_length_secure_compare

Speed up `ActiveSupport::SecurityUtils.fixed_length_secure_compare`
This commit is contained in:
Rafael França 2020-10-22 14:42:31 -04:00 committed by GitHub
commit 7eb855bfbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -1,3 +1,8 @@
* Speed up `ActiveSupport::SecurityUtils.fixed_length_secure_compare` by using
`OpenSSL.fixed_length_secure_compare`, if available.
*Nate Matykiewicz*
* `ActiveSupport::Cache::MemCacheStore` now checks `ENV["MEMCACHE_SERVERS"]` before falling back to `"localhost:11211"` if configured without any addresses.
```ruby

View File

@ -6,14 +6,21 @@ module ActiveSupport
#
# The values compared should be of fixed length, such as strings
# that have already been processed by HMAC. Raises in case of length mismatch.
def fixed_length_secure_compare(a, b)
raise ArgumentError, "string length mismatch." unless a.bytesize == b.bytesize
l = a.unpack "C#{a.bytesize}"
if defined?(OpenSSL.fixed_length_secure_compare)
def fixed_length_secure_compare(a, b)
OpenSSL.fixed_length_secure_compare(a, b)
end
else
def fixed_length_secure_compare(a, b)
raise ArgumentError, "string length mismatch." unless a.bytesize == b.bytesize
res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
l = a.unpack "C#{a.bytesize}"
res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
end
end
module_function :fixed_length_secure_compare