]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ContentId.pm
65d5a76c09b43f4dec2edb08998c22ffe6d6e95e
[public-inbox.git] / lib / PublicInbox / ContentId.pm
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 package PublicInbox::ContentId;
5 use strict;
6 use warnings;
7 use base qw/Exporter/;
8 our @EXPORT_OK = qw/content_id/;
9
10 # not sure if less-widely supported hash families are worth bothering with
11 use Digest::SHA;
12
13 # Content-* headers are often no-ops, so maybe we don't need them
14 my @ID_HEADERS = qw(Subject From Date Message-ID References To Cc In-Reply-To);
15
16 sub content_id ($;$) {
17         my ($mime, $alg) = @_;
18         $alg ||= 256;
19         my $dig = Digest::SHA->new($alg);
20         my $hdr = $mime->header_obj;
21
22         foreach my $h (@ID_HEADERS) {
23                 my @v = $hdr->header_raw($h);
24                 $dig->add($_) foreach @v;
25         }
26         $dig->add($mime->body_raw);
27         'SHA-' . $dig->algorithm . ':' . $dig->hexdigest;
28 }
29
30 1;