class Crypto::Commons
Constants
- SALT_SPLITTER
Public Class Methods
checksum(text)
click to toggle source
# File lib/crypto/commons.rb, line 44 def self.checksum(text) text = text.to_s unless text.is_a? String Digest::SHA2.new(256).hexdigest(text) end
checksum512(text)
click to toggle source
# File lib/crypto/commons.rb, line 49 def self.checksum512(text) text = text.to_s unless text.is_a? String Digest::SHA2.new(512).hexdigest(text) end
decrypt(text, secret = Crypto.secret_key_base.to_s, cipher: 'aes-256-gcm')
click to toggle source
# File lib/crypto/commons.rb, line 17 def self.decrypt(text, secret = Crypto.secret_key_base.to_s, cipher: 'aes-256-gcm') salt, data = text.split SALT_SPLITTER len = ActiveSupport::MessageEncryptor.key_len(cipher) key = ActiveSupport::KeyGenerator.new(secret) .generate_key salt, len crypt = ActiveSupport::MessageEncryptor.new(key, cipher: cipher) crypt.decrypt_and_verify data end
encrypt(text, secret = Crypto.secret_key_base.to_s, cipher: 'aes-256-gcm')
click to toggle source
# File lib/crypto/commons.rb, line 5 def self.encrypt(text, secret = Crypto.secret_key_base.to_s, cipher: 'aes-256-gcm') text = text.to_s unless text.is_a? String len = ActiveSupport::MessageEncryptor.key_len(cipher) salt = SecureRandom.hex len key = ActiveSupport::KeyGenerator.new(secret) .generate_key salt, len crypt = ActiveSupport::MessageEncryptor.new(key, cipher: cipher) encrypted_data = crypt.encrypt_and_sign text "#{salt}#{SALT_SPLITTER}#{encrypted_data}" end
hash(text)
click to toggle source
# File lib/crypto/commons.rb, line 27 def self.hash(text) text = text.to_s unless text.is_a? String cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(text, cost: cost) end
hash_compare(hash, text)
click to toggle source
# File lib/crypto/commons.rb, line 34 def self.hash_compare(hash, text) password = BCrypt::Password.new(hash) password == text end
md5_digest(text)
click to toggle source
# File lib/crypto/commons.rb, line 39 def self.md5_digest(text) text = text.to_s unless text.is_a? String Digest::MD5.hexdigest(text) end
rsa_seal(private_key, passphrase, text)
click to toggle source
# File lib/crypto/commons.rb, line 58 def self.rsa_seal(private_key, passphrase, text) text = text.to_s unless text.is_a? String key = Crypto::RSAKey.new private_key, passphrase key.seal(text) end
sha256(secret_key, text)
click to toggle source
# File lib/crypto/commons.rb, line 54 def self.sha256(secret_key, text) OpenSSL::HMAC.hexdigest('sha256', secret_key, text) end