是的,Ruby中有一些加密和解密的库可以使用。以下是一些常用的库:
openssl
gem来调用OpenSSL的功能。例如,你可以使用OpenSSL::Cipher
类来创建一个加密和解密的cipher对象。安装:gem install openssl
示例:
require 'openssl'
key = OpenSSL::PKey::RSA.new(2048)
cipher = OpenSSL::Cipher.new('AES-256-CBC')
cipher.encrypt
cipher.key = key.export
cipher.iv = cipher.random_iv
plaintext = "Hello, World!"
ciphertext = cipher.update(plaintext) + cipher.final
puts "Plaintext: #{plaintext}"
puts "Ciphertext: #{ciphertext}"
安装:gem install digest
示例:
require 'digest'
plaintext = "Hello, World!"
hash = Digest::SHA256.hexdigest(plaintext)
puts "Plaintext: #{plaintext}"
puts "Hash: #{hash}"
RbNaCl
和bcrypt
。安装:gem install rbNaCl
(对于NaCl库)和gem install bcrypt
(对于bcrypt库)
示例(RbNaCl):
require 'rbnacl'
key = RbNaCl::Random.random_bytes(32)
cipher = RbNaCl::Cipher.new(:aes256_cbc)
cipher.encrypt(key, "Hello, World!")
ciphertext = cipher.finalize
puts "Plaintext: Hello, World!"
puts "Ciphertext: #{ciphertext.unpack1('H*')}"
示例(bcrypt):
require 'bcrypt'
password = "Hello, World!"
salt = BCrypt::Engine.random_salt
hashed_password = BCrypt::Password.create(password, salt: salt)
puts "Plaintext: #{password}"
puts "Hashed Password: #{hashed_password}"
根据你的需求,可以选择合适的库进行加密和解密操作。