Speed up ActiveSupport::SecurityUtils.fixed_length_secure_compare

by using `OpenSSL.fixed_length_secure_compare`, if available.
This commit is contained in:
Nate Matykiewicz 2020-10-22 00:03:19 -05:00
parent 8183963bcd
commit 5017b92362
2 changed files with 18 additions and 6 deletions

@ -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

@ -6,14 +6,21 @@ module SecurityUtils
#
# 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