From 1a11874c57156e576620dd396b4357ec9bab2cc4 Mon Sep 17 00:00:00 2001
From: sun_hai_10 <sunhai10@huawei.com>
Date: Tue, 29 Nov 2022 09:34:09 +0800
Subject: [PATCH] add SM3 with tui

---
 pyanaconda/ui/tui/spokes/root_password.py | 81 ++++++++++++++++++++---
 pyanaconda/ui/tui/tuiobject.py            |  7 +-
 2 files changed, 77 insertions(+), 11 deletions(-)

diff --git a/pyanaconda/ui/tui/spokes/root_password.py b/pyanaconda/ui/tui/spokes/root_password.py
index 3c5ba16..dfaca4e 100644
--- a/pyanaconda/ui/tui/spokes/root_password.py
+++ b/pyanaconda/ui/tui/spokes/root_password.py
@@ -26,7 +26,11 @@ from pyanaconda.core.i18n import N_, _
 from pyanaconda.modules.common.constants.services import USERS
 from pyanaconda.core.constants import PASSWORD_POLICY_ROOT
 
-from simpleline.render.widgets import TextWidget
+from simpleline.render.containers import ListColumnContainer
+from simpleline.render.prompt import Prompt
+from simpleline.render.screen import InputState
+from simpleline.render.screen_handler import ScreenHandler
+from simpleline.render.widgets import TextWidget, CheckboxWidget
 
 
 class PasswordSpoke(FirstbootSpokeMixIn, NormalTUISpoke):
@@ -50,20 +54,18 @@ class PasswordSpoke(FirstbootSpokeMixIn, NormalTUISpoke):
         return FirstbootSpokeMixIn.should_run(environment, data)
 
     def __init__(self, data, storage, payload):
-        NormalTUISpoke.__init__(self, data, storage, payload)
-        self.initialize_start()
+        super().__init__(data, storage, payload)
         self.title = N_("Root password")
-        self.input_required = False
-
-        self._password = None
-
         self._users_module = USERS.get_proxy()
-        self.initialize_done()
+        self._sm3_config = False
+    
+    def _set_sm3_config(self, args):
+        self._sm3_config = not self._sm3_config
 
     @property
     def completed(self):
         return self._users_module.IsRootPasswordSet
-
+    
     @property
     def showable(self):
         return can_modify_root_configuration(self._users_module)
@@ -77,6 +79,59 @@ class PasswordSpoke(FirstbootSpokeMixIn, NormalTUISpoke):
     def status(self):
         return get_root_configuration_status(self._users_module)
 
+    def initialize(self):
+        self.initialize_start()
+        NormalTUISpoke.initialize(self)
+        self.initialize_done()
+
+    def refresh(self, args=None):
+        """ Refresh screen. """
+        NormalTUISpoke.refresh(self, args)
+
+        self._container = ListColumnContainer(1)
+
+        msg = _("SM3 encrypt")
+        sm3_check = CheckboxWidget(title=msg, completed=self._sm3_config)
+        self._container.add(sm3_check, self._set_sm3_config)
+
+        self.window.add_with_separator(self._container)
+
+    def input(self, args, key):
+        """Handle the user input."""
+        if self._container.process_user_input(key):
+            return InputState.PROCESSED_AND_REDRAW
+
+        if key.lower() == Prompt.CONTINUE:
+            spoke = RootPasswordSpoke(
+                self.data,
+                self.storage,
+                self.payload,
+                self._sm3_config,
+            )
+            ScreenHandler.push_screen_modal(spoke)
+            return InputState.PROCESSED_AND_CLOSE
+
+        return super().input(args, key)
+
+
+class RootPasswordSpoke(NormalTUISpoke):
+    """
+       .. inheritance-diagram:: PasswordSpoke
+          :parts: 3
+    """
+
+    def __init__(self, data, storage, payload, sm3_config):
+        NormalTUISpoke.__init__(self, data, storage, payload)
+        self.initialize_start()
+        self.title = N_("Root password")
+        self.input_required = False
+
+        self._password = None
+        self._sm3_config = sm3_config
+
+        self._users_module = USERS.get_proxy()
+        self.initialize_done()
+
     def refresh(self, args=None):
         NormalTUISpoke.refresh(self, args)
 
@@ -85,10 +140,15 @@ class PasswordSpoke(FirstbootSpokeMixIn, NormalTUISpoke):
 
     def show_all(self):
         super().show_all()
+        if self._sm3_config:
+            algo = "sm3"
+        else:
+            algo = None
 
         password_dialog = PasswordDialog(
             title=_("Password"),
-            policy_name=PASSWORD_POLICY_ROOT
+            policy_name=PASSWORD_POLICY_ROOT,
+            func_args=(algo,)
         )
         password_dialog.no_separator = True
         self._password = password_dialog.run()
@@ -101,6 +161,7 @@ class PasswordSpoke(FirstbootSpokeMixIn, NormalTUISpoke):
             self.close()
 
     def apply(self):
+        
         self._users_module.SetCryptedRootPassword(self._password)
         if self._password:
             self._users_module.SetRootAccountLocked(False)
diff --git a/pyanaconda/ui/tui/tuiobject.py b/pyanaconda/ui/tui/tuiobject.py
index 6cb439b..c642931 100644
--- a/pyanaconda/ui/tui/tuiobject.py
+++ b/pyanaconda/ui/tui/tuiobject.py
@@ -209,12 +209,14 @@ class PasswordDialog(Dialog):
                  report_func=reporting_callback,
                  process_func=crypt_password,
                  secret_type=constants.SecretType.PASSWORD,
+                 func_args=None,
                  message=None):
         super().__init__(title, report_func=report_func)
         self._no_separator = False
         self._policy = input_checking.get_policy(policy_name)
         self._secret_type = secret_type
         self._process_password = process_func
+        self._func_args = func_args
         self._dialog_message = message
 
     def run(self):
@@ -292,7 +294,10 @@ class PasswordDialog(Dialog):
         if any(char not in constants.PW_ASCII_CHARS for char in password):
             self._report(_(constants.SECRET_ASCII[self._secret_type]))
 
-        return self._process_password(password)
+        if self._func_args == None:
+            return self._process_password(password)
+        else:
+            return self._process_password(password, *self._func_args)
 
     def _report(self, message):
         if self._report_func:
-- 
2.28.0.windows.1

