ruby-mhash Build Status

Mhash wrapping library for Ruby.

Requirements

You need mhash libray installed.

Debian/Ubuntu

$ sudo apt-get install libmhash-dev

FreeBSD (using ports)

# cd /usr/ports/security/mhash
# make install

Rubies

Tested with Ruby:

  • 1.8.7
  • 1.9.2
  • 1.9.3
  • 2.0.0-preview1

You have to setup rubygems.

Installation

gem install mhash

or in Gemfile (Bundler):

gem 'mhash'

Development/Debug

git clone https://github.com/TibshoOT/ruby-mhash.git
cd ruby-mhash/etx/mhash
[edit source files]
ruby extconf.rb
make
irb -r ./mhash.so
[test it !]
[pull request]

Documentation

These are implemented hashes with their methods:

Cipher (Mhash contanst) digest method hexdigest method
CRC32 Mhash.crc32("a simple test !") Mhash.crc32!("a simple test !")
MD5 Mhash.md5("a simple test !") Mhash.md5!("a simple test !")
SHA1 Mhash.sha1("a simple test !") Mhash.sha1!("a simple test !")
HAVAL256 Mhash.haval256("a simple test !") Mhash.haval256!("a simple test !")
RIPEMD160 Mhash.ripemd160("a simple test !") Mhash.ripemd160!("a simple test !")
TIGER192 Mhash.tiger192("a simple test !") Mhash.tiger192!("a simple test !")
GOST Mhash.gost("a simple test !") Mhash.gost!("a simple test !")
CRC32B Mhash.crc32b("a simple test !") Mhash.crc32b!("a simple test !")
HAVAL224 Mhash.haval224("a simple test !") Mhash.haval224!("a simple test !")
HAVAL192 Mhash.haval192("a simple test !") Mhash.haval192!("a simple test !")
HAVAL160 Mhash.haval160("a simple test !") Mhash.haval160!("a simple test !")
HAVAL128 Mhash.haval128("a simple test !") Mhash.haval128!("a simple test !")
TIGER128 Mhash.tiger128("a simple test !") Mhash.tiger128!("a simple test !")
TIGER160 Mhash.tiger160("a simple test !") Mhash.tiger160!("a simple test !")
SHA256 Mhash.sha256("a simple test !") Mhash.sha256!("a simple test !")
ADLER32 Mhash.adler32("a simple test !") Mhash.adler32!("a simple test !")
SHA224 Mhash.sha224("a simple test !") Mhash.sha224!("a simple test !")
SHA512 Mhash.sha512("a simple test !") Mhash.sha512!("a simple test !")
SHA384 Mhash.sha384("a simple test !") Mhash.sha384!("a simple test !")
WHIRLPOOL Mhash.whirlpool("a simple test !") Mhash.whirlpool!("a simple test !")
RIPEMD128 Mhash.ripemd128("a simple test !") Mhash.ripemd128!("a simple test !")
RIPEMD256 Mhash.ripemd256("a simple test !") Mhash.ripemd256!("a simple test !")
RIPEMD320 Mhash.ripemd320("a simple test !") Mhash.ripemd320!("a simple test !")
SNEFRU128 Mhash.snefru128("a simple test !") Mhash.snefru128!("a simple test !")
SNEFRU256 Mhash.snefru256("a simple test !") Mhash.snefru256!("a simple test !")

Examples

You can find all available hashes doing this:


require 'mhash'

Mhash.constant
=> [:MHASH_CRC32, :MHASH_MD5, :MHASH_SHA1, :MHASH_HAVAL256, :MHASH_RIPEMD160,
    :MHASH_TIGER192, :MHASH_GOST, :MHASH_CRC32B, :MHASH_HAVAL224, :MHASH_HAVAL192,
    :MHASH_HAVAL160, :MHASH_HAVAL128, :MHASH_TIGER128, :MHASH_TIGER160, :MHASH_SHA256,
    :MHASH_ADLER32, :MHASH_SHA224, :MHASH_SHA512, :MHASH_SHA384, :MHASH_WHIRLPOOL,
    :MHASH_RIPEMD128, :MHASH_RIPEMD256, :MHASH_RIPEMD320, :MHASH_SNEFRU128,
    :MHASH_SNEFRU256, :VERSION]

If you just want to have a nibble digest:


require 'mhash'

hash = Mhash.gost("Hello\n\n")
puts hash
=> "\x89\r\x02\xF9*\xF7\xD7V\x82\xD4\xC3\x15\xEC\xA2\xF5\x8E~\x9E\xD0\x8D\xFC\xF1\xC0O\xC4\x16\xB0HOE\x1D\x8D"

If you want an hexdigest:


require 'mhash'

# Using gost nibble digest as previously:
hash = Mhash.gost("Hello\n\n").unpack("H*").first
puts hash
=> "890d02f92af7d75682d4c315eca2f58e7e9ed08dfcf1c04fc416b0484f451d8d"
# Using ! methods:
hash = Mhash.gost!("Hello\n\n")
puts hash
=> "890d02f92af7d75682d4c315eca2f58e7e9ed08dfcf1c04fc416b0484f451d8d"

Want a feature ? Problem ?

Open an issue ;)