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 gems version is newer than the version in Ruby Standand Library.
Cipher methods provided by Ruby OpenSSL library is dicided by the OpenSSL library comes with ruby on your system. To work with 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
Use this command to get a full list of cipher methods supported on your system.
ruby -e "require 'openssl'; puts OpenSSL::Cipher.ciphers"
See github.com/ruby/openssl for more detail.
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.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 32 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.
28 29 30 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 28 def key @key end |
Instance Method Details
#decrypt(message, iv) ⇒ String
Decrypt message by provided IV
66 67 68 69 70 71 72 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 66 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
54 55 56 57 58 59 60 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 54 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
76 77 78 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 76 def iv_len @cipher_encrypt.iv_len end |
#random_iv ⇒ String
Generate a random IV for the cipher method
46 47 48 |
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 46 def random_iv @encrypt_iv = @cipher_encrypt.random_iv end |