[pve-devel] [RFC common] Tools: add file_md5sum and file_sha512sum

Thomas Lamprecht t.lamprecht at proxmox.com
Wed Jan 17 11:40:07 CET 2018


CC: Wolfgang Bumiller <w.bumiller at proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

I'd use such a function to version OVMF Code images and thought that
our Tools package could be a fitting place for it.
If the base idea is OK we could reuse this also in
PVE::API2::Nodes::Nodeinfo->apl_download where we verify the sum of
our CT appliances.

Kept it short (i.e., no formal parameter declaration) as, IMO, its very
clear from context what the method signatures are and would be only
more noisy else - but that's obviously up to debate :)

 src/PVE/Tools.pm | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index d5373a4..ad2537c 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -20,6 +20,7 @@ use base 'Exporter';
 use URI::Escape;
 use Encode;
 use Digest::SHA;
+use Digest::MD5;
 use JSON;
 use Text::ParseWords;
 use String::ShellQuote;
@@ -299,6 +300,25 @@ sub safe_read_from {
     return $input;
 }
 
+my $file_digest = sub {
+    my ($digest_ctx, $fn) = @_;
+
+    return undef if !(defined($fn) && -f $fn);
+
+    open (my $fh, '<', $fn);
+    binmode($fh);
+
+    return $digest_ctx->addfile($fh)->hexdigest;
+};
+
+sub file_md5sum {
+    return $file_digest->(Digest::MD5->new(), $_[0]);
+}
+
+sub file_sha512sum {
+    return $file_digest->(Digest::SHA->new(512), $_[0]);
+}
+
 # The $cmd parameter can be:
 #  -) a string
 #    This is generally executed by passing it to the shell with the -c option.
-- 
2.11.0





More information about the pve-devel mailing list