1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
lpr-partynest/app/models/rsa_public_key.rb

46 lines
978 B
Ruby
Raw Normal View History

2019-09-10 08:08:23 -04:00
# frozen_string_literal: true
class RSAPublicKey < ApplicationRecord
attr_accessor :private_key_pem, :private_key_pem_secret
2019-09-10 08:08:23 -04:00
###############
# Validations #
###############
validates :public_key_pem, presence: true
2019-09-10 08:08:23 -04:00
validates :bits, inclusion: { in: [2048, 4096] }
###########
# Methods #
###########
def encrypt_private_key_pem
cipher = OpenSSL::Cipher::AES256.new
cipher.encrypt
self.private_key_pem_iv = cipher.random_iv.freeze
self.private_key_pem_secret = cipher.random_key.freeze
self.private_key_pem_ciphertext = [
cipher.update(private_key_pem),
cipher.final,
].join.freeze
private_key_pem_secret
end
def decrypt_private_key_pem
cipher = OpenSSL::Cipher::AES256.new
cipher.decrypt
cipher.iv = private_key_pem_iv
cipher.key = private_key_pem_secret
self.private_key_pem = [
cipher.update(private_key_pem_ciphertext),
cipher.final,
].join.freeze
end
2019-09-10 08:08:23 -04:00
end