是否可以创建像
这样的摘要my $digest = Digest::SHA->new('sha1')->add('hello')->digest;
然后将$digest
转换为base10(而不是hex或base64?)或者您可以将hexdigest拆分为5 pieces然后将它们转换为整数吗?那会有用吗?试图想出一种将摘要存储为整数的方法(是的,我知道有人会认为我疯了或愚蠢,可能两者都有)。
更新
理论上我应该能够获取最终的编码整数输出并反向并根据需要重新编码为十六进制和base64。
答案 0 :(得分:4)
如果您要去的地方可以处理任意精度的整数而不退缩(是的,不合时宜,但你永远不知道),那么你可以将它编码为一个整数;)
$ re.pl
$ use Digest::SHA; use Math::BigInt;
$ my $hex = Digest::SHA->new("SHA-1")->add("hello")->hexdigest;
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
$ Math::BigInt->from_hex("0x" . $hex)->bstr
975987071262755080377722350727279193143145743181
$ Math::BigInt->new("975987071262755080377722350727279193143145743181")->as_hex
0xaaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
答案 1 :(得分:3)
我认为pack
& unpack
可能会更有效率:
use Digest::SHA;
my $digest = Digest::SHA->new('sha1')->add('hello')->digest;
my @ints = unpack('N*', $digest);
print "@ints\n";
my $redone = pack('N*', @ints);
print "match\n" if $digest eq $redone;
my $hexdigest = sprintf '%08x' x @ints, @ints;
print "$hexdigest\n";
printf "%s\n", Digest::SHA->new('sha1')->add('hello')->hexdigest;
use MIME::Base64 'encode_base64';
my $b64digest = encode_base64(pack('N*', @ints));
print $b64digest;
printf "%s\n", Digest::SHA->new('sha1')->add('hello')->b64digest;
输出:
2868168221 3703957666 3669941775 994585817 2930328397
match
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
qvTGHdzF6KLavt4PO0gs2a6pQ00=
qvTGHdzF6KLavt4PO0gs2a6pQ00
请注意,MIME::Base64
会填充结果,b64digest
方法则不会。但是,如果需要,可以轻松删除尾随的=
。
答案 2 :(得分:0)
这样的事,也许?
#!/usr/bin/perl
use strict; use warnings;
use Digest::SHA;
use YAML;
my $digest = Digest::SHA->new('sha1')->add('hello')->hexdigest;
print "$digest\n";
print Dump [ map { [$_, hex] } $digest =~ /([[:xdigit:]]{8})/g ];
输出:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d --- - - aaf4c61d - 2868168221 - - dcc5e8a2 - 3703957666 - - dabede0f - 3669941775 - - 3b482cd9 - 994585817 - - aea9434d - 2930328397