class ExoBasic::RSAKeys

Constants

BITS

Public Class Methods

from_pem(key_pem) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 48
def self.from_pem(key_pem)
  OpenSSL::PKey::RSA.new(key_pem)
end
gen_key(bits=nil) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 68
def self.gen_key(bits=nil)
  b = bits.nil? ? RSAKeys::BITS : bits

  RSAKeys.get_key_details(OpenSSL::PKey::RSA.generate(b))
end
get_key_details(private_key) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 28
def self.get_key_details(private_key)
  private_key_pem = private_key.to_pem
  private_key_hex = private_key.to_der.unpack('H*').first

  public_key      = private_key.public_key
  public_key_pem  = public_key.to_pem
  public_key_hex  = public_key.to_der.unpack('H*').first

  {
    :private_pem => private_key_pem,
    :private_hex => private_key_hex,
    :public_pem => public_key_pem,
    :public_hex => public_key_hex
  }
end
private_decrypt(private_key_pem, encrypted_base64) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 108
def self.private_decrypt(private_key_pem, encrypted_base64)
  pkey = RSAKeys.from_pem(private_key_pem)

  encrypted = Base64.decode64(encrypted_base64)
  pkey.private_decrypt(encrypted)
end
private_encrypt(private_key_pem, data) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 90
def self.private_encrypt(private_key_pem, data)
  pkey = RSAKeys.from_pem(private_key_pem)

  encrypted        = pkey.private_encrypt(data)
  encrypted_base64 = Base64.encode64(encrypted).gsub("\n", '')

  encrypted_base64
end
private_from_hex(private_hex) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 52
def self.private_from_hex(private_hex)
  der = [private_hex].pack('H*')
  b64 = Base64.encode64(der)
  pem = RSAKeys.s_to_private_pem(b64)

  RSAKeys.from_pem(pem)
end
private_pem_to_s(pem) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 8
def self.private_pem_to_s(pem)
  pem.gsub("-----BEGIN RSA PRIVATE KEY-----\n", '')
     .gsub("\n-----END RSA PRIVATE KEY-----\n", '')
     .gsub("\n", '')
end
public_decrypt(public_key_pem, encrypted_base64) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 115
def self.public_decrypt(public_key_pem, encrypted_base64)
  pub_key = RSAKeys.from_pem(public_key_pem)

  encrypted = Base64.decode64(encrypted_base64)
  pub_key.public_decrypt(encrypted)
end
public_encrypt(public_key_pem, data) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 99
def self.public_encrypt(public_key_pem, data)
  pub_key = RSAKeys.from_pem(public_key_pem)

  encrypted        = pub_key.public_encrypt(data)
  encrypted_base64 = Base64.encode64(encrypted).gsub("\n", '')

  encrypted_base64
end
public_from_hex(public_hex) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 60
def self.public_from_hex(public_hex)
  der = [public_hex].pack('H*')
  b64 = Base64.encode64(der)
  pem = RSAKeys.s_to_public_pem(b64)

  RSAKeys.from_pem(pem)
end
public_pem_to_s(pem) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 18
def self.public_pem_to_s(pem)
  pem.gsub("-----BEGIN PUBLIC KEY-----\n", '')
     .gsub("\n-----END PUBLIC KEY-----\n", '')
     .gsub("\n", '')
end
s_to_private_pem(b64) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 14
def self.s_to_private_pem(b64)
  "-----BEGIN RSA PRIVATE KEY-----\n#{b64}-----END RSA PRIVATE KEY-----\n"
end
s_to_public_pem(b64) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 24
def self.s_to_public_pem(b64)
  "-----BEGIN PUBLIC KEY-----\n#{b64}-----END PUBLIC KEY-----\n"
end
sign_message(private_key_pem, data) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 74
def self.sign_message(private_key_pem, data)
  pkey = RSAKeys.from_pem(private_key_pem)

  signature        = pkey.sign_pss('SHA256', data, salt_length: :max, mgf1_hash: 'SHA256')
  signature_base64 = Base64.encode64(signature).gsub("\n", '')

  signature_base64
end
to_pem(key) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 44
def self.to_pem(key)
  key.to_pem
end
verify_message(public_key_pem, signature_base64, data) click to toggle source
# File lib/exobasic/encrypt/rsa_keys.rb, line 83
def self.verify_message(public_key_pem, signature_base64, data)
  pub_key = RSAKeys.from_pem(public_key_pem)

  signature = Base64.decode64(signature_base64)
  pub_key.verify_pss('SHA256', signature, data, salt_length: :auto, mgf1_hash: 'SHA256')
end