ITKeyword,专注技术干货聚合推荐

注册 | 登录

ruby技巧001:求md5散列

mydo 分享于 2014-08-02

推荐:generate an MD5 digest for a file

The MessageDigestForFile class demonstrates the generation of an MD5W digest for a file. The MD5 digest is displayed in hex using the Hex class from t

2018阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)
领取地址https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=zo93kaue

    ruby核心库中未包含md5之类的功能,不过在其标准库digest中可以方便的使用该功能:

= Digest

(from ruby core)
------------------------------------------------------------------------------

This module provides a framework for message digest libraries.

You may want to look at OpenSSL::Digest as it supports more algorithms.

A cryptographic hash function is a procedure that takes data and returns a
fixed bit string: the hash value, also known as digest. Hash
functions are also called one-way functions, it is easy to compute a digest
from a message, but it is infeasible to generate a message from a digest.

== Examples

  require 'digest'

  # Compute a complete digest
  Digest::SHA256.digest 'message'       #=> "\xABS\n\x13\xE4Y..."

  sha256 = Digest::SHA256.new
  sha256.digest 'message'               #=> "\xABS\n\x13\xE4Y..."

  # Other encoding formats
  Digest::SHA256.hexdigest 'message'    #=> "ab530a13e459..."
  Digest::SHA256.base64digest 'message' #=> "q1MKE+RZFJgr..."

  # Compute digest by chunks
  md5 = Digest::MD5.new
  md5.update 'message1'
  md5 << 'message2'                     # << is an alias for update

  md5.hexdigest                         #=> "94af09c09bb9..."

  # Compute digest for a file
  sha256 = Digest::SHA256.file 'testfile'
  sha256.hexdigest

Additionally digests can be encoded in "bubble babble" format as a sequence of
consonants and vowels which is more recognizable and comparable than a
hexadecimal digest.

  require 'digest/bubblebabble'

  Digest::SHA256.bubblebabble 'message' #=> "xopoh-fedac-fenyh-..."

See the bubble babble specification at
http://web.mit.edu/kenta/www/one/bubblebabble/spec/jrtrjwzi/draft-huima-01.txt
.

== Digest algorithms

Different digest algorithms (or hash functions) are available:

HMAC:
  See FIPS PUB 198 The Keyed-Hash Message Authentication Code (HMAC).

RIPEMD-160:
  As Digest::RMD160. See
  http://homes.esat.kuleuven.be/~bosselae/ripemd160.html.

SHA1:
  See FIPS 180 Secure Hash Standard.

SHA2 family:
  See FIPS 180 Secure Hash Standard which defines the following algorithms:
  * SHA512
  * SHA384
  * SHA256


The latest versions of the FIPS publications can be found here:
http://csrc.nist.gov/publications/PubsFIPS.html.





------------------------------------------------------------------------------
= Class methods:

  bubblebabble
  hexencode


代码实例如下:

irb(main):006:0> h=Digest::MD5.new
=> #<Digest::MD5: d41d8cd98f00b204e9800998ecf8427e>
irb(main):007:0> h.methods
=> [:reset, :update, :<<, :digest_length, :block_length, :==, :inspect, :new, :digest, :digest!, :hexdigest, :hexdigest!, 
:to_s, :length, :size, :file, :base64digest, :base64digest!, :nil?, :===, :=~, :!~, :eql?, :hash, :<=>, :class, :singleton_class,
 :clone, :dup, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :freeze, :frozen?, :methods, :singleton_methods,
 :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, 
:instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, 
:extend, :display, :method, :public_method, :singleton_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?, 
:!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
irb(main):008:0> h<<"aaa"
=> #<Digest::MD5: 47bce5c74f589f4867dbd57e9ca9f808>
irb(main):009:0> h.hexdigest
=> "47bce5c74f589f4867dbd57e9ca9f808"
irb(main):010:0> h<<"aaa"
=> #<Digest::MD5: 0b4e7a0e5fe84ad35fb5f95b9ceeac79>
irb(main):011:0> h.hexdigest
=> "0b4e7a0e5fe84ad35fb5f95b9ceeac79"


    ruby核心库中未包含md5之类的功能,不过在其标准库digest中可以方便的使用该功能: = Digest(from ruby core)-------------------------------------------------------------------------

相关阅读排行


用户评论

游客

相关内容推荐

阿里云RDS

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。