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. 转义字符\(在hive+shell以及java中注意事项):正则表达式的转义字符为双斜线,split函数解析也是正则

    转义字符 将后边字符转义,使特殊功能字符作为普通字符处理,或者普通字符转化为特殊功能字符. 各个语言中都用应用,如java.python.sql.hive.shell等等. 如sql中 "\ ...

  2. 【Android应用开发】Android Studio 简介 (Android Studio Overview)

    一. Intelij IDEA 环境简介 Android Studio 来源 : Android Studio 是 Intelij IDEA 的免费版本 + Android SDK 集成的; -- I ...

  3. Hibernate单表操作

    单一主键 assigned:由Java应用程序负责生成(即手工的赋值) native:由底层的数据库自动的生成标示符,如果是MySQL就是auto_increment,如果是Oracle就是seque ...

  4. 【java集合框架源码剖析系列】java源码剖析之TreeSet

    本博客将从源码的角度带领大家学习TreeSet相关的知识. 一TreeSet类的定义: public class TreeSet<E> extends AbstractSet<E&g ...

  5. 初探linux子系统集之i2c子系统(二)

    大概也是前年了,一直没有把那个i2c的子系统讲解完,这里偷个懒,把以前整理的i2c相关的知识再梳理一下,做个了结,然后再去学习timer子系统. 先看下i2c在内核中的代码分布: obj-$(CONF ...

  6. Xcode中Objc动态调用方法同时避免警告的几个办法

    我们在Xcode中使用objc写代码的时候往往会碰到动态调用方法的时候. 如果是静态调用这很常见,不会有任何问题: [self performSelector:@selector(method)]; ...

  7. eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样

    eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样 第一种说法: 还真的找到解决的方法了, ...

  8. java设计模式---三种工厂模式

    工厂模式提供创建对象的接口. 工厂模式分为三类:简单工厂模式(Simple Factory), 工厂方法模式(Factory Method)和抽象工厂模式(Abstract Factory).GOF在 ...

  9. UNIX环境高级编程——记录上锁(fcntl函数)以及死锁检测

    一.记录锁 record locking 功能:当一个进程正在读或修改文件的某个部分时,它可以阻止其它进程修改同一文件区. 字节范围锁 byte-range locking 二.历史 flock函数, ...

  10. 简述Java内存泄露

    翻译人员: 铁锚翻译时间: 2013年11月4日原文链接: The Introduction of Memory Leaks内存管理一直是Java 所鼓吹的强大优点.开发者只需要简单地创建对象,而Ja ...