摘要:Ruby 1.9.3中的MD5



我在这里遇到了一些奇怪的事情。我有一个"验证器",它依赖ND5来散列我们匹配的某个字符串作为密码。当我运行测试时,问题是:

NoMethodError: undefined method `md5' for #<CASServer::Authenticators::Billing:0x007fd8e6c906a0>
./models/authenticators/billing.rb:63:in `validate'
./routes/login.rb:166:in `block (2 levels) in <class:Server>'
./routes/login.rb:158:in `each'
./routes/login.rb:158:in `block in <class:Server>'
(eval):2:in `click_button'
./features/step_definitions/when_steps.rb:32:in `/^I enter "(.*)" as username and the generated username password and log in$/'
./features/rubycas.login.feature:14:in `When I enter "username" as username and the generated username password and log in'

所以基本上他不承认MD5是摘要库的一部分。在IDE和IRB控制台中运行测试时会出现此问题:

1.9.3-p125 :001 > require "digest/md5" and Digest::MD5("test")
NoMethodError: undefined method `MD5' for Digest:Module

然而,当我运行以下程序时:

[root@DCUDEV01 /home/morn/rubycas/current]# ruby
require "digest/md5" and Digest::MD5("test")

我没有收到任何错误、转储或异常。Ruby只是接受了。为了让MD5的东西发挥作用,我缺少了什么?

摘要::MD5不是一个方法,而是一个模块。尝试

Digest::MD5.digest("test")

我觉得很困惑,.digest似乎不正确。我也不能说这是错误的。。。

评论人@recombot说得对,IMO,但我是在回来改进QA后才看到评论的,我认为评论不够明显。

http://ruby-doc.org/stdlib-2.4.0/libdoc/digest/rdoc/Digest/MD5.html

下面是md5 hasing的用法示例,用于gibbon用法,即mailchimp gem。

md5_hashed_email_address = Digest::MD5.hexdigest("john.doe@example.com")

最新更新