Module: EncryptoSigno

Defined in:
lib/encrypto_signo.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/encrypto_signo.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/encrypto_signo.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

.generate_keypair(size = 2048) ⇒ Object

Generate a keypair



37
38
39
# File 'lib/encrypto_signo.rb', line 37

def generate_keypair(size = 2048)
  OpenSSL::PKey::RSA.new(size)
end

.sign(key, string) ⇒ Object

Return a signature for the string



27
28
29
# File 'lib/encrypto_signo.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/encrypto_signo.rb', line 32

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