您是否有任何本机功能(不安装其他gem,或者不从shell调用openssl)来压缩字符串或加密字符串?
有点像mysql compress。
"a very long and loose string".compress
output = "8d20\1l\201"
"8d20\1l\201".decompress
output = "a very long and loose string"?
并同样加密一些字符串?
答案 0 :(得分:14)
来自http://ruby-doc.org/stdlib/libdoc/zlib/rdoc/classes/Zlib.html
# aka compress
def deflate(string, level)
z = Zlib::Deflate.new(level)
dst = z.deflate(string, Zlib::FINISH)
z.close
dst
end
# aka decompress
def inflate(string)
zstream = Zlib::Inflate.new
buf = zstream.inflate(string)
zstream.finish
zstream.close
buf
end
http://snippets.dzone.com/posts/show/991加密
require 'openssl'
require 'digest/sha1'
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.encrypt
# your pass is what is used to encrypt/decrypt
c.key = key = Digest::SHA1.hexdigest("yourpass")
c.iv = iv = c.random_iv
e = c.update("crypt this")
e << c.final
puts "encrypted: #{e}\n"
c = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
c.decrypt
c.key = key
c.iv = iv
d = c.update(e)
d << c.final
puts "decrypted: #{d}\n"
答案 1 :(得分:5)
OpenSSL和Zlib。在this question中有一个OpenSSL使用示例。