我使用哈希函数来计算各种文件的哈希值。这是代码,但我得到的名称错误“选项”没有定义。我想我不会这么做的好吧。随便建议?我在代码中使用了之前的选项,所以有什么问题?在#!/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()

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