forked from sequentech/election-orchestra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha256.py
executable file
·55 lines (43 loc) · 1.67 KB
/
sha256.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of election-orchestra.
# Copyright (C) 2013-2016 Agora Voting SL <agora@agoravoting.com>
# election-orchestra is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License.
# election-orchestra is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with election-orchestra. If not, see <http://www.gnu.org/licenses/>.
import sys
import hashlib
from functools import partial
from base64 import urlsafe_b64encode
BUF_SIZE = 10*1024
def hash_data(data):
'''
Return the hexdigest of the data
'''
hash = hashlib.sha256()
hash.update(data)
return urlsafe_b64encode(hash.digest())
def hash_file(file_path):
'''
Returns the hexdigest of the hash of the contents of a file, given the file
path.
Same as executing:
openssl sha256 -binary <file_path> | openssl base64
'''
hash = hashlib.sha256()
f = open(file_path, 'r')
for chunk in iter(partial(f.read, BUF_SIZE), b''):
hash.update(chunk)
f.close()
return urlsafe_b64encode(hash.digest())
if __name__ == '__main__':
if len(sys.argv) < 2:
print("usage: %s <file-path> to obtain the sha256sum" % sys.argv[0])
exit(1)
print("sha256 hash of " + sys.argv[1] + " = " + hash_file(sys.argv[1]))