From fcb1af1b69f8c491824aaa589c7693fa6e731e0b Mon Sep 17 00:00:00 2001
From: "Feng, YunhuaX" <yunhuax.feng@intel.com>
Date: Wed, 8 Aug 2018 14:56:55 +0800
Subject: [PATCH] BaseTools: Use hashlib instead of md5

Use from hashlib import md5 instead of import md5
due to md5 deprecated in python3

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
---
 .../Source/Python/UPT/GenMetaFile/GenDecFile.py      |  4 ++--
 .../Source/Python/UPT/GenMetaFile/GenInfFile.py      |  4 ++--
 BaseTools/Source/Python/UPT/InstallPkg.py            | 12 ++++++------
 BaseTools/Source/Python/UPT/MkPkg.py                 |  4 ++--
 BaseTools/Source/Python/UPT/RmPkg.py                 |  4 ++--
 BaseTools/Source/Python/Workspace/MetaFileParser.py  |  6 +++---
 6 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/BaseTools/Source/Python/UPT/GenMetaFile/GenDecFile.py b/BaseTools/Source/Python/UPT/GenMetaFile/GenDecFile.py
index 0ba07ccebc..e2dd6a02dc 100644
--- a/BaseTools/Source/Python/UPT/GenMetaFile/GenDecFile.py
+++ b/BaseTools/Source/Python/UPT/GenMetaFile/GenDecFile.py
@@ -19,7 +19,7 @@ GenDEC
 import os
 import stat
 import codecs
-import md5
+from hashlib import md5
 from Core.FileHook import __FileHookOpen__
 from Library.Parsing import GenSection
 from Library.CommentGenerating import GenHeaderCommentSection
