将摘要转换为 base10

发布于 2024-09-13 07:12:53 字数 442 浏览 7 评论 0原文

是否可以采用类似创建的摘要

my $digest  = Digest::SHA->new('sha1')->add('hello')->digest;

,然后将 $digest 转换为 base10(而不是十六进制或 base64?),或者您可以将十六进制摘要拆分为 5 块 然后将它们转换为整数?那行得通吗?试图想出一种将摘要存储为整数的方法(是的,我知道有人会认为我疯了或愚蠢,可能两者兼而有之)。

更新

理论上,我应该能够获取最终编码的整数输出,并根据需要反转并重新编码为十六进制和 base64。

Is it possible to take a digest created like

my $digest  = Digest::SHA->new('sha1')->add('hello')->digest;

and then convert $digest to base10 (instead of hex or base64?) or could you split a hexdigest into 5 pieces and then convert those into integers? would that work? Trying to come up with a way to store the digest as integers (yes I know someone is going to think I'm crazy or stupid, probably both).

update

In theory I should be able to take the final encoded integer output and reverse and reencode to hex and base64 as needed.

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

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

发布评论

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

评论(3

甜`诱少女 2024-09-20 07:12:53

如果无论你要去哪里都可以毫不犹豫地处理任意精度的整数(是的,很奇怪,但你永远不知道),那么你可以将其编码为单个整数;)

$ 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

If wherever you're going can handle arbitrary-precision integers without flinching (yeah, unikely, but you never know), then you can just encode it as a single integer ;)

$ 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
萌梦深 2024-09-20 07:12:53

我认为 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 方法则不会。但如果需要的话,删除尾随的 = 很容易。

I think pack & unpack will probably be more efficient:

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;

Output:

2868168221 3703957666 3669941775 994585817 2930328397
match
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
qvTGHdzF6KLavt4PO0gs2a6pQ00=
qvTGHdzF6KLavt4PO0gs2a6pQ00

Note that MIME::Base64 pads the result, and the b64digest method doesn't. But it's easy enough to strip off the trailing = if you need to.

请别遗忘我 2024-09-20 07:12:53

也许是这样的?

#!/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

Something like this, maybe?

#!/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 ];

Output:

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
---
-
  - aaf4c61d
  - 2868168221
-
  - dcc5e8a2
  - 3703957666
-
  - dabede0f
  - 3669941775
-
  - 3b482cd9
  - 994585817
-
  - aea9434d
  - 2930328397
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文