Class: ShadowsocksRuby::Cipher::OpenSSL

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

Instance Method Summary collapse

Constructor Details

#initialize(method, password) ⇒ OpenSSL

Returns a new instance of OpenSSL.

Parameters:

  • method (String)

    Cipher methods

  • password (String)

    Password



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

#keyString (readonly)

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

Returns:

  • (String)

    key



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

Parameters:

  • message (String)
  • iv (String)

Returns:

  • (String)

    Decrypted Message



66
67
68
69
70
71
72
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 66

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



54
55
56
57
58
59
60
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 54

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)


76
77
78
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 76

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



46
47
48
# File 'lib/shadowsocks_ruby/cipher/openssl.rb', line 46

def random_iv
  @encrypt_iv = @cipher_encrypt.random_iv
end