Class: ShadowsocksRuby::Cipher::OpenSSL

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(method, password) ⇒ OpenSSL

Returns a new instance of OpenSSL.

Parameters:

  • method (String)

    Cipher methods

  • password (String)

    Password


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

#keyString (readonly)

Return the key, which length is decided by the cipher method.

Returns:

  • (String)

    key


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

Parameters:

  • message (String)
  • iv (String)

Returns:

  • (String)

    Decrypted Message


63
64
65
66
67
68
69
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 63

def decrypt(message, iv)
  if @decrypt_iv != iv
    @decrypt_iv = iv
    @cipher_decrypt.iv = iv
  end
  @cipher_decrypt.update(message) << @cipher_decrypt.final
end

#encrypt(message, iv) ⇒ String

Encrypt message by provided IV

Parameters:

  • message (String)
  • iv (String)

Returns:

  • (String)

    Encrypted Message


51
52
53
54
55
56
57
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 51

def encrypt(message, iv)
  if @encrypt_iv != iv
    @encrypt_iv = iv
    @cipher_encrypt.iv = iv
  end
  @cipher_encrypt.update(message) << @cipher_encrypt.final
end

#iv_lenInteger

Get the cipher object's IV length

Returns:

  • (Integer)

73
74
75
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 73

def iv_len
  @cipher_encrypt.iv_len
end

#random_ivString

Generate a random IV for the cipher method

Returns:

  • (String)

    random IV of the length of 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