1
0
Fork 0

Add model EcurveKey

This commit is contained in:
Alex Kotov 2019-09-14 04:20:47 +05:00
parent 9250076a3a
commit 85830aa66a
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
6 changed files with 66 additions and 1 deletions

11
app/models/ecurve_key.rb Normal file
View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
class EcurveKey < AsymmetricKey
###############
# Validations #
###############
validates :curve, inclusion: { in: %w[prime256v1 secp384r1] }
validates :bits, absence: true
end

View file

@ -6,4 +6,6 @@ class RSAKey < AsymmetricKey
###############
validates :bits, inclusion: { in: [2048, 4096] }
validates :curve, absence: true
end

27
factories/ecurve_keys.rb Normal file
View file

@ -0,0 +1,27 @@
# frozen_string_literal: true
FactoryBot.define do
factory :ecurve_key do
association :account, factory: :usual_account
public_key_pem do
point = OpenSSL::PKey::EC.generate(curve).public_key
pkey = OpenSSL::PKey::EC.new point.group
pkey.public_key = point
pkey.to_pem
end
public_key_der do
point = OpenSSL::PKey::EC.generate(curve).public_key
pkey = OpenSSL::PKey::EC.new point.group
pkey.public_key = point
pkey.to_der
end
has_password { [false, true].sample }
sha1 { Digest::SHA1.hexdigest SecureRandom.hex }
sha256 { Digest::SHA256.hexdigest SecureRandom.hex }
curve { %w[prime256v1 secp384r1].sample }
end
end

View file

@ -8,8 +8,9 @@ FactoryBot.define do
public_key_der { OpenSSL::PKey::RSA.new(bits).public_key.to_der }
has_password { [false, true].sample }
bits { [2048, 4096].sample }
sha1 { Digest::SHA1.hexdigest SecureRandom.hex }
sha256 { Digest::SHA256.hexdigest SecureRandom.hex }
bits { [2048, 4096].sample }
end
end

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe EcurveKey do
subject { create :ecurve_key }
it_behaves_like 'asymmetric_key'
describe '#curve' do
it do
is_expected.to \
validate_inclusion_of(:curve).in_array(%w[prime256v1 secp384r1])
end
end
describe '#bits' do
it { is_expected.to validate_absence_of :bits }
end
end

View file

@ -11,6 +11,10 @@ RSpec.describe RSAKey do
it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) }
end
describe '#curve' do
it { is_expected.to validate_absence_of :curve }
end
describe '#encrypt_private_key_pem' do
subject { create :rsa_key, private_key_pem: cleartext }