PBKDF256

Description

A very simple, but fast (native) PBKDF2-HMAC-SHA256 ruby gem based on Colin Percival’s C implementation.

This gem is mostly useful for Ruby installations that have been compiled against an OpenSSL version earlier than 0.9.9, which will be lacking PBKDF2 compliant function (i.e., OpenSSL::PKCS5.pbkdf2_hmac).

Limitation

SHA256 is the only available Hash function.

Benchmark

Performance comparison of OpenSSL, PBKDF256, and PBKDF2-ruby (Ruby) implementations.

PBKDF2(“pleaseletmein”, “SodiumChloride”, 2000, 64):

                 user     system      total        real
OpenSSL:     0.010000   0.000000   0.010000 (  0.015129)
PBKDF256:    0.020000   0.000000   0.020000 (  0.046734)
PBKDF2-ruby: 0.140000   0.000000   0.140000 (  0.153980)

PBKDF2(“pleaseletmein”, “SodiumChloride”, 100000, 64):

                 user     system      total        real
OpenSSL:     0.760000   0.020000   0.780000 (  1.452442)
PBKDF256:    0.920000   0.020000   0.940000 (  1.537676)
PBKDF2-ruby: 7.030000   0.150000   7.180000 ( 11.195839)
  • Smaller is better

Install

$ gem install pbkdf256

Examples

require 'pbkdf256'
require 'openssl'

salt = OpenSSL::Random.random_bytes(16)
# => "\x14~\xFC\xF7\xF4\xA2\xF7\xF7\x93H5\xF9nX\xB6\xA5"

PBKDF256.dk("p@ssw0rd", salt, 2000, 32)
# => "\xA4\xFF\x83*k\x820#=\x02\x0F\xD1\xE8,\x85K\xAA\x0F\x16{\xE0\xDD\x14\xA2i\x86`\xD3{\xF5^/"

PBKDF256.pbkdf2_sha256("M0$terM@gnet", salt, 1353, 16)
# => "\x80\x80.\x87SC\x96\b\x8F\x9F\xB4\xBD\xAF\x1E\x932"

PBKDF256.hmac_sha256 "The Last Samurai", salt, 1, 16 
# => "C<&\x8F\xB2(\x8D\xD8\x82\xEA\xB6l\xE5]O\b"

Requirements

Ruby, and native build environment (GCC 4.x).

Tested with Ruby 1.9.x on Mac OS X (10.8.2), and Ubuntu 12.04.

Copyright © 2012 Johanns Gregorian

See LICENSE.txt for details.