Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
class method
OpenSSL.fixed_length_secure_compare
(::String, ::String) -> bool
OpenSSL.fixed_length_secure_compare(string, string) → true or false
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 *p2;
rb_len_t len1;
rb_len_t len2;
StringValue(str1);
StringValue(str2);
p1 = (const unsigned char *)RSTRING_PTR(str1);
p2 = (const unsigned char *)RSTRING_PTR(str2);
len1 = RSTRING_LEN(str1);
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.