Class: Themis::Smessage

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

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

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(private_key, peer_public_key) ⇒ Smessage

Returns a new instance of Smessage.



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/rbthemis.rb', line 404

def initialize(private_key, peer_public_key)
  if not Themis.valid_key(private_key)
    raise ThemisError, "Secure Message: invalid private key"
  end
  if not Themis.valid_key(peer_public_key)
    raise ThemisError, "Secure Message: invalid public key"
  end
  if not Themis.private_key(private_key)
    raise ThemisError, "Secure Message: public key used instead of private"
  end
  if not Themis.public_key(peer_public_key)
    raise ThemisError, "Secure Message: private key used instead of public"
  end

  @private_key, @private_key_length = string_to_pointer_size(private_key)
  @peer_public_key, @peer_public_key_length =
    string_to_pointer_size(peer_public_key)
end

Instance Method Details

#unwrap(message) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/rbthemis.rb', line 448

def unwrap(message)
  message_, message_length_ = string_to_pointer_size(message)
  unwrapped_message_length = FFI::MemoryPointer.new(:uint)
  res = themis_secure_message_decrypt(
    @private_key, @private_key_length, @peer_public_key,
    @peer_public_key_length, message_, message_length_,
    nil, unwrapped_message_length)
  if res != BUFFER_TOO_SMALL
    raise ThemisError, "Secure Message failed to decrypt: #{res}"
  end

  unwrapped_message = FFI::MemoryPointer.new(
    :char, unwrapped_message_length.read_uint)
  res = themis_secure_message_decrypt(
    @private_key, @private_key_length, @peer_public_key,
    @peer_public_key_length, message_, message_length_,
    unwrapped_message, unwrapped_message_length)
  if res != SUCCESS
    raise ThemisError, "Secure Message failed to decrypt: #{res}"
  end

  unwrapped_message.get_bytes(0, unwrapped_message_length.read_uint)
end

#wrap(message) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/rbthemis.rb', line 423

def wrap(message)
    message_, message_length_ = string_to_pointer_size(message)

    wrapped_message_length = FFI::MemoryPointer.new(:uint)
    res = themis_secure_message_encrypt(
      @private_key, @private_key_length, @peer_public_key,
      @peer_public_key_length, message_, message_length_,
      nil, wrapped_message_length)
    if res != BUFFER_TOO_SMALL
      raise ThemisError, "Secure Message failed to encrypt: #{res}"
    end

    wrapped_message = FFI::MemoryPointer.new(
      :char, wrapped_message_length.read_uint)
    res = themis_secure_message_encrypt(
      @private_key, @private_key_length, @peer_public_key,
      @peer_public_key_length, message_, message_length_,
      wrapped_message, wrapped_message_length)
    if res != SUCCESS
      raise ThemisError, "Secure Message failed to encrypt: #{res}"
    end

    wrapped_message.get_bytes(0, wrapped_message_length.read_uint)
end