【问题标题】:hash filenames into combinations of words将文件名散列成单词的组合
【发布时间】:2021-08-14 23:50:41
【问题描述】:

假设您有一个很长的文件名列表,例如:

[
"01_this_is_a_unnecessary_long_filename_that_I_cant_quite_shorten_arbitrarily.txt",
"this_is_another_unnecessary_long_filename_that_I_cant_quite_shorten_arbitrarily_21473q.txt",
...
]

我想将这些文件名映射为更短且更容易记住的文件名,例如:

["flying_dog", "red_bird", ...]

当然,我可以为每个单独的文件名字符串创建一个哈希,然后创建一个包含来自封闭集合的单词组合的查找表。

我只是想知道现成的东西是否已经存在?

【问题讨论】:

  • 您不需要这样的查找表。只需散列名称并使用散列的 N 组 M 位作为索引到 wordlist 中。 N 决定单词的数量,M 决定单词列表的基数。这就是BIP 39 的工作原理。

标签: python-3.x encryption hash


【解决方案1】:

用一种可能的实现扩展@Peter 的评论:

import hashlib
from mnemonic import Mnemonic

filename = "01_this_is_a_unnecessary_long_filename_that_I_cant_quite_shorten_arbitrarily.txt"

hash_object = hashlib.md5(filename).encode('UTF-8')
hash = hash_object.hexdigest().encode('UTF-8')

mnemo = Mnemonic("english")
words_code = mnemo.to_mnemonic(hash)

N = 4 # only use first N words; small N will create collision.
slug = '_'.join(words_code.split(' ')[:N])

print(slug)

这需要你安装mnemonic 包:

pip install mnemonic

Mnemonic 旨在为比特币钱包创建助记符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2011-04-16
    • 1970-01-01
    • 2018-11-07
    相关资源
    最近更新 更多