Module: Duse::Encryption

Extended by:
Encryption
Included in:
Encryption
Defined in:
lib/duse/encryption.rb

Defined Under Namespace

Modules: Asymmetric, Encoding, Symmetric

Instance Method Summary collapse

Instance Method Details

#decrypt(cipher_text, shares, private_key) ⇒ Object



89
90
91
92
# File 'lib/duse/encryption.rb', line 89

def decrypt(cipher_text, shares, private_key)
  key, iv = decrypt_symmetric_key(shares, private_key).split ' '
  Encryption::Symmetric.decrypt(key, iv, cipher_text)
end

#decrypt_symmetric_key(shares, private_key) ⇒ Object



103
104
105
106
107
108
# File 'lib/duse/encryption.rb', line 103

def decrypt_symmetric_key(shares, private_key)
  raw_shares = shares.map do |share|
    Encryption::Asymmetric.decrypt private_key, share
  end
  SecretSharing.recover_secret(raw_shares)
end

#encrypt(secret_text, users, private_key) ⇒ Object



83
84
85
86
87
# File 'lib/duse/encryption.rb', line 83

def encrypt(secret_text, users, private_key)
  key, iv, cipher_text = Encryption::Symmetric.encrypt secret_text
  shares = encrypt_symmetric_key("#{key.strip} #{iv.strip}", users, private_key)
  [cipher_text, shares]
end

#encrypt_symmetric_key(symmetric_key, users, private_key) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/duse/encryption.rb', line 94

def encrypt_symmetric_key(symmetric_key, users, private_key)
  raw_shares = SecretSharing.split_secret(symmetric_key, 2, users.length)
  users.map.with_index do |user, index|
    share = raw_shares[index]
    content, signature = Encryption::Asymmetric.encrypt(private_key, user.public_key, share)
    {"user_id" => user.id, "content" => content, "signature" => signature}
  end
end