Module: BasicSSL

Defined in:
lib/basic_ssl.rb

Class Method Summary collapse

Class Method Details

.decrypt(key, string) ⇒ Object

 Return the raw string after decryption & decoding



18
19
20
21
22
23
24
# File 'lib/basic_ssl.rb', line 18

def decrypt(key, string)
  encrypted_key, crypt = string.split("|").map{|a| Base64.decode64(a) }
  aes_key = rsa_key(key).private_decrypt(encrypted_key)
  aes_decrypt = OpenSSL::Cipher::Cipher.new('AES-256-CBC').decrypt
  aes_decrypt.key = aes_key
  aes_decrypt.update(crypt) << aes_decrypt.final
end

.encrypt(key, string) ⇒ Object

Returns an Base64 encoded string with encryption



9
10
11
12
13
14
15
# File 'lib/basic_ssl.rb', line 9

def encrypt(key, string)
  aes_encrypt = OpenSSL::Cipher::Cipher.new('AES-256-CBC').encrypt
  aes_encrypt.key = aes_key = aes_encrypt.random_key
  crypt = aes_encrypt.update(string) << aes_encrypt.final
  encrypted_key = rsa_key(key).public_encrypt(aes_key)
  [Base64.encode64(encrypted_key), Base64.encode64(crypt)].join("|")
end

.sign(key, string) ⇒ Object

Return a signature for the string



27
28
29
# File 'lib/basic_ssl.rb', line 27

def sign(key, string)
  Base64.encode64(rsa_key(key).sign(OpenSSL::Digest::SHA1.new, string))
end

.verify(key, signature, string) ⇒ Object

Verify the string and signature



32
33
34
# File 'lib/basic_ssl.rb', line 32

def verify(key, signature, string)
  rsa_key(key).verify(OpenSSL::Digest::SHA1.new, Base64.decode64(signature), string)
end