Sha256: f3242c0967cd25326b468818803662314b97cf09feed56ac9b8a2492158f3765
Contents?: true
Size: 1.44 KB
Versions: 3
Compression:
Stored size: 1.44 KB
Contents
require_relative '../data_conversion' module Ccrypto module Ruby class HKDFEngine include DataConversion include TR::CondUtils def initialize(*args, &block) @config = args.first raise KDFEngineException, "KDF config is expected" if not @config.is_a?(Ccrypto::KDFConfig) raise KDFEngineException, "Output bit length (outBitLength) value is not given or not a positive value (#{@config.outBitLength})" if is_empty?(@config.outBitLength) or @config.outBitLength <= 0 @config.salt = SecureRandom.random_bytes(16) if is_empty?(@config.salt) end def derive(input, output = :binary) digest = init_digest(@config.digest) @config.info = "" if @config.info.nil? res = OpenSSL::KDF.hkdf(input, salt: @config.salt, info: @config.info, length: @config.outBitLength/8, hash: digest) case output when :hex to_hex(res) when :b64 to_b64(res) else res end end private def init_digest(algo) if DigestEngine.is_supported?(algo) conf = DigestEngine.engineKeys[algo] if not_empty?(conf) OpenSSL::Digest.new(conf.provider_config) else raise DigestEngineException, "Algo config '#{algo}' not found" end else raise DigestEngineException, "Digest algo '#{algo}' is not supported" 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/hkdf_engine.rb |
ccrypto-ruby-0.1.1 | lib/ccrypto/ruby/engines/hkdf_engine.rb |
ccrypto-ruby-0.1.0 | lib/ccrypto/ruby/engines/hkdf_engine.rb |