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



392
393
394
# File 'lib/origami/encryption.rb', line 392

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

Instance Method Details

#decrypt!Object

Raises:



420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/origami/encryption.rb', line 420

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:



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/origami/encryption.rb', line 396

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