Sha256: 663ac248970845e0bf8059825b1d64e5dba299ceab17bf539388c127ca25ca5e

Contents?: true

Size: 769 Bytes

Versions: 2

Compression:

Stored size: 769 Bytes

Contents

require 'openssl'
module SymmetricEncryption
  # Class that manages the key that is used to encrypt the encryption key.
  # Currently uses RSA asymmetric encryption to secure the key.
  #
  # Note:
  #   No encoding or decoding is performed.
  class KeyEncryptionKey
    # Returns [String] a new key encryption key.
    def self.generate(options = {})
      options = options.dup
      size    = options.delete(:size) || 2048
      OpenSSL::PKey::RSA.generate(size).to_s
    end

    def initialize(key_encryption_key)
      @rsa = OpenSSL::PKey::RSA.new(key_encryption_key)
    end

    def encrypt(key)
      rsa.public_encrypt(key)
    end

    def decrypt(encrypted_key)
      rsa.private_decrypt(encrypted_key)
    end

    private

    attr_reader :rsa
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
symmetric-encryption-3.9.1 lib/symmetric_encryption/key_encryption_key.rb
symmetric-encryption-3.9.0 lib/symmetric_encryption/key_encryption_key.rb