Sha256: d18e1d4e4940282f351213f9a809c5c11307f1a551df34482aaa0ccdfc730ccd

Contents?: true

Size: 977 Bytes

Versions: 3

Compression:

Stored size: 977 Bytes

Contents

require_relative '../data_conversion'

module Ccrypto
  module Ruby
    class ScryptEngine
      include DataConversion
      include TR::CondUtils

      def initialize(conf, &block)
        raise KDFEngineException, "KDF config is expected" if not conf.is_a?(Ccrypto::KDFConfig)
        raise KDFEngineException, "Output bit length (outBitLength) value is not given or not a positive value (#{conf.outBitLength})" if is_empty?(conf.outBitLength) or conf.outBitLength <= 0
        @config = conf

        if is_empty?(@config.salt)
          @config.salt = SecureRandom.random_bytes(16)
        end
      end

      def derive(input, output = :binary)
        res = OpenSSL::KDF.scrypt(input, salt: @config.salt, N: @config.cost, r: @config.blockSize, p: @config.parallel, length: @config.outBitLength/8)  
        case output
        when :hex
          to_hex(res)
        when :b64
          to_b64(res)
        else
          res
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ccrypto-ruby-0.1.2 lib/ccrypto/ruby/engines/scrypt_engine.rb
ccrypto-ruby-0.1.1 lib/ccrypto/ruby/engines/scrypt_engine.rb
ccrypto-ruby-0.1.0 lib/ccrypto/ruby/engines/scrypt_engine.rb