Skip to content

Commit 869f706

Browse files
committed
SUSE Hack Week 2023
1 parent a9c78d6 commit 869f706

File tree

11 files changed

+48
-57
lines changed

11 files changed

+48
-57
lines changed

encrypt-passwords/README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ This script can be ran interactively or silently, see the below to see how.
1111
### Getting Started
1212
To utilize this script in the command-line, please follow the below workflow:
1313

14-
(1) Clone the script into your environment.\
15-
(2) Make sure latest version of Python 3 is installed on the environment.\
16-
(3) Run the script interactively: `python3 encryptpw.py 1 -p <password>`.\
17-
(4) Run the script interactively: `python3 encryptpw.py 2`.
14+
1. Clone the script into your environment.
15+
2. Make sure latest version of Python 3 is installed on the environment.
16+
3. Run the script interactively: `python3 encryptpw.py 1 -p <password>`.
17+
4. Run the script interactively: `python3 encryptpw.py 2`.
1818

1919
Additionally, this can be ran from your Python IDE of choice such as PyCharm, IDLE, Visual Studio Code, etc.
2020

@@ -25,3 +25,5 @@ See below an image of the script in action:
2525
Included is `encryptpw_unittest.py` to check for correctness. The tests provide few, basic tests to verify the addition, subtraction, multiplication and division functions are working properly. To run in the command-line, run the following command:
2626

