Project

General

Profile

Actions

Bug #5156

closed

Problem with creating a SHA2 digest hash when string has character sequence "#$"

Added by mydoghasworms (Martin Ceronio) over 12 years ago. Updated over 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux], ruby 1.8.7 (2010-01-10 patchlevel 249) [i386-mingw32]
[ruby-core:38779]

Description

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) over 12 years ago

This is a problem of string interpolation, not SHA:

ruby-1.9.2-p290 :002 > s="968901L#$q675PwX"
=> "968901L"
ruby-1.9.2-p290 :003 > t='968901L#$q675PwX'
=> "968901L#$q675PwX"
ruby-1.9.2-p290 :004 > Digest::SHA2.new.update(s).hexdigest
=> "6b9139de7a8d0e7436e98a4e39317e54642f2e4185c31c9395ce7a12cfd56627"
ruby-1.9.2-p290 :005 > Digest::SHA2.new.update(t).hexdigest
=> "6659bd73f7295ee5ddc35dc03e46274e30c2488bacd4f7d4926e5649202918b9"

As such, you should only observe this on double-quoted literal strings.

Updated by naruse (Yui NARUSE) over 12 years ago

  • Status changed from Open to Rejected

As Alex pointed, use single quotes.

You may know Ruby expands the variable in a double quoted string literal like "a#{b}c".
Moreover Ruby has " #$a " " #@a " " #@@a ".

Actions

Also available in: Atom PDF

Like0
Like0Like0