Python 中的 while 循环中的字符串不情愿地连接

发布于 2025-01-13 00:02:24 字数 1108 浏览 4 评论 0原文

我正在编写一个简单的程序,它从文件中获取一个字符串并对其进行哈希处理。由于某种原因,循环不情愿地连接字符串。它在 for 循环和 while 循环之外工作,但在其中执行一些时髦的事情。这是我的代码。

import hashlib

f = open('1000-most-common-passwords.txt', 'r')  # Opens file with all of the strings to compare it to.

unparsed = f.read()
unparsed = unparsed.replace('\n', ' ').split(' ')  # Turns string into list with every new line.
sha1 = hashlib.sha1()
sha1.update(unparsed[0].encode('utf-8'))  # Line 1 is hashed into SHA-1.

这效果很好。我可以替换 unparsed[0] 中的索引,它会从该行中选择字符串并将其打印出哈希值。现在,我想对文本文件中的每一行执行此操作,因此我编写了一个简单的 while 循环。看起来是这样的。

i = 0  # Selects the first line.
while i < len(unparsed):  # While i is less than the amount of values in the list, keep going.
    sha1.update(unparsed[i].encode('utf-8'))  # Update the index to the current value in the list.
    print(sha1.hexdigest())
    i += 1

这不会给我任何错误。相反,它看起来就像我想要的样子。但它实际上的作用让我感到困扰。它不是为我提供每个值的哈希值,而是为我提供所有先前哈希值的某种串联。它不是对 123456 进行哈希处理,而是对 123456123456123456password 进行哈希处理。为什么这可以在循环之外工作,但不能在循环内部工作?非常感谢任何帮助。

I'm writing a simple program that takes a string from a file and hashes it. The loop unwillingly concatenates strings for some reason. It works outside of a for loop and while loop, but does funky stuff inside of one. Here's my code.

import hashlib

f = open('1000-most-common-passwords.txt', 'r')  # Opens file with all of the strings to compare it to.

unparsed = f.read()
unparsed = unparsed.replace('\n', ' ').split(' ')  # Turns string into list with every new line.
sha1 = hashlib.sha1()
sha1.update(unparsed[0].encode('utf-8'))  # Line 1 is hashed into SHA-1.

This works well. I can substitue the index in unparsed[0] and it selects the string from that line and prints it out hashed. Now, I'd like to do this for every line in the text file, so I wrtoe a simple while loop. Here's how that looks.

i = 0  # Selects the first line.
while i < len(unparsed):  # While i is less than the amount of values in the list, keep going.
    sha1.update(unparsed[i].encode('utf-8'))  # Update the index to the current value in the list.
    print(sha1.hexdigest())
    i += 1

This doesn't give me any errors. To the contrary, it looks like how I want it to look. But what it actually does bothers me. Instead of giving me the hash for each value, it gives me some sort of concatonation of all previous hashes. Instead of hashing 123456, it hashes 123456123456 or 123456password. Why does this work outside of a loop but not inside of one? Any help is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

避讳 2025-01-20 00:02:24

看起来你想单独散列每一行; update 将继续对您提供的所有数据进行哈希处理,因此您需要每行创建一个新的哈希对象才能获得您想要的内容。方法如下:

from hashlib import sha1

# Just read the file in binary mode so you don't have to re-encode it:
with open('1000-most-common-passwords.txt', 'rb') as f:
  for line in f.readlines():  # iterate over all the lines in the file
    pw = line.strip()  # Don't include the trailing newline in the hash
    digest = sha1(pw).hexdigest()
    print(f'{pw} hashes to {digest}')

Seems like you want to hash each line separately; update is going to keep hashing all the data you give it, so instead you need to create a new hash object per line to get what you want. Here's how:

from hashlib import sha1

# Just read the file in binary mode so you don't have to re-encode it:
with open('1000-most-common-passwords.txt', 'rb') as f:
  for line in f.readlines():  # iterate over all the lines in the file
    pw = line.strip()  # Don't include the trailing newline in the hash
    digest = sha1(pw).hexdigest()
    print(f'{pw} hashes to {digest}')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文