#requires chilkat http://www.example-code.com/ruby/default.asp class PasswordUtil MIN_KEY_SIZE = 128 # for the example, salt = "xW\x8EZ]c\xCB\x06", password = "password" # the salt in hexa is "78578E5A5D63CB06" def self.derive(password, salt) require 'chilkat' crypt = Chilkat::CkCrypt2.new() success = crypt.UnlockComponent(ENV['chilkat_license'] || "Anything for 30-day trial") if (success != true) print crypt.lastErrorText() + "\n" exit end # http://www.di-mgt.com.au/cryptoKDFs.html#examplespbkdf pwCharset = "ascii" # Hash algorithms may be: sha1, md2, md5, etc. hashAlg = "sha1" # The salt should be 8 bytes: saltHex = text_to_hexa(salt) iterationCount = 100 # Derive a 128-bit key from the password. outputBitLen = 128 enc = "hex" hexKey = crypt.pbkdf1(password,pwCharset,hashAlg,saltHex,iterationCount,outputBitLen,enc) end def self.text_to_hexa(value) value.bytes.map{|x| x.to_s(16).upcase}.map{|x| (x.length < 2) ? '0'+x : x}.join('') end def self.fill_key(key) (MIN_KEY_SIZE/8 - key.length).times{ key << '*'} && key end def self.derive_fastshop_default(key='Fast', salt='') derive(fill_key(key), salt) end end