1
0
Fork 0

Add virtual attribute RSAPublicKey#private_key_pem

This commit is contained in:
Alex Kotov 2019-09-12 07:10:50 +05:00
parent 3df7acd095
commit 4f41d2d0e4
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
8 changed files with 16 additions and 26 deletions

View file

@ -9,11 +9,10 @@ class CreateRSAKeys
before :set_ciphertext before :set_ciphertext
def call def call
context.private_key_pem = @pkey.to_pem.freeze
context.public_key = RSAPublicKey.create!( context.public_key = RSAPublicKey.create!(
bits: BITS, bits: BITS,
public_key_pem: @pkey.public_key.to_pem.freeze, public_key_pem: @pkey.public_key.to_pem.freeze,
private_key_pem: @pkey.to_pem.freeze,
private_key_pem_iv: @iv, private_key_pem_iv: @iv,
private_key_pem_secret: @key, private_key_pem_secret: @key,
private_key_pem_ciphertext: @ciphertext, private_key_pem_ciphertext: @ciphertext,

View file

@ -14,7 +14,8 @@ class CreateX509CertificateRequest
private private
def private_key_pkey def private_key_pkey
@private_key_pkey ||= OpenSSL::PKey::RSA.new context.private_key_pem @private_key_pkey ||=
OpenSSL::PKey::RSA.new context.public_key.private_key_pem
end end
def public_key_pkey def public_key_pkey

View file

@ -22,7 +22,8 @@ class CreateX509SelfSignedCertificate
private private
def private_key_pkey def private_key_pkey
@private_key_pkey ||= OpenSSL::PKey::RSA.new context.private_key_pem @private_key_pkey ||=
OpenSSL::PKey::RSA.new context.public_key.private_key_pem
end end
def public_key_pkey def public_key_pkey

View file

@ -6,7 +6,7 @@ class DecryptRSAPrivateKey
before :set_cipher before :set_cipher
def call def call
context.private_key_pem_cleartext = [ context.public_key.private_key_pem = [
@cipher.update(context.public_key.private_key_pem_ciphertext), @cipher.update(context.public_key.private_key_pem_ciphertext),
@cipher.final, @cipher.final,
].join.freeze ].join.freeze

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true # frozen_string_literal: true
class RSAPublicKey < ApplicationRecord class RSAPublicKey < ApplicationRecord
attr_accessor :private_key_pem_secret attr_accessor :private_key_pem, :private_key_pem_secret
############### ###############
# Validations # # Validations #

View file

@ -23,10 +23,6 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
expect { subject }.to change(X509Certificate, :count).by(1) expect { subject }.to change(X509Certificate, :count).by(1)
end end
specify do
expect(subject.private_key_pem).to be_instance_of String
end
specify do specify do
expect(subject.public_key).to be_instance_of RSAPublicKey expect(subject.public_key).to be_instance_of RSAPublicKey
end end
@ -36,7 +32,7 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do
end end
specify do specify do
expect(subject.private_key_pem).not_to be_blank expect(subject.public_key.private_key_pem).not_to be_blank
end end
specify do specify do

View file

@ -9,20 +9,12 @@ RSpec.describe CreateRSAKeys do
expect { subject }.to change(RSAPublicKey, :count).by(1) expect { subject }.to change(RSAPublicKey, :count).by(1)
end end
specify do
expect(subject.private_key_pem).to be_instance_of String
end
specify do specify do
expect(subject.public_key).to be_instance_of RSAPublicKey expect(subject.public_key).to be_instance_of RSAPublicKey
end end
specify do specify do
expect(subject.private_key_pem).to be_frozen expect(subject.public_key.private_key_pem).not_to be_blank
end
specify do
expect(subject.private_key_pem).not_to be_blank
end end
specify do specify do
@ -30,7 +22,9 @@ RSpec.describe CreateRSAKeys do
end end
specify do specify do
expect { OpenSSL::PKey::RSA.new subject.private_key_pem }.not_to raise_error expect do
OpenSSL::PKey::RSA.new subject.public_key.private_key_pem
end.not_to raise_error
end end
specify do specify do
@ -39,8 +33,9 @@ RSpec.describe CreateRSAKeys do
end end
specify do specify do
expect(subject.public_key.public_key_pem).to \ expect(subject.public_key.public_key_pem).to eq(
eq OpenSSL::PKey::RSA.new(subject.private_key_pem).public_key.to_pem OpenSSL::PKey::RSA.new(subject.public_key.private_key_pem).public_key.to_pem,
)
end end
specify do specify do
@ -62,6 +57,6 @@ RSpec.describe CreateRSAKeys do
cipher.final, cipher.final,
].join.freeze ].join.freeze
expect(cleartext).to eq subject.private_key_pem expect(cleartext).to eq subject.public_key.private_key_pem
end end
end end

View file

@ -5,14 +5,12 @@ require 'rails_helper'
RSpec.describe CreateX509CertificateRequest do RSpec.describe CreateX509CertificateRequest do
subject do subject do
described_class.call( described_class.call(
private_key_pem: private_key_pem,
public_key: public_key, public_key: public_key,
distinguished_name: distinguished_name, distinguished_name: distinguished_name,
) )
end end
let(:rsa_keys) { CreateRSAKeys.call } let(:rsa_keys) { CreateRSAKeys.call }
let(:private_key_pem) { rsa_keys.private_key_pem }
let(:public_key) { rsa_keys.public_key } let(:public_key) { rsa_keys.public_key }
let(:distinguished_name) { "CN=#{Faker::Internet.domain_name}" } let(:distinguished_name) { "CN=#{Faker::Internet.domain_name}" }