OSDN Git Service

53cfca3a49b91c74d8d32d46dfca7b4e988b1058
[mhash384/mhash384.git] / bindings / Python / example / Example.py
1 ##################################################################################################
2 # MHash-384 - Language bindings for Python                                                       #
3 # Copyright(c) 2016 LoRd_MuldeR <mulder2@gmx.de>                                                 #
4 #                                                                                                #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy of this software  #
6 # and associated documentation files (the "Software"), to deal in the Software without           #
7 # restriction, including without limitation the rights to use, copy, modify, merge, publish,     #
8 # distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the  #
9 # Software is furnished to do so, subject to the following conditions:                           #
10 #                                                                                                #
11 # The above copyright notice and this permission notice shall be included in all copies or       #
12 # substantial portions of the Software.                                                          #
13 #                                                                                                #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING  #
15 # BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND     #
16 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   #
17 # DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.        #
19 ##################################################################################################
20
21 import sys
22 import binascii
23 from MHashPy384_Wrapper import MHash384
24
25 def read_chunks(fs, chunk_size=4096):
26     while True:
27         data = fs.read(chunk_size)
28         if not data:
29             break
30         yield data
31
32 def main(argv):
33     if(len(argv) < 1):
34         print("Command-line argument is missing!")
35         return
36     with MHash384() as digest:
37         with open(argv[0], 'rb') as fs:
38             for chunk in read_chunks(fs):
39                 digest.update(chunk)
40             print(binascii.hexlify(digest.result()))
41
42 if __name__ == "__main__":
43     main(sys.argv[1:])