class Occson::Encrypter

Encrypts the given content for transmission. Uses AES-256 in CBC mode internally, with salting.

Public Class Methods

new(passphrase, content, salt) click to toggle source

Constructs an Encrypter instance with given passphrase, content and salt. Salt must be exactly 8 characters long.

@example

passphrase = 'my long document passphrase'
content = 'very secret content'
salt = '12345678'

Occson::Encrypter.new(passphrase, content, salt)

@param passphrase [String] Document passphrase. @param content [String] Plaintext content to be encrypted. @param salt [String] Salt to reinforce the encryption, included in

plaintext in the encrypted document.
# File lib/occson/encrypter.rb, line 21
def initialize(passphrase, content, salt)
  @passphrase = passphrase
  @content = content
  @salt = salt
end

Public Instance Methods

call() click to toggle source

Performs the actual encryption, returning base64-encoded ciphertext.

@return [String] base64-encoded ciphertext

# File lib/occson/encrypter.rb, line 30
def call
  encryptor.pkcs5_keyivgen(@passphrase, @salt, 1)
  encrypted = encryptor.update(@content)
  encrypted << encryptor.final

  openssl_salted_ciphertext = 'Salted__' + @salt + encrypted
  Base64.strict_encode64(openssl_salted_ciphertext)
end

Private Instance Methods

encryptor() click to toggle source
# File lib/occson/encrypter.rb, line 41
def encryptor
  @encryptor ||= OpenSSL::Cipher::AES.new(256, :CBC).encrypt
end