End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
OpenSSL.fixed_length_secure_compare
OpenSSL.fixed_length_secure_compare(string, string) → boolean
ext/openssl/ossl.c
View on GitHub ↗
static VALUE
ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
{
const unsigned char *p1 = (const unsigned char *)StringValuePtr(str1);
const unsigned char *p2 = (const unsigned char *)StringValuePtr(str2);
long len1 = RSTRING_LEN(str1);
long len2 = RSTRING_LEN(str2);
if (len1 != len2) {
ossl_raise(rb_eArgError, "inputs must be of equal length");
}
switch (CRYPTO_memcmp(p1, p2, len1)) {
case 0: return Qtrue;
default: return Qfalse;
}
}
Constant time memory comparison for fixed length strings, such as results of HMAC calculations.
Returns true if the strings are identical, false if they are of the same length but not identical. If the length is different, ArgumentError is raised.
See also
.fips_mode=
.mem_check_start
.fips_mode
.print_mem_leaks
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0