summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSiddharth Ravikumar <sravik@bgsu.edu>2015-01-13 23:15:12 -0500
committerSiddharth Ravikumar <sravik@bgsu.edu>2015-01-13 23:15:12 -0500
commitff9f0ada050d1b3d13862786a099a060f38c9169 (patch)
tree6b8e1ba49c94a24c0001c69b13237fdb7bf40dd1
parent30cf34bef72df235be4ada2e7b15afbeeb84dd23 (diff)
combox/crypto.py: wrote split_and_encrypt & decrypt_and_glue functions
-rw-r--r--combox/crypto.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/combox/crypto.py b/combox/crypto.py
index 3f17cc0..a353113 100644
--- a/combox/crypto.py
+++ b/combox/crypto.py
@@ -21,7 +21,13 @@
import base64
import os
+from combox.config import get_nodedirs
+from combox.file import (read_file, write_file,
+ read_shards, write_shards,
+ split_data, glue_data)
+
from Crypto.Cipher import AES
+from os import path
BLOCK_SIZE = 32
PAD_CHAR = '#'
@@ -82,3 +88,58 @@ def decrypt_shards(ciphers, secret):
shards.append(shard)
return shards
+
+
+def split_and_encrypt(fpath, config):
+ """
+ Splits the file, encrypts the shards and writes them to the nodes.
+
+ fpath: The path to file that has to be split.
+ config: The dictonary containing the combox configuration information.
+ """
+
+ # no. of shards = no. of nodes.
+ SHARDS = len(config['nodes_info'].keys())
+
+ f = path.abspath(fpath)
+ f_content = read_file(f)
+ f_shards = split_data(f_content, SHARDS)
+
+ # encrypt shards
+ ciphered_shards = encrypt_shards(f_shards, config['topsecret'])
+
+
+ # write ciphered shards to disk
+ f_basename = path.basename(f)
+ # gets the list of node' directories.
+ nodes = get_nodedirs(config)
+
+ write_shards(ciphered_shards, nodes, f_basename)
+
+
+def decrypt_and_glue(fpath, config):
+
+ """
+ Reads encrypted shards, decrypts and glues them.
+
+ fpath: The path to file that has to be decrypted & glued from the nodes.
+ config: The dictonary containing the combox configuration information.
+
+ """
+
+ f = path.abspath(fpath)
+ f_basename = path.basename(f)
+ # gets the list of node' directories.
+ nodes = get_nodedirs(config)
+
+ ciphered_shards = read_shards(nodes, f_basename)
+
+ # decrypt shards
+ f_parts = decrypt_shards(ciphered_shards, config['topsecret'])
+
+ # glue them together
+ f_content = glue_data(f_parts)
+
+ # write the glued content to fpath
+ write_file(f, f_content)
+.