]> Sergey Matveev's repositories - public-inbox.git/commitdiff
content_id: no need to be human-friendly
authorEric Wong (Contractor, The Linux Foundation) <e@80x24.org>
Fri, 2 Mar 2018 18:27:54 +0000 (18:27 +0000)
committerEric Wong (Contractor, The Linux Foundation) <e@80x24.org>
Fri, 2 Mar 2018 18:28:54 +0000 (18:28 +0000)
We merely use this for internal comparisons and do not store
this in Xapian.  So using a shorter, non-human readable digest
is enough.  Furthermore, introduce "content_digest" which
returns the Digest::SHA object for extra changes.

lib/PublicInbox/ContentId.pm
t/content_id.t

index d1a009e77f2ed9e9e8b4de30a79f85a706ef37e8..8347de2d2846654ef2042c4d34aa6d4595e92dc1 100644 (file)
@@ -5,7 +5,7 @@ package PublicInbox::ContentId;
 use strict;
 use warnings;
 use base qw/Exporter/;
-our @EXPORT_OK = qw/content_id/;
+our @EXPORT_OK = qw/content_id content_digest/;
 use PublicInbox::MID qw(mids references);
 
 # not sure if less-widely supported hash families are worth bothering with
@@ -14,10 +14,9 @@ use Digest::SHA;
 # Content-* headers are often no-ops, so maybe we don't need them
 my @ID_HEADERS = qw(Subject From Date To Cc);
 
-sub content_id ($;$) {
-       my ($mime, $alg) = @_;
-       $alg ||= 256;
-       my $dig = Digest::SHA->new($alg);
+sub content_digest ($) {
+       my ($mime) = @_;
+       my $dig = Digest::SHA->new(256);
        my $hdr = $mime->header_obj;
 
        # References: and In-Reply-To: get used interchangeably
@@ -37,7 +36,11 @@ sub content_id ($;$) {
                $dig->add("$h: $_") foreach @v;
        }
        $dig->add($mime->body_raw);
-       'SHA-' . $dig->algorithm . ':' . $dig->hexdigest;
+       $dig;
+}
+
+sub content_id ($) {
+       content_digest($_[0])->digest;
 }
 
 1;
index c0ae6ecde27a47bd440ebd5c93a456182365400e..adcdb6c19a66a999f96189a75cc95a54aa2e62df 100644 (file)
@@ -18,7 +18,8 @@ my $mime = Email::MIME->create(
        body => "hello world\n",
 );
 
-my $res = content_id($mime);
-like($res, qr/\ASHA-256:[a-f0-9]{64}\z/, 'cid in format expected');
+my $orig = content_id($mime);
+my $reload = content_id(Email::MIME->new($mime->as_string));
+is($orig, $reload, 'content_id matches after serialization');
 
 done_testing();