2727
`python3 -m unittest encryptpw_unittest.py`
28+
29+
![Unit-test](https://github.com/markusewalker/Misc-Python-Scripts/blob/main/encrypt-passwords/test.jpg)

encrypt-passwords/example.jpg

-77 KB
Loading

encrypt-passwords/src/encryptpw.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
#!/usr/bin/env python3
22
#
33
# Authored By: Markus Walker
4-
# Date Modified: 3/4/22
4+
# Date Modified: 1/31/23
55
#
66
# Description: Encrypts passwords using the Fernet cryptography method.
77

88
import argparse, getpass
99
from cryptography.fernet import Fernet
1010

11-
# Function to encrypt passwords using Fernet cryptography.
1211
def encryption(password):
13-
# Instance the Fernet class with the key variable...
1412
key = Fernet.generate_key()
1513
fernet = Fernet(key)
1614

17-
# Encoding to byte string before we can actually encrypt...
1815
new_pass = fernet.encrypt(password.encode())
1916

2017
return new_pass
2118

19+
def output(password, result):
20+
with open("pass.txt", "w") as text_file:
21+
print(f"{password}", file=text_file)
22+
print(f"{result}", file=text_file)
23+
text_file.close()
24+
25+
print("Key and encrypted password have been saved to pass.txt")
26+
2227
def main():
2328
parser = argparse.ArgumentParser(description="Encrypts user-inputted passwords using Fernet cryptography")
2429
parser.add_argument("mode", metavar="mode", type=int, help="runs the script in interactive or silent mode: 1 for silent, 2 for interactive. If silent, run required command-line flags")
2530
parser.add_argument("-p", "--password", help="password that will be encrypted")
2631

2732
args = parser.parse_args()
2833

29-
# If user chooses option 1, run script silently. Else, if user chooses option 2, run script interactively.
3034
if args.mode == 1:
31-
# Print out the initial password along with the encrypted password.
3235
result = encryption(args.password)
33-
print(f"\nKey: {args.password}")
34-
print(f"\nEncrypted Password: {result}\n")
36+
output(args.password, result)
3537
elif args.mode == 2:
3638
welcome = """
3739
Welcome to the Encrypt Passwords tool! This will take user-inputted passwords and
3840
encrypt them using the Fernet cryptography method.
3941
"""
4042
print(welcome)
4143

42-
# Once password is received, print the key along with the encrypted password.
4344
password = getpass.getpass(prompt="Please enter a password that will be used as a key to encrypt: ")
4445
result = encryption(password)
45-
print(f"\nKey: {password}")
46-
print(f"\nEncrypted Password: {result}\n")
46+
output(password, result)
4747

4848
if __name__ == "__main__":
4949
main()

encrypt-passwords/src/encryptpw_unittest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python3
22
#
33
# Authored By: Markus Walker
4-
# Date Modified: 3/4/22
4+
# Date Modified: 1/31/23
55
#
66
# Description: Unit test for the encrypt-passwords.py script.
77
import unittest
@@ -12,6 +12,8 @@ def test_encryption(self):
1212
self.assertNotEqual(encryption("test"), "b'gAAAAABiItEqE3Kt4bPp0i-7wRP1_4eOlHa3fySmR0sHf7sOrLSBm8_iOVXJAwkfCW1dkTYiXZAcsP5KIxbJsACoCw0eU_sik35WcrJE6MJDujKXSdx939I='")
1313
self.assertNotEqual(encryption("hello"), "b'gAAAAABiItEqE3Kt4bPp0i-7wRP1_4eOlHa3fySmR0sHf7sOrLSBm8_iOVXJAwkfCW1dkTYiXZAcsP5KIxbJsACoCw0eU_sik35WcrJE6MJDujKXSdx939I='")
1414
self.assertNotEqual(encryption("yoyo"), "b'gAAAAABiItEqE3Kt4bPp0i-7wRP1_4eOlHa3fySmR0sHf7sOrLSBm8_iOVXJAwkfCW1dkTYiXZAcsP5KIxbJsACoCw0eU_sik35WcrJE6MJDujKXSdx939I='")
15-
15+
def test_output(self):
16+
self.assertFalse(output("test", "b'gAAAAABiItEqE3Kt4bPp0i-7wRP1_4eOlHa3fySmR0sHf7sOrLSBm8_iOVXJAwkfCW1dkTYiXZAcsP5KIxbJsACoCw0eU_sik35WcrJE6MJDujKXSdx939I='"))
17+
1618
if __name__ == "__main__":
1719
unittest.main()

encrypt-passwords/test.jpg

45.3 KB
Loading

python-calculator/README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ This script can be ran interactively or silently using command-line arguments. T
88
### Getting Started
99
To utilize this script in the command-line, please follow the below workflow:
1010

11-
(1) Clone the script into your environment.\
12-
(2) Make sure latest version of Python 3 is installed on the environment.\
13-
(3) Run the script silently: **python3 calculator.py 1 -n1 <number> -n2 <number> -o <operation>**.\
14-
(4) Run the script interactively: **python3 calculator.py 2**
11+
1. Clone the script into your environment.
12+
2. Make sure latest version of Python 3 is installed on the environment.
13+
3. Run the script silently: `python3 calculator.py 1 -n1 <number> -n2 <number> -o <operation>`.
14+
4. Run the script interactively: `python3 calculator.py 2`
1515

1616
Additionally, this can be ran from your Python IDE of choice such as PyCharm, IDLE, Visual Studio Code, etc.
1717

1818
See below an image of the script in action:
19-
![Image of Calculator](https://github.com/markusewalker/Misc-Python-Scripts/blob/main/python-calculator/calculator.png)
19+
![Image of Calculator](https://github.com/markusewalker/Misc-Python-Scripts/blob/main/python-calculator/example.jpg)
2020

21-
### Testing
21+
### Unit Testing
2222
Included is `calculator_unittest.py` to check for correctness. The tests provide few, basic tests to verify the addition, subtraction, multiplication and division functions are working properly. To run in the command-line, run the following command:
2323

24-
**python3 -m unittest calculator_unittest.py**
24+
`python3 -m unittest calculator_unittest.py`
25+
26+
![Unit-test](https://github.com/markusewalker/Misc-Python-Scripts/blob/main/python-calculator/test.jpg)

python-calculator/calculator.png

-95.1 KB
Binary file not shown.

python-calculator/example.jpg

167 KB
Loading

python-calculator/calculator.py renamed to python-calculator/src/calculator.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
#!/usr/bin/env python3
22

33
# Authored By: Markus Walker
4-
# Date Modified: 2/13/22
4+
# Date Modified: 1/31/23
55
#
6-
# Descrption: Calculator written in Python 3. It provides the following
7-
# operations:
8-
# 1. Addition
9-
# 2. Subtraction
10-
# 3. Multiplication
11-
# 4. Division
12-
# 5. Remainder
13-
# 6. Squared
14-
# 7. Cubed
6+
# Descrption: Calculator written in Python 3. It provides the following operations:
7+
# addition, subtraction, multiplication, division, remainder, squared, cubed.
8+
159
import argparse
1610

1711
def addition(x, y):
@@ -35,10 +29,9 @@ def squared(x, y):
3529
def cubed(x, y):
3630
return (x ** y) * x
3731

38-
# Function to interactively run the script if the user chose option 2.
3932
def interactive():
4033
welcome = """
41-
Welcome to the Python Calculator! Please find the below supported operations:
34+
Welcome to the Python Calculator! Please find the below supported operations:
4235
4336
1. Addition
4437
2. Subtraction
@@ -48,15 +41,13 @@ def interactive():
4841
6. Squared
4942
7. Cubed
5043
51-
To keep it simple, this calculator takes only two numbers as input.
52-
"""
44+
To keep it simple, this calculator takes only two numbers as input.
45+
"""
5346
print(welcome)
5447

55-
# While loop that will check the user input for the calculator and reference appropriate function.
5648
while True:
5749
choice = input("Please enter the operation you wish to perform: ")
5850

59-
# Must choose a number in the range of 1-6....if not, it will throw an error.
6051
if choice in ("1", "2", "3", "4", "5", "6", "7"):
6152
num1 = float(input("Please enter the first number: "))
6253
num2 = float(input("Please enter the second number: "))
@@ -76,33 +67,30 @@ def interactive():
7667
elif choice == "7":
7768
print(f"{num1} ** {num2} * {num1} =", cubed(num1, num2))
7869

79-
# Ask if the user wishes to continue performing calculations; end if they do not.
80-
next_choice = input("Would you like to continue? Enter 'yes' or 'no': ")
81-
if next_choice == "yes":
70+
next_choice = input("\nWould you like to continue? Enter 'yes'|'y' or 'no'|'n': ")
71+
while next_choice not in ("yes", "y", "no", "n"):
72+
next_choice = input("Please enter 'yes'|'y' or 'no'|'n': ")
73+
if next_choice == "yes" or next_choice == "y":
8274
continue
83-
elif next_choice == "no":
84-
break
85-
else:
86-
print("Huh? Ending the program...")
75+
elif next_choice == "no" or next_choice == "n":
76+
print("Thank you for using the Python Calculator!")
8777
break
8878
else:
89-
print("ERROR. You must choose a number in the range 1-7. Please try again...")
79+
print("You must choose a number in the range 1-7. Please try again...\n")
80+
continue
9081

9182
def main():
9283
parser = argparse.ArgumentParser(description="Performs the following operations: addition, subtraction, multiplication, division, remainder, squared, cubed.")
9384
parser.add_argument('mode', metavar='mode', type=int, help="runs the script in interactive or silent mode: 1 for silent, 2 for interactive. If silent, run the other command-line flags as well.")
9485
parser.add_argument("-n1", "--number1", type=int, help="first number to input")
9586
parser.add_argument("-n2", "--number2", type=int, help="second number to input")
96-
parser.add_argument("-o", "--operation", help="operation to perform: 'add', 'sub', 'mult', 'div', 'rem', 'squ', 'cube'", action="store_true")
87+
parser.add_argument("-o", "--operation", type=str, help="operation to perform: add, sub, mult, div, rem, squ, cub")
9788

98-
# Read in the above arguments and assign num1 and num2 to the user inputted numbers.
9989
args = parser.parse_args()
10090
num1 = args.number1
10191
num2 = args.number2
10292

103-
# If user chooses option 1, then run in silent mode.
10493
if args.mode == 1:
105-
# Depending on operation chose, perform the respective calculation with the two numbers.
10694
if args.operation == "add":
10795
print(f"{num1} + {num2} =", addition(num1, num2))
10896
elif args.operation == "sub":
@@ -117,7 +105,6 @@ def main():
117105
print(f"{num1} ** {num2} =", squared(num1, num2))
118106
elif args.operation == "cub":
119107
print(f"{num1} ** {num2} * {num1} =", cubed(num1, num2))
120-
# If user chooses option 2, then run in interactive mode.
121108
elif args.mode == 2:
122109
interactive()
123110
else:

python-calculator/calculator_unittest.py renamed to python-calculator/src/calculator_unittest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#!/usr/bin/env python3
22

33
# Authored By: Markus Walker
4-
# Date Modified: 1/30/22
4+
# Date Modified: 1/31/23
55
#
6-
# Descrption: Unit test for the calculator.py script. Several unit tests
7-
# against the addition, subtraction, multiplication and division functions
8-
# of the calculator.py script.
6+
# Descrption: Unit test for the calculator.py script.
97
from calculator import *
108
import unittest
119

python-calculator/test.jpg

29.5 KB
Loading

0 commit comments

Comments
 (0)