From 5b491c0b308e576897cfcc270647ba4e35c6cc8a Mon Sep 17 00:00:00 2001 From: "Eric Wong (Contractor, The Linux Foundation)" Date: Sat, 10 Feb 2018 05:13:19 +0000 Subject: import: initial handling for v2 Call order will need to change a bit since this is going to be tied to Xapian --- lib/PublicInbox/ContentId.pm | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/PublicInbox/ContentId.pm (limited to 'lib/PublicInbox/ContentId.pm') diff --git a/lib/PublicInbox/ContentId.pm b/lib/PublicInbox/ContentId.pm new file mode 100644 index 00000000..65d5a76c --- /dev/null +++ b/lib/PublicInbox/ContentId.pm @@ -0,0 +1,30 @@ +# Copyright (C) 2018 all contributors +# License: AGPL-3.0+ + +package PublicInbox::ContentId; +use strict; +use warnings; +use base qw/Exporter/; +our @EXPORT_OK = qw/content_id/; + +# not sure if less-widely supported hash families are worth bothering with +use Digest::SHA; + +# Content-* headers are often no-ops, so maybe we don't need them +my @ID_HEADERS = qw(Subject From Date Message-ID References To Cc In-Reply-To); + +sub content_id ($;$) { + my ($mime, $alg) = @_; + $alg ||= 256; + my $dig = Digest::SHA->new($alg); + my $hdr = $mime->header_obj; + + foreach my $h (@ID_HEADERS) { + my @v = $hdr->header_raw($h); + $dig->add($_) foreach @v; + } + $dig->add($mime->body_raw); + 'SHA-' . $dig->algorithm . ':' . $dig->hexdigest; +} + +1; -- cgit v1.2.3-24-ge0c7