提问人:indra respati 提问时间:9/4/2023 更新时间:9/4/2023 访问量:4
从哈希结果中计算字符数的函数
function to count character from hashing result
问:
这段代码有什么问题:
这是 Sintax 的洞:
导入 bcrypt 导入哈希库 导入时间 导入字符串
示例密码 密码 =“mySecretP@ss”
def bcrypt_hash(密码): 盐 = bcrypt.gensalt() 返回 bcrypt.hashpw(password.encode(), salt)
def md5_hash(密码): 返回 hashlib.md5(password.encode()).hexdigest()
def sha1_hash(密码): 返回 hashlib.sha1(password.encode()).hexdigest()
def count_characters_and_punctuation(文本): num_characters = len(文本) num_punctuation = sum(如果 char in string.punctuation,则为 1 表示文本中的字符) 返回num_characters,num_punctuation
def main(): start_time = 时间.time() bcrypt_result = bcrypt_hash(密码) bcrypt_time = (time.time() - start_time) * 1000 # 转换为毫秒
start_time = 时间.time() md5_result = md5_hash(密码) md5_time = (time.time() - start_time) * 1000
start_time = 时间.time() sha1_result = sha1_hash(密码) sha1_time = (time.time() - start_time) * 1000
print(f“密码:{密码}”) print(f“BCrypt 哈希: {bcrypt_result}”) print(f“MD5 哈希: {md5_result}”) print(f“SHA-1 哈希:{sha1_result}”)
print(f“BCrypt 所用时间:{bcrypt_time:.6f} ms”) print(f“MD5 所用时间:{md5_time:.6f} ms”) print(f“SHA-1 所用时间:{sha1_time:.6f} ms”)
bcrypt_char_count,bcrypt_punctuation_count = count_characters_and_punctuation(bcrypt_result) md5_char_count,md5_punctuation_count = count_characters_and_punctuation(md5_result) sha1_char_count,sha1_punctuation_count = count_characters_and_punctuation(sha1_result)
print(f“BCrypt:字符数:{bcrypt_char_count},标点符号数:{bcrypt_punctuation_count}”) print(f“MD5:字符数:{md5_char_count},标点符号数:{md5_punctuation_count}”) print(f“SHA-1:字符数:{sha1_char_count},标点符号数:{sha1_punctuation_count}”)
如果名称 == “main”: main()
20 num_characters = len(文本) ---> 21 num_punctuation = sum(1 表示文本中的字符,如果 char in string.punctuation) 22 往返num_characters,num_punctuation 23
TypeError: 'in ' 需要字符串作为左操作数,而不是 int
答: 暂无答案
评论