require 'openssl' def encrypt(aaa, solt = 'solt') enc = OpenSSL::Cipher::Cipher.new('aes256') enc.encrypt enc.pkcs5_keyivgen(solt) ((enc.update(aaa) + enc.final).unpack("H*")).to_s rescue false end def decrypt(bbb, solt = 'solt') dec = OpenSSL::Cipher::Cipher.new('aes256') dec.decrypt dec.pkcs5_keyivgen(solt) (dec.update(Array.new([bbb]).pack("H*")) + dec.final) rescue false end def hash(ccc) OpenSSL::Digest::SHA1.new(ccc) end secret = "himitsu" solt = "unoh" enc = encrypt(secret, solt) p enc dec = decrypt(enc, solt) p dec p hash(secret) #-- #"2906e1eca22b8b74ab2a14cbf185185c" #"himitsu" #c44ade485240212403b41eba503fcb222d190cd0
便利ですね。
http://www.ruby-lang.org/ja/man/?cmd=view;name=openssl
http://www.ruby-lang.org/ja/man/?cmd=view;name=OpenSSL%3A%3ACipher%3A%3ACipher