需要一些关于在 Perl 中生成校验和的指导

发布于 2024-10-17 18:15:09 字数 263 浏览 2 评论 0原文

我需要在 Perl 中为一堆数据生成校验和,我遇到了这个 Digest::MD5 模块。看起来它符合要求,但我想我会在这里询问是否有人有任何建议或可能知道更好的模块可以使用,甚至可能是更合适的摘要算法。被散列的内容是大约 10 个表的数据(一次一个逻辑元组)。这将是我第一次使用校验和,所以任何提示、技巧、陷阱将不胜感激。

编辑:据我所知,Digest:MD5 没有任何问题,但我从未使用过它,也不熟悉哈希算法。我希望有经验的人能够告诉我我是否走在正确的道路上。只是想在走得太远之前得到一点确认。

I'm needing to produce a checksum for a bunch of data in Perl, and I came across this Digest::MD5 module. It looks like it will fit the bill, but I thought I would ask here to see if anyone has any advise or possibly knows of a better module to use or even maybe a more suitable digest algorithm. What's being hashed is about 10 tables worth of data (one logical tuple at a time). This will be the first time I make use of checksums, so any tips, tricks, gotchas would be very appreciated.

Edit: As far as I know, there's nothing wrong with Digest:MD5, but I've never used it nor am I familiar with hash algorithms. I was hoping someone with experience would be able to tell me if I was on the right track or not. Just wanted a little bit of confirmation before going too far.

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

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

发布评论

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

评论(1

是的, Digest::MD5 就可以了;它由 Gisle Aas(LWP 以及其他优秀软件包的作者)编写,并拥有良好的评论和cpan ratings 上的评级,这两者都应该让您放心,这是一个不错的选择。

使用它可以很简单:

my $checksum =  Digest::MD5::md5_hex($data);

如果您认为将来可能会更改您选择的摘要算法(例如,使用 SHA-1),您可能需要考虑使用 Digest - 也是由 Gisle Aas 编写,并为各种 Digest 模块提供了一个简单的接口。

例如:

my $digest = Digest->new('MD5');
$digest->add($data);             # to add data from a scalar, or:
$digest->add_file($filehandle);  # to add data read from a filehandle
my $checksum = $digest->hexdigest;  # or just ->digest for binary

该方法的好处是您只需将“MD5”更改为“SHA-1”,然后就完成了。

为了完整起见,我将添加为什么您可能希望设计能够轻松使用其他哈希算法 - 如果这用于任何安全目的,MD5 已被证明容易受到哈希冲突 - 美国国土安全部建议 MD5“应被视为加密已损坏且不适合进一步使用”。然而,对于数据完整性的一般检查,它仍然是许多人可以接受的选择,并且得到广泛支持。

SHA-1 也被认为是弱的; SHA-2 被认为是用于加密目的的安全散列的最佳选择。

Yes, Digest::MD5 will do the trick; it's written by Gisle Aas (author of LWP among other excellent packages) and has good reviews & ratings on cpanratings, both of which should reassure you that it's a good choice.

Using it can be as simple as:

my $checksum =  Digest::MD5::md5_hex($data);

If you think you may be likely to change your chosen digest algorithm in future (for instance, using SHA-1 instead), you might want to consider using Digest instead - also written by Gisle Aas, and providing an easy interface to various Digest modules.

For example:

my $digest = Digest->new('MD5');
$digest->add($data);             # to add data from a scalar, or:
$digest->add_file($filehandle);  # to add data read from a filehandle
my $checksum = $digest->hexdigest;  # or just ->digest for binary

That approach has the benefit that you could just change the "MD5" to e.g. "SHA-1", and you're done.

Just for completeness, I'll add why you might want to design with the ability to use other hashing algorithms easily - if this was used for any security purposes, MD5 has been shown to be vulnerable to hash collisions - the US Department of Homeland Security advises that MD5 "should be considered cryptographically broken and unsuitable for further use". However, for general checking of data integrity, it's still an acceptable choice for many, and is widely supported.

SHA-1 is also considered weak; SHA-2 is considered the best choice for secure hashing for cryptographic purposes.

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