Răsfoiți Sursa

Create random passwords

oz123 10 ani în urmă
părinte
comite
ffbfeb999e
3 a modificat fișierele cu 49 adăugiri și 56 ștergeri
  1. 18 2
      pwman/tests/test_crypto_engine.py
  2. 3 3
      pwman/ui/baseui.py
  3. 28 51
      pwman/ui/tools.py

+ 18 - 2
pwman/tests/test_crypto_engine.py

@@ -1,3 +1,21 @@
+# ============================================================================
+# This file is part of Pwman3.
+#
+# Pwman3 is free software; you can redistribute iut and/or modify
+# it under the terms of the GNU General Public License, version 2
+# as published by the Free Software Foundation;
+#
+# Pwman3 is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Pwman3; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+# ============================================================================
+# Copyright (C) 2012, 2013, 2014 Oz Nahum Tiram <nahumoz@gmail.com>
+# ============================================================================
 import unittest
 import unittest
 import os
 import os
 import time
 import time
@@ -17,8 +35,6 @@ default_config = {'Global': {'umask': '0100', 'colors': 'yes',
                                                        "history")}
                                                        "history")}
                   }
                   }
 
 
-#config.set_defaults(default_config)
-
 give_key = lambda msg: "12345"
 give_key = lambda msg: "12345"
 give_wrong_key = lambda msg: "verywrongtkey"
 give_wrong_key = lambda msg: "verywrongtkey"
 
 

+ 3 - 3
pwman/ui/baseui.py

@@ -19,7 +19,6 @@
 from __future__ import print_function
 from __future__ import print_function
 import sys
 import sys
 import os
 import os
-import getpass
 import ast
 import ast
 import csv
 import csv
 import time
 import time
@@ -31,7 +30,8 @@ from pwman.ui import tools
 from pwman.util.crypto_engine import CryptoEngine
 from pwman.util.crypto_engine import CryptoEngine
 from pwman.util.crypto_engine import zerome
 from pwman.util.crypto_engine import zerome
 from pwman.ui.tools import CliMenuItem
 from pwman.ui.tools import CliMenuItem
-from pwman.ui.tools import CMDLoop
+from pwman.ui.tools import CMDLoop, get_or_create_pass
+
 
 
 if sys.version_info.major > 2:  # pragma: no cover
 if sys.version_info.major > 2:  # pragma: no cover
     raw_input = input
     raw_input = input
@@ -422,7 +422,7 @@ class BaseCommands(HelpUIMixin, AliasesMixin):
     def _get_secret(self):
     def _get_secret(self):
         # TODO: enable old functionallity, with password generator.
         # TODO: enable old functionallity, with password generator.
         if sys.stdin.isatty():  # pragma: no cover
         if sys.stdin.isatty():  # pragma: no cover
-            p = getpass.getpass()
+            p = get_or_create_pass()
         else:
         else:
             p = sys.stdin.readline().rstrip()
             p = sys.stdin.readline().rstrip()
         return p
         return p

+ 28 - 51
pwman/ui/tools.py

@@ -28,8 +28,8 @@ import shlex
 import platform
 import platform
 import colorama
 import colorama
 import os
 import os
-#from pwman.util.config import get_pass_conf
 from pwman.util.callback import Callback
 from pwman.util.callback import Callback
+from pwman.util.crypto_engine import generate_password
 
 
 
 
 if sys.version_info.major > 2:  # pragma: no cover
 if sys.version_info.major > 2:  # pragma: no cover
@@ -128,55 +128,6 @@ def open_url(link, macosx=False):  # pragma: no cover
         print("Executing open_url failed with:\n", e)
         print("Executing open_url failed with:\n", e)
 
 
 
 
-def get_password(question, config):
-    # TODO: enable old functionallity, with password generator.
-    if sys.stdin.isatty():  # pragma: no cover
-        p = getpass.getpass()
-    else:
-        p = sys.stdin.readline().rstrip()
-    return p
-
-
-def getpassword(question, argsgiven=None,
-                width=_defaultwidth, echo=False,
-                reader=getpass.getpass, numerics=False, leetify=False,
-                symbols=False, special_signs=False,
-                length=None, config=None):  # pragma: no cover
-    if argsgiven == 1 or length:
-        while not length:
-            try:
-                default_length = config.get_value(
-                    'Generator', 'default_pw_length') or '8'
-                length = getinput(
-                    "Password length (default %s): " % default_length,
-                    default=default_length)
-                length = int(length)
-            except ValueError:
-                print("please enter a proper integer")
-
-        #password, dumpme = generator.generate_password(
-        #    length, length, True, symbols=leetify, numerics=numerics,
-        #    special_chars=special_signs)
-        #print ("New password: %s" % (password))
-        #return password
-    # no args given
-    while True:
-        a1 = reader(question.ljust(width))
-        if not a1:
-            return getpassword(
-                '', argsgiven=1, width=width, echo=echo, reader=reader,
-                numerics=numerics, leetify=leetify, symbols=symbols,
-                special_signs=special_signs, length=length, config=config)
-        a2 = reader("[Repeat] %s" % (question.ljust(width)))
-        if a1 == a2:
-            if leetify:
-                pass  # return generator.leetify(a1)
-            else:
-                return a1
-        else:
-            print ("Passwords don't match. Try again.")
-
-
 def get_terminal_size():
 def get_terminal_size():
     """ getTerminalSize()
     """ getTerminalSize()
      - get width and height of console
      - get width and height of console
@@ -300,6 +251,32 @@ def getinput(question, default="", reader=raw_input,
         return reader()
         return reader()
 
 
 
 
+def get_or_create_pass():
+
+    p = getpass.getpass(prompt='Password (leave empty to create one):')
+    if not p:
+        while True:
+            try:
+                print("Password length (default: 8):", end="")
+                sys.stdout.flush()
+                l = sys.stdin.readline().strip()
+                l = int(l) if l else 8
+                break
+            except ValueError:
+                print("You did not enter an integer...")
+        p = generate_password(l)
+    return p
+
+
+def _get_secret():
+    # TODO: enable old functionallity, with password generator.
+    if sys.stdin.isatty():  # pragma: no cover
+        p = get_or_create_pass()
+    else:
+        p = sys.stdin.readline().rstrip()
+        return p
+
+
 class CMDLoop(object):  # pragma: no cover
 class CMDLoop(object):  # pragma: no cover
 
 
     """
     """
@@ -337,7 +314,7 @@ class CMDLoop(object):  # pragma: no cover
                     self.items[0].getter = new_node.username
                     self.items[0].getter = new_node.username
                     self.items[0].setter = new_node.username
                     self.items[0].setter = new_node.username
                 elif selection == 1:  # for password
                 elif selection == 1:  # for password
-                    new_node.password = get_password("Password:", self.config)
+                    new_node.password = _get_secret()
                     self.items[1].getter = new_node.password
                     self.items[1].getter = new_node.password
                     self.items[1].setter = new_node.password
                     self.items[1].setter = new_node.password
                 elif selection == 2:
                 elif selection == 2: