编辑:2013-05-17。2013-05-27
在阅读了 Tom Leek 的第一个答案和网络上的一些文档之后,我开始为我的工具genpassphrase.pl编写一些选项:
$ ./genpassphrase.pl -h
Usage: genpassphrase.pl [-h] [-d dict file] [-i mIn length] [-a mAx length]
[-e entropy bits] [-r random file] [-w words] [-l lines] [lines]
Version: passphrase.pl v1.3 - (2013-05-12 10:43:14).
-h This help.
-l num number of phrases to generate (default: 1)
-w num number of words by phrase (default: 5)
-e bits Entropy bits for each words (default: 15)
-d filename Dictionary file (default: /usr/share/dict/american-english)
-i length Minimal word length (default: 4)
-a length Maximal word length (default: 11)
-r device Random file or generator (default: /dev/urandom)
默认输出如下所示:
With 5 words over 32768 ( 15 entropy bits ) = 1/3.777893e+22 -> 75 bits.
With 5 words from 56947 ( 15.797 entropy bits ) = 1/5.988999e+23 -> 78.987 bits.
3.736 206.819 foggier enforced albatrosses loftiest foursquare
第一行显示在字典中找到的 uniq 单词的计数,下降到2^Entropy. 第二行显示 uniq 单词的初始计数,并据此计算理论熵。
每个输出行以两个值开头,第一个是Shanon's entropy,我不清楚它的含义和用法。第二个是基于整行中的字符数,每个字符为 1/26。
计算熵减少
David Cary 的回答证实了这个计算非常近似且难以表示,但给出了一些很好的评价和一种思维方式:
我认为降低所有价值可能会对我的问题有所了解:
$ ./genpassphrase.pl -i 1 -a 1 -l 4
Warning: Bunch of 26 words too small! Entropy bits dropped down to 4 bits index.
With 5 words over 16 ( 4 entropy bits ) = 1/1.048576e+06 -> 20 bits.
With 5 words from 26 ( 4.700 entropy bits ) = 1/1.188138e+07 -> 23.502 bits.
2.322 23.502 f r h j u
1.922 23.502 t f g e f
1.922 23.502 r k i y r
2.322 23.502 y u x f i
这使得更容易表示人类对此的选择将如何减少熵:对于样本,如果我不喜欢一个、两个或最多 10 个字母,则基于 26 个字母的 4 位最终熵仍然保持不变......
所以通过扩展,如果超过一堆 56947 个词,我不会排除超过 24179 个词,15bit/word 的最终熵仍然保持不变,但是:
$ ./genpassphrase.pl -a 8
With 5 words over 32768 ( 15 entropy bits ) = 1/3.777893e+22 -> 75 bits.
With 5 words from 34954 ( 15.093 entropy bits ) = 1/5.217764e+22 -> 75.466 bits.
3.397 159.815 corded boosts hatters overhear rabbles
如果人类不选择,对于比字符长的示例单词,要排除的单词数将下降到 2186。最差:如果人类拒绝使用超过 7 个字符的单词(使用我的个人 dict 文件),这将下降总熵:
$ ./genpassphrase.pl -a 7
Warning: Bunch of 24366 words too small! Entropy bits dropped down to 14 bits index.
With 5 words over 16384 ( 14 entropy bits ) = 1/1.180592e+21 -> 70 bits.
With 5 words from 24366 ( 14.573 entropy bits ) = 1/8.588577e+21 -> 72.863 bits.
3.923 141.013 nitpick buglers loaders arms promo
到 70 位(可能是 72.8 位??),而不是 75。
从那里...
我想用一些简单的文档和建议来完成这个工具。
原帖
在搜索了生成随机密码短语的工具后,我开始了自己的...
拿我桌子上已经存在的字典:/usr/share/dict/american-english
看看:
wc -l /usr/share/dict/american-english
98569
快速浏览后,我看到许多's以大写字母开头的终止符和名称。
sed -ne '/^[a-z]\{4,99\}$/p' /usr/share/dict/american-english | wc -l
63469
哦,少于 65536,因为我不能只读取 15.953 位,所以我将把它降低到 15 位索引(使用伪随机,因为现在这可能就足够了。)。
与 5 个单词相比,我可以计算出 75 位密码:
#!/usr/bin/perl -w
use strict;
open my $fh, "</usr/share/dict/american-english" or die;
my @words = map { chomp $_; $_ } grep { /^[a-z]{4,11}$/ } <$fh>;
close $fh;
while (scalar @words > 32768 ) {
my $rndIdx=int( rand(1) * scalar @words );
splice @words, $rndIdx, 1 if $words[$rndIdx]=~/s$/ || int(rand()*3)==2;
}
open $fh, "</dev/random" or die;
$_='';
do { sysread $fh, my $buff, 10; $_.=$buff; } while 10 > length;
$_ = unpack "B80", $_;
s/([01]{15})/print " ".$words[unpack("s",pack("b15",$1))]/eg;
print "\n";
这可能会产生如下输出:
value nationally blacktopped prettify celebration
从那里,我有3个问题:
.1 一个词的最小长度是多少?4个字符就够了吗?如何计算 4 个字母单词的熵?
在普通字母表中,一个字母是 1/26 -> 4.7 位,但后面的字母通常是元音,所以 1/6 -> 2.5 位!?
如果我是对的,一个 4 个字母的单词不能代表超过 14.57 位??
.2有些人可能会尝试运行几次以获得一些选择:
for i in {1..6};do ./gen_pass_phrase.pl ; done
commons tweaking inhered driveways sedately
pantheon appeaser inmate quantifiers pyrite
loopier cloistering asceticism auctions table
value nationally blacktopped prettify celebration
fainer arthritis deplete vestry fostering
deuterium junipers luckless burro harmonic
并在这堆中选择 5 个单词是人类可感知的:
commons value fainer quantifiers celebration
这将减少熵:
性感的词更有机会被选中。
但我不能用数字论证来表示这一点。
.3从我玩的时候开始,我意识到 1/3 的单词是复数:
sed -ne '/^[a-z]\{4,9\}$/p' /usr/share/dict/american-english | wc -l
44476
sed -ne '/^[a-z]\{3,8\}s$/p' /usr/share/dict/american-english | wc -l
13408
我试图在删除多余的单词时对其进行补偿,因为我认为删除每个s终止的单词也不是一个好主意,所以我在s终止或 rand 1/3 时删除了 exedent。
没问题。
最大75 位的熵,似乎用这种方法下降了,但我不代表如何演示它们,也不代表如何计算它们。