@@ -646,7 +646,7 @@ def GenPackageUNIEncodeFile(PackageObject, UniFileHeader = '', Encoding=TAB_ENCO
     File = codecs.open(ContainerFile, 'w', Encoding)
     File.write(u'\uFEFF' + Content)
     File.stream.close()
-    Md5Sigature = md5.new(__FileHookOpen__(str(ContainerFile), 'rb').read())
+    Md5Sigature = md5(__FileHookOpen__(str(ContainerFile), 'rb').read())
     Md5Sum = Md5Sigature.hexdigest()
     if (ContainerFile, Md5Sum) not in PackageObject.FileList:
         PackageObject.FileList.append((ContainerFile, Md5Sum))
diff --git a/BaseTools/Source/Python/UPT/GenMetaFile/GenInfFile.py b/BaseTools/Source/Python/UPT/GenMetaFile/GenInfFile.py
index b62c705137..c2a240a884 100644
--- a/BaseTools/Source/Python/UPT/GenMetaFile/GenInfFile.py
+++ b/BaseTools/Source/Python/UPT/GenMetaFile/GenInfFile.py
@@ -18,7 +18,7 @@ GenInf
 import os
 import stat
 import codecs
-import md5
+from hashlib import md5
 from Core.FileHook import __FileHookOpen__
 from Library.StringUtils import GetSplitValueList
 from Library.Parsing import GenSection
@@ -255,7 +255,7 @@ def GenModuleUNIEncodeFile(ModuleObject, UniFileHeader='', Encoding=DT.TAB_ENCOD
         File = codecs.open(ContainerFile, 'wb', Encoding)
         File.write(u'\uFEFF' + Content)
         File.stream.close()
-    Md5Sigature = md5.new(__FileHookOpen__(str(ContainerFile), 'rb').read())
+    Md5Sigature = md5(__FileHookOpen__(str(ContainerFile), 'rb').read())
     Md5Sum = Md5Sigature.hexdigest()
     if (ContainerFile, Md5Sum) not in ModuleObject.FileList:
         ModuleObject.FileList.append((ContainerFile, Md5Sum))
diff --git a/BaseTools/Source/Python/UPT/InstallPkg.py b/BaseTools/Source/Python/UPT/InstallPkg.py
index 3573886d5a..c553d707fd 100644
--- a/BaseTools/Source/Python/UPT/InstallPkg.py
+++ b/BaseTools/Source/Python/UPT/InstallPkg.py
@@ -23,7 +23,7 @@ from os import chmod
 from os import SEEK_SET
 from os import SEEK_END
 import stat
-import md5
+from hashlib import md5
 import copy
 from sys import stdin
 from sys import platform
@@ -176,7 +176,7 @@ def UnZipDp(WorkspaceDir, DpPkgFileName, Index=1):
     # verify MD5 signature when existed
     #
     if DistPkg.Header.Signature != '':
-        Md5Sigature = md5.new(__FileHookOpen__(ContentFile, 'rb').read())
+        Md5Sigature = md5(__FileHookOpen__(ContentFile, 'rb').read())
         if DistPkg.Header.Signature != Md5Sigature.hexdigest():
             ContentZipFile.Close()
             Logger.Error("InstallPkg", FILE_CHECKSUM_FAILURE,
@@ -215,7 +215,7 @@ def GetPackageList(DistPkg, Dep, WorkspaceDir, Options, ContentZipFile, ModuleLi
     #
     for Package in PackageList:
         FilePath = PackageToDec(Package, DistPkg.Header)
-        Md5Sigature = md5.new(__FileHookOpen__(str(FilePath), 'rb').read())
+        Md5Sigature = md5(__FileHookOpen__(str(FilePath), 'rb').read())
         Md5Sum = Md5Sigature.hexdigest()
         if (FilePath, Md5Sum) not in Package.FileList:
             Package.FileList.append((FilePath, Md5Sum))
@@ -275,7 +275,7 @@ def GetModuleList(DistPkg, Dep, WorkspaceDir, ContentZipFile, ModuleList):
     for (Module, Package) in ModuleList:
         CheckCNameInModuleRedefined(Module, DistPkg)
         FilePath = ModuleToInf(Module, Package, DistPkg.Header)
-        Md5Sigature = md5.new(__FileHookOpen__(str(FilePath), 'rb').read())
+        Md5Sigature = md5(__FileHookOpen__(str(FilePath), 'rb').read())
         Md5Sum = Md5Sigature.hexdigest()
         if Package:
             if (FilePath, Md5Sum) not in Package.FileList:
@@ -803,7 +803,7 @@ def InstallFile(ContentZipFile, FromFile, ToFile, ReadOnly, Executable=False):
         else:
             chmod(ToFile, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR | stat.S_IWGRP | stat.S_IWOTH)
 
-    Md5Sigature = md5.new(__FileHookOpen__(str(ToFile), 'rb').read())
+    Md5Sigature = md5(__FileHookOpen__(str(ToFile), 'rb').read())
     Md5Sum = Md5Sigature.hexdigest()
 
     return Md5Sum
@@ -876,7 +876,7 @@ def InstallPackageContent(FromPath, ToPath, Package, ContentZipFile, Dep,
             chmod(ToFile, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
         else:
             chmod(ToFile, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH|stat.S_IWUSR|stat.S_IWGRP|stat.S_IWOTH)
-        Md5Sigature = md5.new(__FileHookOpen__(str(ToFile), 'rb').read())
+        Md5Sigature = md5(__FileHookOpen__(str(ToFile), 'rb').read())
         Md5Sum = Md5Sigature.hexdigest()
         if (ToFile, Md5Sum) not in Package.FileList:
             Package.FileList.append((ToFile, Md5Sum))
diff --git a/BaseTools/Source/Python/UPT/MkPkg.py b/BaseTools/Source/Python/UPT/MkPkg.py
index 37942b8ed9..053d115547 100644
--- a/BaseTools/Source/Python/UPT/MkPkg.py
+++ b/BaseTools/Source/Python/UPT/MkPkg.py
@@ -27,7 +27,7 @@ from sys import stdin
 from sys import platform
 from traceback import format_exc
 from platform import python_version
-import md5
+from hashlib import md5
 from time import strftime
 from time import localtime
 from uuid import uuid4
@@ -196,7 +196,7 @@ def Main(Options = None):
         #
         # Add Md5Sigature
         #
-        DistPkg.Header.Signature = md5.new(open(str(ContentFile), 'rb').read()).hexdigest()
+        DistPkg.Header.Signature = md5(open(str(ContentFile), 'rb').read()).hexdigest()
         #
         # Add current Date
         #
diff --git a/BaseTools/Source/Python/UPT/RmPkg.py b/BaseTools/Source/Python/UPT/RmPkg.py
index 78469dc758..f1d141c736 100644
--- a/BaseTools/Source/Python/UPT/RmPkg.py
+++ b/BaseTools/Source/Python/UPT/RmPkg.py
@@ -23,7 +23,7 @@ import os.path
 from stat import S_IWUSR
 from traceback import format_exc
 from platform import python_version
-import md5
+from hashlib import md5
 from sys import stdin
 from sys import platform
 
@@ -242,7 +242,7 @@ def RemoveDist(Guid, Version, StoredDistFile, DataBase, WorkspaceDir, ForceRemov
                 #
                 # check whether modified by users
                 #
-                Md5Sigature = md5.new(open(str(Path), 'rb').read())
+                Md5Sigature = md5(open(str(Path), 'rb').read())
                 if Md5Sum != Md5Sigature.hexdigest():
                     Logger.Info(ST.MSG_CONFIRM_REMOVE2 % Path)
                     Input = stdin.readline()
diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTools/Source/Python/Workspace/MetaFileParser.py
index 2b1ab40439..7bfe0933a1 100644
--- a/BaseTools/Source/Python/Workspace/MetaFileParser.py
+++ b/BaseTools/Source/Python/Workspace/MetaFileParser.py
@@ -21,7 +21,7 @@ import Common.LongFilePathOs as os
 import re
 import time
 import copy
-import md5
+from hashlib import md5
 
 import Common.EdkLogger as EdkLogger
 import Common.GlobalData as GlobalData
@@ -1929,10 +1929,10 @@ class DecParser(MetaFileParser):
                     return
 
                 if self._include_flag:
-                    self._ValueList[1] = "<HeaderFiles>_" + md5.new(self._CurrentLine).hexdigest()
+                    self._ValueList[1] = "<HeaderFiles>_" + md5(self._CurrentLine).hexdigest()
                     self._ValueList[2] = self._CurrentLine
                 if self._package_flag and "}" != self._CurrentLine:
-                    self._ValueList[1] = "<Packages>_" + md5.new(self._CurrentLine).hexdigest()
+                    self._ValueList[1] = "<Packages>_" + md5(self._CurrentLine).hexdigest()
                     self._ValueList[2] = self._CurrentLine
                 if self._CurrentLine == "}":
                     self._package_flag = False