Class: Encryption

Inherits:
Object
  • Object
show all
Defined in:
lib/credify/encryption.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



8
9
10
# File 'lib/credify/encryption.rb', line 8

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



8
9
10
# File 'lib/credify/encryption.rb', line 8

def public_key
  @public_key
end

Instance Method Details

#decrypt(cipher) ⇒ String

decrypt

Parameters:

  • cipher (String)
    • Base64 URL encoded cipher text

Returns:

  • (String)

    Plain text



84
85
86
87
88
89
90
91
92
93
# 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(message) ⇒ String

encrypt

Parameters:

  • message (String)

Returns:

  • (String)

    Base64 URL encoded string after encryption



70
71
72
73
74
75
76
77
78
# 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(in_base64_url = false) ⇒ Signing | String

export_private_key

Parameters:

  • in_base64_url (Boolean) (defaults to: false)

Returns:

  • (Signing | String)
    • PCKS8 PEM or Base64 URL encoded string



99
100
101
102
103
104
105
106
107
108
109
110
111
# 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(in_base64_url = false) ⇒ Signing | String

export_public_key

Parameters:

  • in_base64_url (Boolean) (defaults to: false)

Returns:

  • (Signing | String)
    • PCKS8 PEM or Base64 URL encoded string



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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_pairBoolean

generate_key_pair

Returns:

  • (Boolean)


13
14
15
16
17
18
# 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(pem) ⇒ Boolean

import_private_key

Parameters:

  • pem (String)

Returns:

  • (Boolean)


24
25
26
27
28
29
# 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(payload) ⇒ Boolean

import_private_key_base64_url

Parameters:

  • payload (String)
    • Base64 URL encoded string

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# 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(pem) ⇒ Boolean

import_public_key

Parameters:

  • pem (String)

Returns:

  • (Boolean)


35
36
37
38
39
40
# 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(payload) ⇒ Boolean

import_public_key_base64_url

Parameters:

  • payload (String)
    • Base64 URL encoded string

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# 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