Class: Themis::ScellSealPassphrase

Inherits:
ScellSeal show all
Includes:
ThemisCommon, ThemisImport
Defined in:
lib/rbthemis.rb

Overview

Secure Cell in Seal mode.

Constant Summary

Constants included from ThemisImport

ThemisImport::THEMIS_KEY_EC_PRIVATE, ThemisImport::THEMIS_KEY_EC_PUBLIC, ThemisImport::THEMIS_KEY_INVALID, ThemisImport::THEMIS_KEY_RSA_PRIVATE, ThemisImport::THEMIS_KEY_RSA_PUBLIC

Constants inherited from Scell

Themis::Scell::CONTEXT_IMPRINT_MODE, Themis::Scell::SEAL_MODE, Themis::Scell::TOKEN_PROTECT_MODE

Instance Method Summary collapse

Methods included from ThemisImport

canonical_themis_paths, load_themis

Methods included from ThemisCommon

empty?, string_to_pointer_size

Constructor Details

#initialize(passphrase, encoding: Encoding::UTF_8) ⇒ ScellSealPassphrase

Make a new Secure Cell with given passphrase. The passphrase must not be empty. If the passphrase is not binary it will be encoded in UTF-8 by default, you can use optional “encoding:” argument to use a different encoding.



691
692
693
694
695
696
697
698
699
# File 'lib/rbthemis.rb', line 691

def initialize(passphrase, encoding: Encoding::UTF_8)
  if empty? passphrase
    raise ThemisError, "passphrase cannot be empty"
  end
  if passphrase.encoding != Encoding::BINARY
    passphrase = passphrase.encode(encoding)
  end
  @passphrase, @passphrase_length = string_to_pointer_size(passphrase)
end

Instance Method Details

#decrypt(message, context = nil) ⇒ Object

Decrypts message with given context. The context must be the same as the one used during encryption, or be omitted or set to nil if no context were used. Decrypted message is returned as binary data.



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
# File 'lib/rbthemis.rb', line 739

def decrypt(message, context = nil)
  if empty? message
    raise ThemisError, "message cannot be empty"
  end

  message_, message_length_ = string_to_pointer_size(message)
  context_, context_length_ =
    context.nil? ? [nil, 0] : string_to_pointer_size(context)

  decrypted_length = FFI::MemoryPointer.new(:uint)
  res = themis_secure_cell_decrypt_seal_with_passphrase(
    @passphrase, @passphrase_length, context_, context_length_,
    message_, message_length_, nil, decrypted_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError.new(res), "decrypt failed"
  end

  decrypted_message = FFI::MemoryPointer.new(:char, decrypted_length.read_uint)
  res = themis_secure_cell_decrypt_seal_with_passphrase(
    @passphrase, @passphrase_length, context_, context_length_,
    message_, message_length_, decrypted_message, decrypted_length)
  if res != SUCCESS
    raise ThemisError.new(res), "decrypt failed"
  end

  decrypted_message.get_bytes(0, decrypted_length.read_uint)
end

#encrypt(message, context = nil) ⇒ Object

Encrypts message with given optional context. The context is cryptographically combined with message but is not included into encrypted data, you will need to provide the same context for decryption. Resulting encrypted message includes authentication token. Message must not be empty, but context may be omitted. Both message and context are treated as binary data.



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/rbthemis.rb', line 707

def encrypt(message, context = nil)
  if empty? message
    raise ThemisError, "message cannot be empty"
  end

  message_, message_length_ = string_to_pointer_size(message)
  context_, context_length_ =
    context.nil? ? [nil, 0] : string_to_pointer_size(context)

  encrypted_length = FFI::MemoryPointer.new(:uint)
  res = themis_secure_cell_encrypt_seal_with_passphrase(
    @passphrase, @passphrase_length, context_, context_length_,
    message_, message_length_, nil, encrypted_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError.new(res), "encrypt failed"
  end

  encrypted_message = FFI::MemoryPointer.new(:char, encrypted_length.read_uint)
  res = themis_secure_cell_encrypt_seal_with_passphrase(
    @passphrase, @passphrase_length, context_, context_length_,
    message_, message_length_, encrypted_message, encrypted_length)
  if res != SUCCESS
    raise ThemisError.new(res), "encrypt failed"
  end

  encrypted_message.get_bytes(0, encrypted_length.read_uint)
end