Problem with creating a SHA2 digest hash when string has character sequence "#$"
When calculating a SHA2 hash in Ruby using Digest::SHA2, it gives weird results.
Consider the following string: "968901L#$q675PwX" which has a SHA2 hash of 6659bd73f7295ee5ddc35dc03e46274e30c2488bacd4f7d4926e5649202918b9 (confirmed with http://www.xorbin.com/tools/sha256-hash-calculator and ABAP language as well).
Using Digest::SHA2 in Ruby, the hash is 6b9139de7a8d0e7436e98a4e39317e54642f2e4185c31c9395ce7a12cfd56627.
Where things seem to fall apart is from the hash and dollar sign ("#$") onwards. If you try hash a string ending in "#$" with Digest::SHA2, it gives an exception in Ruby ("unterminated string meets end of file").
Tested under Windows and Linux with Ruby 1.8.7. Specifically:
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux]
ruby 1.8.7 (2010-01-10 patchlevel 249) [i386-mingw32]
Updated by regularfry (Alex Young) about 12 years ago
This is a problem of string interpolation, not SHA:
ruby-1.9.2-p290 :002 > s="968901L#$q675PwX"
ruby-1.9.2-p290 :003 > t='968901L#$q675PwX'
ruby-1.9.2-p290 :004 > Digest::SHA2.new.update(s).hexdigest
ruby-1.9.2-p290 :005 > Digest::SHA2.new.update(t).hexdigest
As such, you should only observe this on double-quoted literal strings.