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技巧001:求md5散列的更多相关文章

  1. shiro中自定义realm实现md5散列算法加密的模拟

    shiro中自定义realm实现md5散列算法加密的模拟.首先:我这里是做了一下shiro 自定义realm散列模拟,并没有真正链接数据库,因为那样东西就更多了,相信学到shiro的人对连接数据库的一 ...

  2. C#、WinForm、ASP.NET - Md5散列加密

     MD5值概念解释: 转载自:http://free0007.iteye.com/blog/2047163 所 谓MD5,即"Message-Digest Algorithm 5(信息-摘要 ...

  3. MD5散列算法的示例

    在很多地方,都用到了数据加密,比较多的就是MD5了,也比较安全,下面就贴上个示例,输入一串字符串,通过MD5加密 加密算法如下 public static string MD5_Encrypt(str ...

  4. 加密算法和MD5等散列算法的区别(转)

    本文转自http://www.cnblogs.com/eternalwt/archive/2013/03/21/2973807.html 感谢作者 1.在软件开发的用户注册功能中常出现MD5加密这个概 ...

  5. 个人理解c#对称加密 非对称加密 散列算法的应用场景

    c#类库默认实现了一系列加密算法在System.Security.Cryptography; 命名空间下 对称加密 通过同一密匙进行加密和解密.往往应用在内部数据传输情况下.比如公司a程序 和B程序 ...

  6. java学习-sha1散列算法

    直接调用HashKit.sha1(String str)方法就可以了,,返回的是16进制的字符串长度是40, 也就是用md.digest()方法解析出来的字节数是160字节长度. 而MD5散列算法生成 ...

  7. Shiro入门学习之散列算法与凭证配置(六)

    一.散列算法概述 散列算法一般用于生成数据的摘要信息,是一种不可逆的算法,一般适合存储密码之类的数据,常见的散列算法如MD5.SHA等,一般进行散列时最好提供一个salt(“盐”),什么意思?举个栗子 ...

  8. Java 消息摘要 散列 MD5 SHA

    package xxx.common.util; import java.math.BigInteger; import java.security.MessageDigest; import jav ...

  9. java加密算法--MD5加密和哈希散列带秘钥加密算法源码

    package com.ompa.common.utils; import java.security.MessageDigest; import java.security.NoSuchAlgori ...

随机推荐

  1. Swift基础之对FMDB第三方的使用方法

    相信大家都熟悉OC使用FMDB第三方库,进行数据库操作,增.删.改.查,现在我就来利用代码展示一下Swift对此库的使用方法,我是通过Pods添加的第三方库,如果手动添加记得创建桥接文件,在文件中调用 ...

  2. iOS完整预装字体清单

    iOS完整预装字体清单:http://iosfonts.com/

  3. 剑指Offer——回溯算法

    剑指Offer--回溯算法 什么是回溯法 回溯法实际是穷举算法,按问题某种变化趋势穷举下去,如某状态的变化用完还没有得到最优解,则返回上一种状态继续穷举.回溯法有"通用的解题法"之 ...

  4. MyBatis与MySQL交互

    MyBatis是我接触到的第一个框架,下面谈一谈我第一次使用MyBatis时的感悟. 首先是一些准备工作 下载相关的jar包.到GitHub上就行,上面有全面和完整的jar文件 在eclipse上安装 ...

  5. MySQL设计软件登录模块

    学了一段时间的Java了,思量着做一点简单的小模块的东西吧,于是就有了下面的这个简单的小案例. 大致实现的功能就是注册于登录还有就是用到了一点,分层思想.仅此而已,所以非常的适合新手围观. 建立好数据 ...

  6. App引导界面,可以这么玩

    什么是ViewPager,刚一听到这个词,我们可能感觉很奇怪,但是我相信我们大部分人都曾见到过这些界面的.其实它就是我们在安装好一个app之后第一次使用时的那些引导界面的效果.这就是通过ViewPag ...

  7. C++中const加强

    demo // C语言中的const是一个冒牌货 int main() { // 好像a是一个常量 const int a = 10; int *p = NULL; p = (int *)&a ...

  8. java虚拟机工具入门

    jps 能显示现在都有那些java程序运行 C:\Users\Administrator>jps 16964 DeadLockJstack 9172 PULSEI~1.JAR 19392 Jps ...

  9. 流量控制闸门——LimitLatch套接字连接数限制器

    Tomcat作为web服务器,对于每个客户端的请求将给予处理响应,但对于一台机器而言,访问请求的总流量有高峰期且服务器有物理极限,为了保证web服务器不被冲垮我们需要采取一些措施进行保护预防,需要稍微 ...

  10. FFmpeg深入分析(一)

    最近在做一个关于监控的项目,要在iphone 客户端实现播放监控的实时视频以及录像视频.使用到了FFmpeg,看到这篇文章,写的非常不错.转自:http://blog.chinaunix.net/ui ...