Module: WSDL::Security::SecureCompare
- Defined in:
- lib/wsdl/security/secure_compare.rb
Overview
Provides timing-safe comparison utilities for cryptographic values.
Standard string comparison (==) short-circuits on the first differing
character, which leaks timing information about how many characters match.
This can be exploited in timing attacks to gradually guess secret values
by measuring response times.
This module wraps Ruby's OpenSSL.secure_compare to provide constant-time
string comparison that prevents such attacks.
Class Method Summary collapse
-
.equal?(expected, actual) ⇒ Boolean
Performs a timing-safe comparison of two strings.
Class Method Details
.equal?(expected, actual) ⇒ Boolean
Performs a timing-safe comparison of two strings.
This method uses OpenSSL.secure_compare which:
- Hashes both inputs with SHA-256 to normalize lengths
- Uses constant-time comparison on the hashes
- Performs a final equality check
This ensures the comparison time is independent of:
- How many characters match
- The length of either string
51 52 53 54 55 |
# File 'lib/wsdl/security/secure_compare.rb', line 51 def equal?(expected, actual) return false unless expected.is_a?(String) && actual.is_a?(String) OpenSSL.secure_compare(expected, actual) end |