Browse Source

Clean crypto_engine

oz123 10 years ago
parent
commit
c982a85baf
2 changed files with 2 additions and 98 deletions
  1. 1 13
      pwman/tests/test_crypto_engine.py
  2. 1 85
      pwman/util/crypto_engine.py

+ 1 - 13
pwman/tests/test_crypto_engine.py

@@ -1,9 +1,7 @@
 import unittest
 import pwman.util.config as config
 import os
-from pwman.util.crypto_engine import (write_password, save_a_secret_message,
-                                      read_a_secret_message,
-                                      CryptoEngine, CryptoException)
+from pwman.util.crypto_engine import (CryptoEngine, CryptoException)
 import time
 
 # set cls_timout to negative number (e.g. -1) to disable
@@ -25,15 +23,6 @@ give_wrong_key = lambda msg: "verywrongtkey"
 
 class CryptoEngineTest(unittest.TestCase):
 
-    def test1_a_write_password(self):
-        write_password(reader=give_key)
-
-    def test2_b_save_secret(self):
-        save_a_secret_message(reader=give_key)
-
-    def test3_c_read_secret(self):
-        read_a_secret_message(reader=give_key)
-
     def test4_d_get_crypto(self):
         ce = CryptoEngine.get()
 
@@ -80,4 +69,3 @@ class CryptoEngineTest(unittest.TestCase):
         secret = ce.encrypt("topsecret")
         decrypt = ce.decrypt(secret)
         self.assertEqual(decrypt, "topsecret")
-

+ 1 - 85
pwman/util/crypto_engine.py

@@ -55,29 +55,6 @@ def authenticate(password, salt, digest):
     return binascii.hexlify(dig) == digest
 
 
-def write_password(reader=raw_input):
-    """
-    Write a secret password as a hash and the salt used for this hash
-    to a file
-    """
-    salt = base64.b64encode(os.urandom(32))
-    passwd = reader("Please type in the secret key:")
-    key = get_digest(passwd, salt)
-    f = open('passwords.txt', 'wt')
-    hpk = salt+'$6$'.encode('utf8')+binascii.hexlify(key)
-    f.write(hpk.decode('utf-8'))
-    f.close()
-
-
-def get_digest_from_file(filename):
-    """
-    Read a digested password and salt from the file
-    """
-    f = open(filename, 'rt')
-    salt, digest = f.readline().split('$6$')
-    return salt.encode('utf-8'), digest.encode('utf-8')
-
-
 def get_cipher(password, salt):
     """
     Create a chiper object from a hashed password
@@ -88,26 +65,6 @@ def get_cipher(password, salt):
     return chiper
 
 
-def cli_auth(reader=raw_input):
-    """
-    Read password from the user, if the password is correct,
-    finish the execution an return the password and salt which
-    are read from the file.
-    """
-    salt, digest = get_digest_from_file('passwords.txt')
-    tries = 0
-    while tries < 5:
-        password = reader("Please type in your master password:"
-                          ).encode('utf-8')
-        if authenticate(password, salt, digest):
-            return password, salt
-
-        print("You entered a wrong password...")
-        tries += 1
-
-    raise CryptoException("You entered wrong password 5 times..")
-
-
 def prepare_data(text, block_size):
     """
     prepare data before encryption so the lenght matches the expected
@@ -118,38 +75,6 @@ def prepare_data(text, block_size):
     return text.ljust(newdatasize)
 
 
-def save_a_secret_message(reader=raw_input):
-    """
-    PoC to show we can encrypt a message
-    """
-    secret_msg = """This is a very important message! Learn Cryptography!!!"""
-    # the secret message will be encrypted with the secret password found
-    # in the file
-    passwd, salt = cli_auth(reader=reader)
-    cipher = get_cipher(passwd, salt)
-    # explictly destroy password, so now there is no clear text reference
-    # to the input given by the user
-    del(passwd)
-    msg = EncodeAES(cipher, prepare_data(secret_msg, AES.block_size))
-    with open('secret.enc', 'wt') as s:
-        s.write(msg.decode())
-    print("The cipher message is:", msg.decode())
-
-
-def read_a_secret_message(reader=raw_input):
-    """
-    PoC to show we can decrypt a message
-    """
-    passwd, salt = cli_auth(reader)
-    cipher = get_cipher(passwd, salt)
-    del(passwd)
-    with open('secret.enc') as s:
-        msg = s.readline()
-        print("The decrypted secret message is:")
-        decoded = DecodeAES(cipher, msg)
-        print(decoded)
-
-
 class CryptoEngine(object):  # pagma: no cover
     _timeoutcount = 0
     _instance = None
@@ -173,10 +98,8 @@ class CryptoEngine(object):  # pagma: no cover
         except ValueError:
             timeout = -1
 
-        salt, digest = get_digest_from_file('passwords.txt')
-
         kwargs = {'algorithm': algo,
-                  'timeout': timeout, 'salt': salt, 'digest': digest}
+                  'timeout': timeout}
 
         if dbver >= 0.5:
             CryptoEngine._instance_new = CryptoEngine(**kwargs)
@@ -314,10 +237,3 @@ class CryptoEngine(object):  # pagma: no cover
         return _keycrypted
         """
         return self._salt + '$6$' + self._digest
-
-
-if __name__ == '__main__':  # pragma: no cover
-    if '-i' in sys.argv:
-        write_password()
-    save_a_secret_message()
-    read_a_secret_message()