Sha256: 310c3ded010e5060838c06ecc0d6555464585288b6d1f5944ae6d0ef657c3189

Contents?: true

Size: 1.23 KB

Versions: 8

Compression:

Stored size: 1.23 KB

Contents

#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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fastshop_catalog-0.0.8 password_util.rb
fastshop_catalog-0.0.7 password_util.rb
fastshop_catalog-0.0.6 password_util.rb
fastshop_catalog-0.0.5 password_util.rb
fastshop_catalog-0.0.4 password_util.rb
fastshop_catalog-0.0.3 password_util.rb
fastshop_catalog-0.0.2 password_util.rb
fastshop_catalog-0.0.1 password_util.rb