class ShadowsocksRuby::Cipher::RbNaCl

Encapsulate RbNaCl ruby library, cipher methods provided by this Class are:

Normally you should use {ShadowsocksRuby::Cipher#build} to get an instance of this class.

Attributes

key[R]

Public Class Methods

new(method, password) click to toggle source

(see OpenSSL#initialize)

# File lib/shadowsocks_ruby/cipher/rbnacl.rb, line 18
def initialize method, password
  klass = case method
    when 'chacha20'
      ::RbNaCl::AEAD::ChaCha20Poly1305Legacy
    when 'chacha20-ietf'
      ::RbNaCl::AEAD::ChaCha20Poly1305IETF
    when 'salsa20'
      ::RbNaCl::SecretBoxes::XSalsa20Poly1305
    else
      raise CipherError, "unsupported method: " + method
    end
  key_len = klass.key_bytes
  iv_len = klass.nonce_bytes
  @key = ShadowsocksRuby::Cipher.bytes_to_key(password, key_len, iv_len)
  @cipher = klass.new(@key)
end

Public Instance Methods

decrypt(message, iv) click to toggle source

(see OpenSSL#decrypt)

# File lib/shadowsocks_ruby/cipher/rbnacl.rb, line 50
def decrypt(message, iv)
  if @cipher.class == ::RbNaCl::SecretBoxes::XSalsa20Poly1305
    @cipher.decrypt(iv, message)
  else
    @cipher.decrypt(iv, message, nil)
  end
end
encrypt(message, iv) click to toggle source

(see OpenSSL#encrypt)

# File lib/shadowsocks_ruby/cipher/rbnacl.rb, line 41
def encrypt(message, iv)
  if @cipher.class == ::RbNaCl::SecretBoxes::XSalsa20Poly1305
    @cipher.encrypt(iv, message)
  else
    @cipher.encrypt(iv, message, nil)
  end
end
iv_len() click to toggle source

(see OpenSSL#iv_len)

# File lib/shadowsocks_ruby/cipher/rbnacl.rb, line 59
def iv_len
  @cipher.iv_bytes
end
random_iv() click to toggle source

(see OpenSSL#random_iv)

# File lib/shadowsocks_ruby/cipher/rbnacl.rb, line 36
def random_iv
  ::RbNaCl::Random.random_bytes(@cipher.nonce_bytes)
end