ruby/ruby — pull request #688 by arrtchiu (2014-07-28 15:02:57+00:00)
Title: add timing-safe hmac verify method

I could be totally wrong, but it seems the standard library doesn't provide a reliable way of comparing hashes in constant-time.

- The docs for OpenSSL::HMAC [encourage the use of `Digest#to_s`](http://ruby-doc.org/stdlib-2.1.0/libdoc/openssl/rdoc/OpenSSL/HMAC.html#method-c-new).
- Ruby's string comparison [uses memcmp](http://rxr.whitequark.org/mri/source/string.c#2382), which isn't timing safe.

With this patch I propose to add an additional method, `OpenSSL::HMAC#verify`, which takes a binary string with a digest and compares it against the computed hash.

I've also logged a feature request at https://bugs.ruby-lang.org/issues/10098 - since this is my first time contributing I'm not sure what constitutes a "tiny fix"; here's a pull request too.