Module: Duse::Encryption

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

Defined Under Namespace

Modules: Asymmetric, CryptographicHash, Digest, Encoding, Symmetric

Instance Method Summary collapse

Methods included from Encoding

decode, encode

Instance Method Details

#decrypt(cipher_text, shares, private_key) ⇒ Object



109
110
111
112
# File 'lib/duse/encryption.rb', line 109

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



127
128
129
130
131
132
# File 'lib/duse/encryption.rb', line 127

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

#encrypt(secret_text, users, private_key) ⇒ Object



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

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



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/duse/encryption.rb', line 114

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

#hmac(key, data) ⇒ Object



99
100
101
# File 'lib/duse/encryption.rb', line 99

def hmac(key, data)
  Duse::Encryption::CryptographicHash.hmac(key, data)
end