Bash 在脚本中循环和散列值

Bash looping through and hashing values in a script

大约有 600 个单词。有一个 Google Sheet,其中 ~600 个单词都在列(类似于 B1:AV1)和行(类似于 A2:A640)中。所有的中间都连接了行的单词,space,和列的单词。一个简单的例子,说明它是 4x 时的样子:

我正在尝试获取每个单词组合的 SHA-256 总和,以便我可以找到我拥有的最终值中的两个单词(并且正在尝试将组合匹配到)。

一位朋友告诉我最好的方法是下载传播sheet然后使用bash脚本遍历sheet的每个元素和return 单词的组合 if/when 它找到与我要匹配的那个对应的哈希和。

请帮忙!

您没有指定是哪种语言,所以我选择 python 我最熟悉的语言,而且库也很棒。

您可以执行以下操作:

import itertools
import hashlib

words = ["the","quick","brown","fox"]

all_permutations = [p for p in itertools.product(words, repeat=2)]

# The hash is calculated from the both words joined with a space e.g. 'brown fox'
hashes = {hashlib.sha256(" ".join(permutation).encode('utf-8')).hexdigest():permutation for permutation in all_permutations}

hash_to_look_for = 'fb565eb6a7d52f851e9c3ffdcf2ff1b78fff40b6f5afeb8efa4be231ba987409'

print(hashes[hash_to_look_for])

生成输出:

('quick', 'fox')

它是如何工作的:

  1. 指定单词列表 words = ["the","quick","brown","fox"] 当然你也可以从文件中生成这个单词列表 internet/Whosebug
  2. 上有足够的 "How-To"
  3. 生成所有排列(基本上就是你的 nxn table)。 all_permutations = [p for p in itertools.product(words, repeat=2)]
  4. 生成一个键值存储(字典),键为散列,值为排列:hashes = {hashlib.sha256(" ".join(permutation).encode('utf-8')).hexdigest():permutation for permutation in all_permutations} 基本上你有一个包含以下条目的字典:{'9324c764e9cd717efd7c30d439dc0ce9cf7ecff6b6b00a67eb24676ba3bc34ba': ('the', 'the') 总共 nxn 次(在示例 16 个条目)
  5. 我指定要查找的哈希值:hash_to_look_for = 'fb565eb6a7d52f851e9c3ffdcf2ff1b78fff40b6f5afeb8efa4be231ba987409'
  6. 由于散列始终是我们字典中的键,我们可以使用此命令轻松打印它:hashes[hash_to_look_for] 并获得生成此散列的所需排列。