module Cryptology
Constants
- VERSION
Public Class Methods
decrypt(data:, key:, salt:, iter: 10_000, cipher: 'AES-256-CBC', iv:)
click to toggle source
# File lib/cryptology.rb, line 17 def self.decrypt(data:, key:, salt:, iter: 10_000, cipher: 'AES-256-CBC', iv:) base64_decoded = ::Base64.decode64(data.to_s) decrypt_data(base64_decoded, digest_key(key, salt, iter), cipher, iv) .force_encoding('UTF-8').encode end
decryptable?(data:, key:, salt:, iter: 10_000, cipher: 'AES-256-CBC', iv:)
click to toggle source
# File lib/cryptology.rb, line 23 def self.decryptable?(data:, key:, salt:, iter: 10_000, cipher: 'AES-256-CBC', iv:) return true if decrypt(data: data, key: key, salt: salt, iter: iter, cipher: cipher, iv: iv) rescue OpenSSL::Cipher::CipherError false end
encrypt(data:, key:, salt: nil, iter: 10_000, cipher: 'AES-256-CBC', iv: nil)
click to toggle source
# File lib/cryptology.rb, line 6 def self.encrypt(data:, key:, salt: nil, iter: 10_000, cipher: 'AES-256-CBC', iv: nil) salt ||= OpenSSL::Random.random_bytes(16) iv ||= OpenSSL::Cipher.new(cipher).random_iv encrypted = encrypt_data(data.to_s, digest_key(key, salt, iter), cipher, iv) { 'cipher' => cipher, 'salt' => salt, 'iter' => iter, 'iv' => iv, 'data' => ::Base64.encode64(encrypted) } end
Private Class Methods
decrypt_data(data, key, cipher, iv)
click to toggle source
# File lib/cryptology.rb, line 37 def self.decrypt_data(data, key, cipher, iv) decipher = OpenSSL::Cipher.new(cipher) decipher.decrypt decipher.key = key decipher.iv = iv unless iv.length != decipher.random_iv.length decipher.update(data) + decipher.final end
digest_key(key, salt, iter)
click to toggle source
# File lib/cryptology.rb, line 45 def self.digest_key(key, salt, iter) digest = OpenSSL::Digest::SHA256.new len = digest.digest_length OpenSSL::PKCS5.pbkdf2_hmac(key, salt, iter, len, digest) end
encrypt_data(data, key, cipher, iv)
click to toggle source
# File lib/cryptology.rb, line 29 def self.encrypt_data(data, key, cipher, iv) c = OpenSSL::Cipher.new(cipher) c.encrypt c.key = key c.iv = iv unless iv.length != c.random_iv.length c.update(data) + c.final end