Class: DIDWW::Encrypt

Inherits:
Object
  • Object
show all
Defined in:
lib/didww/encrypt.rb

Overview

Allows to encrypt file on Ruby-side before uploading to ‘/v3/encrypted_files`.

Examples:

file_content_1 = File.read('file_to_send_1.jpg', mode: 'rb')
file_content_2 = File.read('file_to_send_1.pdf', mode: 'rb')
enc = DIDWW::Encrypt.new
enc_data_1 = enc.encrypt(file_content_1)
enc_data_2 = enc.encrypt(file_content_2)
enc_io_1 = Faraday::UploadIO.new(StringIO.new(enc_data_1), 'application/octet-stream')
enc_io_2 = Faraday::UploadIO.new(StringIO.new(enc_data_2), 'application/octet-stream')
DIDWW::Resource::EncryptedFile.upload(
  encryption_fingerprint: enc.encryption_fingerprint,
  items: [
    { file: enc_io_1, description: 'file_to_send_1.jpg' },
    { file: enc_io_2, description: 'file_to_send_2.pdf' }
  ]
) # => Array if IDs

Constant Summary collapse

AES_ALGO =
[256, :CBC]
AES_KEY_LEN =
32
AES_IV_LEN =
16
LABEL =
''
SEPARATOR =
':::'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEncrypt

Returns a new instance of Encrypt.



41
42
43
# File 'lib/didww/encrypt.rb', line 41

def initialize
  reset!
end

Instance Attribute Details

#encryption_fingerprintObject (readonly)

Returns the value of attribute encryption_fingerprint.



39
40
41
# File 'lib/didww/encrypt.rb', line 39

def encryption_fingerprint
  @encryption_fingerprint
end

#public_keysObject (readonly)

Returns the value of attribute public_keys.



39
40
41
# File 'lib/didww/encrypt.rb', line 39

def public_keys
  @public_keys
end

Class Method Details

.encrypt(binary) ⇒ String

Parameters:

  • binary (String)

Returns:

  • (String)


34
35
36
# File 'lib/didww/encrypt.rb', line 34

def encrypt(binary)
  new.encrypt(binary)
end

Instance Method Details

#encrypt(binary) ⇒ String

Returns binary content of an encrypted file.

Parameters:

  • binary (String)

    binary content of a file.

Returns:

  • (String)

    binary content of an encrypted file.



47
48
49
50
51
52
53
# File 'lib/didww/encrypt.rb', line 47

def encrypt(binary)
  aes_key, aes_iv, encrypted_aes = encrypt_aes(binary)
  aes_credentials = aes_key + aes_iv
  encrypted_rsa_a = encrypt_rsa_oaep(public_keys[0], aes_credentials)
  encrypted_rsa_b = encrypt_rsa_oaep(public_keys[1], aes_credentials)
  encrypted_rsa_a + encrypted_rsa_b + encrypted_aes
end

#reset!Object

Resets public keys and fingerprint.



56
57
58
59
# File 'lib/didww/encrypt.rb', line 56

def reset!
  @public_keys = fetch_public_keys
  @encryption_fingerprint = calculate_fingerprint
end