Class: ShadowsocksRuby::Cipher::OpenSSL
- Inherits:
-
Object
- Object
- ShadowsocksRuby::Cipher::OpenSSL
- Defined in:
- lib/shadowsocks_ruby/cipher/openssl.rb
Overview
Encapsulate RubyGems version of OpenSSL, the RubyGems version OpenSSL can be installed with specific OpenSSL version you choose.
Note1: To work with a specific version of OpenSSL library other than the version comes with ruby, you may need to specify the path where OpenSSL is installed:
gem install openssl -- --with-openssl-dir=/opt/openssl
# See https://github.com/ruby/openssl for more detail.
Note2: Use this command to get a full list of cipher methods supported on your system.
ruby -e "require 'openssl'; puts OpenSSL::Cipher.ciphers"
Normally you should use #build to get an instance of this class.
Instance Attribute Summary collapse
-
#key ⇒ String
readonly
Return the key, which length is decided by the cipher method.
Instance Method Summary collapse
-
#decrypt(message, iv) ⇒ String
Decrypt message by provided IV.
-
#encrypt(message, iv) ⇒ String
Encrypt message by provided IV.
-
#initialize(method, password) ⇒ OpenSSL
constructor
A new instance of OpenSSL.
-
#iv_len ⇒ Integer
Get the cipher object’s IV length.
-
#random_iv ⇒ String
Generate a random IV for the cipher method.
Constructor Details
#initialize(method, password) ⇒ OpenSSL
Returns a new instance of OpenSSL.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 29 def initialize method, password @cipher_encrypt = ::OpenSSL::Cipher.new(method).encrypt @cipher_decrypt = ::OpenSSL::Cipher.new(method).decrypt key_len = @cipher_encrypt.key_len iv_len = @cipher_encrypt.iv_len @key = Cipher.bytes_to_key(password, key_len, iv_len) @cipher_encrypt.key = @key @cipher_decrypt.key = @key @encrypt_iv = nil @decrypt_iv = nil end |
Instance Attribute Details
#key ⇒ String (readonly)
Return the key, which length is decided by the cipher method.
25 26 27 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 25 def key @key end |
Instance Method Details
#decrypt(message, iv) ⇒ String
Decrypt message by provided IV
63 64 65 66 67 68 69 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 63 def decrypt(, iv) if @decrypt_iv != iv @decrypt_iv = iv @cipher_decrypt.iv = iv end @cipher_decrypt.update() << @cipher_decrypt.final end |
#encrypt(message, iv) ⇒ String
Encrypt message by provided IV
51 52 53 54 55 56 57 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 51 def encrypt(, iv) if @encrypt_iv != iv @encrypt_iv = iv @cipher_encrypt.iv = iv end @cipher_encrypt.update() << @cipher_encrypt.final end |
#iv_len ⇒ Integer
Get the cipher object’s IV length
73 74 75 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 73 def iv_len @cipher_encrypt.iv_len end |
#random_iv ⇒ String
Generate a random IV for the cipher method
43 44 45 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 43 def random_iv @encrypt_iv = @cipher_encrypt.random_iv end |