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. C/C++的mem函数和strcpy函数的区别和应用

    mem系列函数是面试的时候常考的知识点,我们需要熟练掌握这三个函数的原理和代码实现,要能准确无误的写出代码. memcpy.memset和memset三个函数在使用过程中,均需包含以下头文件: //在 ...

  2. Android中三种计时器Timer、CountDownTimer、handler.postDelayed的使用

    在android开发中,我们常常需要用到计时器,倒计时多少秒后再执行相应的功能,下面我就分别来讲讲这三种常用的计时的方法. 一.CountDownTimer 该类是个抽象类,如果要使用这个类中的方法, ...

  3. antlr v4 使用指南连载3——g4文件概览

    g4文件概览        在深入介绍之前,有必要先给大家了解一下g4文件的结构,以便对如何编写语法规则文件有个全局的认识,我想这是大有禆益的.因为这样我们就可以很清晰地知道需要的东西写在哪里,或者哪 ...

  4. Libgdx教程目录

    Libgdx教程 Note:本教程用的Libgdx 1.9.2版本,在教程更新完毕之前应该不会更新版本.之前的博客中也发表过Libgdx的内容,不过当时都从别人那里拷贝的,因此现在想做一个Libgdx ...

  5. 参数估计:最大似然估计MLE

    http://blog.csdn.net/pipisorry/article/details/51461997 最大似然估计MLE 顾名思义,当然是要找到一个参数,使得L最大,为什么要使得它最大呢,因 ...

  6. 插件开发之360 DroidPlugin源码分析(二)Hook机制

    转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52124397 前言:新插件的开发,可以说是为插件开发者带来了福音,虽然还很多坑要填补, ...

  7. (NO.00005)iOS实现炸弹人游戏(四):游戏数据的初始化(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 上一篇我们初步看了一下MainScene类的初始化方法里都做了神 ...

  8. Iframe高度自适应(兼容IEFirefox、同域跨域)

    在实际的项目进行中,很多地方可能由于历史原因不得不去使用iframe,包括目前正火热的应用开发也是如此. 随之而来的就是在实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固 ...

  9. Oracle EBS SLA 详解

    SLA概述 SLA :子分类账(Subledger Accounting),这个在R12中大力宣扬的内容,我们通常的认为总账就是对Journal的汇总,但是在实际的操作中我们会发现,对于Sub sys ...

  10. 海量数据挖掘MMDS week6: 决策树Decision Trees

    http://blog.csdn.net/pipisorry/article/details/49445465 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...