Module: Origami::Encryption::EncryptedString

Includes:
EncryptedObject
Defined in:
lib/origami/encryption.rb

Overview

Module for encrypted String.

Instance Attribute Summary

Attributes included from EncryptedObject

#decrypted

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EncryptedObject

#post_build

Class Method Details

.extended(obj) ⇒ Object



457
458
459
# File 'lib/origami/encryption.rb', line 457

def self.extended(obj)
    obj.decrypted = false
end

Instance Method Details

#decrypt!Object

Raises:



485
486
487
488
489
490
491
492
493
494
495
496
497
# File 'lib/origami/encryption.rb', line 485

def decrypt!
    return self if @decrypted

    cipher = self.document.string_encryption_cipher
    raise EncryptionError, "Cannot find string encryption filter" if cipher.nil?

    key = compute_object_key(cipher)

    self.replace(cipher.decrypt(key, self.to_str))
    @decrypted = true

    self
end

#encrypt!Object

Raises:



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# File 'lib/origami/encryption.rb', line 461

def encrypt!
    return self unless @decrypted

    cipher = self.document.string_encryption_cipher
    raise EncryptionError, "Cannot find string encryption filter" if cipher.nil?

    key = compute_object_key(cipher)

    encrypted_data =
        if cipher == RC4 or cipher == Identity
            cipher.encrypt(key, self.value)
        else
            iv = Encryption.rand_bytes(AES::BLOCKSIZE)
            cipher.encrypt(key, iv, self.value)
        end

    @decrypted = false

    self.replace(encrypted_data)
    self.freeze

    self
end