Sha256: f495c0ee845e9139e059a029e1e864ad3ada80305153afc22639103f97500979

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require_relative '../data_conversion'

module Ccrypto
  module Java
    
    class BCryptEngine
      include TR::CondUtils
      include DataConversion

      def initialize(*args, &block)
        @config = args.first

        raise KDFEngineException, "Ccrypto::BCryptConfig is expected. Given #{@config}" if not @config.is_a?(Ccrypto::BCryptConfig)

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

      def derive(input, output = :binary)
        
        begin

          binput = to_java_bytes(input)

          #logger.debug "bcrypt input : #{binput.inspect}"
          logger.debug "bcrypt salt : #{to_hex(@config.salt)}"
          logger.debug "bcrypt cost : #{@config.cost}"

          res = org.bouncycastle.crypto.generators.BCrypt.generate(binput, to_java_bytes(@config.salt), @config.cost)

          case output
          when :b64
            to_b64(res)
          when :hex
            to_hex(res)
          else
            res
          end

        rescue Exception => ex
          raise KDFEngineException, ex
        end
        
      end


      private
      def logger
        Ccrypto::Java.logger(:bcrypt)
      end

      
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ccrypto-java-0.2.0 lib/ccrypto/java/engines/bcrypt_engine.rb