diff mbox

[v4,04/16] osstest: introduce a helper to calculate the sha256 of a given file

Message ID 20170706144227.36580-5-roger.pau@citrix.com (mailing list archive)
State New, archived
Headers show

Commit Message

Roger Pau Monné July 6, 2017, 2:42 p.m. UTC
The helper is going to be used to calculate the sha256 of the FreeBSD
installer images.

Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>
---
Changes since v3:
 - New in this version.
---
 Osstest/TestSupport.pm | 13 +++++++++++++
 1 file changed, 13 insertions(+)

Comments

Ian Jackson July 6, 2017, 2:43 p.m. UTC | #1
Roger Pau Monne writes ("[PATCH v4 04/16] osstest: introduce a helper to calculate the sha256 of a given file"):
> The helper is going to be used to calculate the sha256 of the FreeBSD
> installer images.
> 
> Signed-off-by: Roger Pau Monné <roger.pau@citrix.com>

Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
diff mbox

Patch

diff --git a/Osstest/TestSupport.pm b/Osstest/TestSupport.pm
index e30e5759..12ebc32d 100644
--- a/Osstest/TestSupport.pm
+++ b/Osstest/TestSupport.pm
@@ -32,6 +32,7 @@  use File::Copy;
 use File::Basename;
 use IO::Handle;
 use Carp;
+use Digest::SHA;
 
 BEGIN {
     use Exporter ();
@@ -132,6 +133,7 @@  BEGIN {
                       guest_editconfig_nocd
                       host_install_postboot_complete
                       target_core_dump_setup
+                      sha256file
                       );
     %EXPORT_TAGS = ( );
 
@@ -2769,4 +2771,15 @@  sub target_https_mitm_proxy_setup ($) {
     target_cmd_root($ho, 'update-ca-certificates', 300);
 }
 
+sub sha256file ($;$) {
+    my ($file,$truncate) = @_;
+    my $sha = Digest::SHA->new(256);
+    my $digest;
+
+    $sha->addfile($file);
+    $digest = $sha->hexdigest;
+
+    return $truncate ? substr($digest, 0, $truncate) : $digest;
+}
+
 1;