summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSiddharth Ravikumar <sravik@bgsu.edu>2015-01-21 10:43:43 -0500
committerSiddharth Ravikumar <sravik@bgsu.edu>2015-01-21 10:43:43 -0500
commit26282a6a6331cc0f8d6d8e239a335507de715682 (patch)
treec55bb87c3096e5f4a744ffd590531db7db246ab1
parent6d53e749aef11732e222b64f3fdd427991e7194e (diff)
combox/file.py: defined hash_file function.
hash_file function returns the SHA512 hash of the file's content. The test for the hash_file function is at tests/file_test.py
-rw-r--r--combox/file.py13
-rw-r--r--tests/file_test.py14
2 files changed, 26 insertions, 1 deletions
diff --git a/combox/file.py b/combox/file.py
index 563e8cd..e4677e9 100644
--- a/combox/file.py
+++ b/combox/file.py
@@ -18,6 +18,7 @@
import os
+from hashlib import sha512
from os import path
from sys import exit
from glob import glob
@@ -243,6 +244,18 @@ def read_file(filename):
return file_.read()
+def hash_file(filename):
+ """Does a SHA512 hash on the contents of file.
+
+ Returns the hexdigest of the file content's hash.
+
+ filename: Absolute pathname of the file.
+ """
+ file_content = read_file(filename)
+
+ return sha512(file_content).hexdigest()
+
+
def write_file(filename, filecontent):
"""Write `filecontent' to `filename'.
diff --git a/tests/file_test.py b/tests/file_test.py
index 558bcd7..4787406 100644
--- a/tests/file_test.py
+++ b/tests/file_test.py
@@ -18,13 +18,15 @@
import yaml
+from hashlib import sha512
from glob import glob
from nose.tools import *
from os import path, remove
from combox.config import get_nodedirs
from combox.file import (split_data, glue_data, write_file,
- read_file, write_shards, read_shards)
+ read_file, write_shards, read_shards,
+ hash_file)
CONFIG_DIR = path.join('tests', 'test-config')
@@ -82,3 +84,13 @@ and check if they're the same as the orginal file.
f_content_glued = glue_data(f_shards)
assert f_content == f_content_glued
+
+
+def test_hashing():
+ """
+ Tests the hashing function - hash_file
+ """
+ fhash = hash_file(TEST_FILE)
+ fcontent = read_file(TEST_FILE)
+
+ assert fhash == sha512(fcontent).hexdigest()