Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | encryptor gem で暗号化・復号化を行う

概要

encryptor gem で暗号化・復号化を行う

詳細

encryptor gem で暗号化・復号化を行います。

サンプル

※要 gem install encryptor

# encoding: utf-8
require "encryptor"
require "tbpgr_utils"

Encryptor.default_options.merge!(:key => 'encrypt_key')
encrypted = "hello".encrypt
decrypted = encrypted.decrypt
bulk_puts_eval binding, <<-EOS
encrypted
decrypted
EOS

__END__
下記はTbpgrUtils gemの機能

bulk_puts_eval

詳しくは下記参照
https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

補足

今回はString#encrypt, decryptを利用しているが
Encryptor.encrypt,decryptも利用可能。
また、iv,saltなども利用可能。
詳細はGitHub参照。

出力

encrypted # => "\x87{\x95/\xCC\x9F\xCB\xE7\xD2a4\xD7>\x8E\x15\xCA"
decrypted # => "hello"