开发者

Where does Digest::SHA2.hexdigest() defined?

I'm following through the tutorial from the book Agile Web Development with Rails and I found the following code:

def User.encrypt_password(password, salt) 
  Digest::SHA2.hexdigest(password + "wibble" 开发者_如何学编程+ salt)
end

Looking into the Digest source code (digest.rb and digest/sha2.rb inside the lib directory in my ruby installation), however, I can't seem to find where the hexdigest method is defined, but yet the code seems to work just fine.

Can someone enlighten me how this happens? I assume that I need to look for a code that somehow looks like:

def hexdigest(...)
   ...
end


The hexdigest part, and several other similar methods are written as a C extension for speed. It's found in ext/digest/ in the Ruby source.

static VALUE rb_digest_instance_hexdigest(int argc, VALUE *argv, VALUE self) is defined on line 216 in ext/digest/digest.c in my Ruby 1.9.2-p0 source. It just calls a bunch of other functions, but it might be a starting point at least.

For SHA2, there is another ext/digest/sha2/sha2.c that contains those functions. digest.c is just the basics, "extended" by the other ones


According to http://ruby-doc.org/stdlib/libdoc/digest/rdoc/classes/Digest/Class.html

Digest is implemented in digest.rb and also digest.c (native methods). I believe what is happening here is hexdigest is a class method on Digest which Digest::SHA2 inherits. The implementation of hexdigest calls the digest class method which each digest type implements and returns the result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