cdnbcguy
cdnbcguy

Reputation: 109

Why am I getting a hash other than the one I expected?

When I enter this code from http://ruby.about.com/od/advancedruby/ss/Cryptographic-Hashes-In-Ruby.htm

#!/usr/bin/env ruby
require 'digest'

password = "A user's password"
hash = Digest::SHA1.hexdigest(password)
puts hash

# This will produce the hash
# 62018390552aaba3d344e3b43bfa14e49e535dfc

I get the answer they said I would.

But when I enter this shell command

echo "A user's password" | openssl dgst -sha1 -hex

I get 95f33732bafc1744bf24e0cae4e014ab2e5f1580

Why, please?

Upvotes: 2

Views: 155

Answers (1)

GargantuChet
GargantuChet

Reputation: 5779

Your command-line example is including a newline, which isn't specified in the Ruby string. Try using -n so echo skips the newline:

$ echo "A user's password" | openssl dgst -sha1 -hex
95f33732bafc1744bf24e0cae4e014ab2e5f1580
$ echo -n "A user's password" | openssl dgst -sha1 -hex
62018390552aaba3d344e3b43bfa14e49e535dfc

Upvotes: 11

Related Questions