我使用哈希函数来计算各种文件的哈希值。这是代码,但我得到的名称错误“选项”没有定义。我想我不会这么做的好吧。随便建议?我在代码中使用了之前的选项,所以有什么问题?在#!/usr/bin/python
import sys
import itertools
import hashlib
# function reads file and calculate the MD5 signature
def calcMd5Hash(filename):
hash = hashlib.md5()
with open(filename) as f:
for chunk in iter(lambda: f.read(4096), ""):
hash.update(chunk)
return hash.hexdigest()
# function reads file and calculate the SHA1 signature
def calcSHA1Hash(filename):
hash = hashlib.sha1()
with open(filename) as f:
for chunk in iter(lambda: f.read(4096), ""):
hash.update(chunk)
return hash.hexdigest()
# function reads file and calculate the SHA256 signature
def calcSHA256Hash(filename):
hash = hashlib.sha256()
with open(filename) as f:
for chunk in iter(lambda: f.read(4096), ""):
hash.update(chunk)
return hash.hexdigest()
def main():
num = input("Select the hashing method you wish to use:\n 1. MD5\n 2. SHA1\n
3. SHA256\n")
options = {
1: calcMd5Hash,
2: calcSHA1Hash,
3: calcSHA256Hash,
}
# test for enough command line arguments
if len(sys.argv) < 3:
print("Usage python calculate_hash.py ")
return
hashString = options[num](sys.argv[1])
print("The MD5 hash of file named: "+str(sys.argv[1])+" is: "+options[num]
(sys.argv[1]))
main()