Class: Ccs::Encrypter

Inherits:
Object
  • Object
show all
Defined in:
lib/ccs/encrypter.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(passphrase, content, salt) ⇒ Encrypter

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

Examples:

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

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

Parameters:

  • passphrase (String)

    Document passphrase.

  • content (String)

    Plaintext content to be encrypted.

  • salt (String)

    Salt to reinforce the encryption, included in plaintext in the encrypted document.



21
22
23
24
25
# File 'lib/ccs/encrypter.rb', line 21

def initialize(passphrase, content, salt)
  @passphrase = passphrase
  @content = content
  @salt = salt
end

Instance Method Details

#callString

Performs the actual encryption, returning base64-encoded ciphertext.

Returns:

  • (String)

    base64-encoded ciphertext



30
31
32
33
34
35
36
37
# File 'lib/ccs/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