class Encryption
Attributes
Public Instance Methods
decrypt @param [String] cipher - Base64 URL encoded cipher text @return [String] Plain text
# File lib/credify/encryption.rb, line 84 def decrypt(cipher) if @private_key.nil? raise Exception.new 'Please pass private key' end label = '' md = OpenSSL::Digest::SHA256 raw_cipher = Credify::Helpers.short_urlsafe_decode64(cipher) raw_text = @private_key.private_decrypt_oaep(raw_cipher, label, md) raw_text end
encrypt @param [String] message @return [String] Base64 URL encoded string after encryption
# File lib/credify/encryption.rb, line 70 def encrypt(message) if @public_key.nil? raise Exception.new 'Please pass public key' end label = '' md = OpenSSL::Digest::SHA256 cipher_text = @public_key.public_encrypt_oaep(message, label, md) Credify::Helpers.short_urlsafe_encode64(cipher_text) end
export_private_key
@param [Boolean] in_base64_url @return [Signing | String] - PCKS8 PEM or Base64 URL encoded string
# File lib/credify/encryption.rb, line 99 def export_private_key(in_base64_url = false) if @private_key.nil? raise Exception.new 'Please pass private key' end pem = @private_key.to_pem_pkcs8.gsub(/#{$/}$/, "") if in_base64_url formatted = remove_box('PRIVATE KEY', pem) Credify::Helpers.short_urlsafe_encode64(Base64.decode64(formatted)) else pem end end
export_public_key
@param [Boolean] in_base64_url @return [Signing | String] - PCKS8 PEM or Base64 URL encoded string
# File lib/credify/encryption.rb, line 117 def export_public_key(in_base64_url = false) if @public_key.nil? raise Exception.new 'Please pass public key' end pem = @public_key.to_pem_pkcs8.gsub(/#{$/}$/, "") if in_base64_url formatted = remove_box('PUBLIC KEY', pem) Credify::Helpers.short_urlsafe_encode64(Base64.decode64(formatted)) else pem end end
generate_key_pair
@return [Boolean]
# File lib/credify/encryption.rb, line 13 def generate_key_pair key = OpenSSL::PKey::RSA.generate(4096, 17) @private_key = key @public_key = key.public_key @private_key.nil? end
import_private_key
@param [String] pem @return [Boolean]
# File lib/credify/encryption.rb, line 24 def import_private_key(pem) key = OpenSSL::PKey::RSA.new pem @private_key = key @public_key = key.public_key @private_key.nil? end
import_private_key_base64_url
@param [String] payload - Base64 URL encoded string @return [Boolean]
# File lib/credify/encryption.rb, line 46 def import_private_key_base64_url(payload) bytes = Credify::Helpers.short_urlsafe_decode64(payload) base64 = Base64.encode64(bytes) formatted = base64.scan(/.{1,64}/).join("\n") pem = add_box('PRIVATE KEY', formatted) import_private_key(pem) end
import_public_key
@param [String] pem @return [Boolean]
# File lib/credify/encryption.rb, line 35 def import_public_key(pem) key = OpenSSL::PKey::RSA.new pem # @private_key = key @public_key = key.public_key @public_key.nil? end
import_public_key_base64_url
@param [String] payload - Base64 URL encoded string @return [Boolean]
# File lib/credify/encryption.rb, line 58 def import_public_key_base64_url(payload) bytes = Credify::Helpers.short_urlsafe_decode64(payload) base64 = Base64.encode64(bytes) formatted = base64.scan(/.{1,64}/).join("\n") pem = add_box('PUBLIC KEY', formatted) import_public_key(pem) end
Protected Instance Methods
add_box
@param [String] tag - Either 'PUBLIC KEY' or 'PRIVATE KEY' @param [String] base64 - Base64 encoded string @return [String] - PEM
# File lib/credify/encryption.rb, line 151 def add_box(tag, base64) payload = base64.scan(/.{1,64}/).join("\n") "-----BEGIN #{tag}-----\n" << payload << "\n-----END #{tag}-----" end
remove_box
@param [String] tag - Either 'PUBLIC KEY' or 'PRIVATE KEY' @param [String] pem - String value loaded from a PEM file @return [String] - Base64 encoded string in PEM file
# File lib/credify/encryption.rb, line 140 def remove_box(tag, pem) tmp = pem.gsub("-----BEGIN #{tag}-----", '') tmp = tmp.gsub("-----END #{tag}-----", '') tmp.gsub(/\n/, '') end