Skip to content

Instantly share code, notes, and snippets.

@verticonaut
Created January 18, 2023 10:29
Show Gist options
  • Save verticonaut/4f5d0a8e356f9066d0a3913488ed35c9 to your computer and use it in GitHub Desktop.
Save verticonaut/4f5d0a8e356f9066d0a3913488ed35c9 to your computer and use it in GitHub Desktop.
secure string comparison in ruby
# plain ruby
def secure_compare(a, b)
return false if a.empty? || b.empty? || a.bytesize != b.bytesize
l = a.unpack "C#{a.bytesize}"
res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
end
# RAILS
ActiveSupport::SecurityUtils.secure_compare(a, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment