1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 package PublicInbox::ContentId;
8 our @EXPORT_OK = qw/content_id/;
10 # not sure if less-widely supported hash families are worth bothering with
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);
16 sub content_id ($;$) {
17 my ($mime, $alg) = @_;
19 my $dig = Digest::SHA->new($alg);
20 my $hdr = $mime->header_obj;
22 foreach my $h (@ID_HEADERS) {
23 my @v = $hdr->header_raw($h);
24 $dig->add($_) foreach @v;
26 $dig->add($mime->body_raw);
27 'SHA-' . $dig->algorithm . ':' . $dig->hexdigest;