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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import string
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
def txt_to_list(filename):
|
| 6 |
+
with open(filename, 'r') as file:
|
| 7 |
+
lines = file.readlines()
|
| 8 |
+
# Strip whitespace and return
|
| 9 |
+
return [line.strip() for line in lines]
|
| 10 |
+
|
| 11 |
+
commonpasswords_list = txt_to_list('10-million-password-list-top-1000000.txt')
|
| 12 |
+
englishwords_list = txt_to_list('words.txt')
|
| 13 |
+
|
| 14 |
+
#Remove any single alphabet letter
|
| 15 |
+
commonpasswords_list = [item for item in commonpasswords_list if not (isinstance(item, str) and len(item) == 1 and item.isalpha())]
|
| 16 |
+
englishwords_list = [item for item in englishwords_list if not (isinstance(item, str) and len(item) == 1 and item.isalpha())]
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
#Remove '', which would cause error
|
| 20 |
+
englishwords_list.remove('')
|
| 21 |
+
#Double check whether it works
|
| 22 |
+
any(elem in commonpasswords_list for elem in list(string.ascii_lowercase))
|
| 23 |
+
any(elem in englishwords_list for elem in list(string.ascii_lowercase))
|
| 24 |
+
|
| 25 |
+
topwords_list = txt_to_list('top-1000-nouns.txt')
|
| 26 |
+
#Remove any single alphabet letter
|
| 27 |
+
|
| 28 |
+
topwords_list = [item for item in topwords_list if not (isinstance(item, str) and len(item) == 1 and item.isalpha())]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
#Remove '', which would cause error
|
| 32 |
+
#Double check whether it works
|
| 33 |
+
any(elem in topwords_list for elem in list(string.ascii_lowercase))
|
| 34 |
+
topwords_list[0] = 'time'
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
common_passwords = commonpasswords_list
|
| 38 |
+
dictionary_words = englishwords_list
|
| 39 |
+
|
| 40 |
+
def generate_password(length=12, use_uppercase=True, use_lowercase=True,
|
| 41 |
+
use_numbers=True, use_symbols=True, dictionary = True):
|
| 42 |
+
# Define the character sets
|
| 43 |
+
uppercase = string.ascii_uppercase
|
| 44 |
+
lowercase = string.ascii_lowercase
|
| 45 |
+
numbers = string.digits
|
| 46 |
+
symbols = "!@#$%^&*()-_+=[]{}|:;<>,.?/"
|
| 47 |
+
|
| 48 |
+
# Build the character pool based on the user's requirements
|
| 49 |
+
character_pool = ""
|
| 50 |
+
if use_uppercase:
|
| 51 |
+
character_pool += uppercase
|
| 52 |
+
if use_lowercase:
|
| 53 |
+
character_pool += lowercase
|
| 54 |
+
if use_numbers:
|
| 55 |
+
character_pool += numbers
|
| 56 |
+
if use_symbols:
|
| 57 |
+
character_pool += symbols
|
| 58 |
+
|
| 59 |
+
# Check if character pool is empty
|
| 60 |
+
if not character_pool:
|
| 61 |
+
raise ValueError("No character sets selected!")
|
| 62 |
+
|
| 63 |
+
# Generate the password
|
| 64 |
+
while True:
|
| 65 |
+
|
| 66 |
+
password = ''.join(random.choice(character_pool) for _ in range(length))
|
| 67 |
+
|
| 68 |
+
# Check if password contains any common English word
|
| 69 |
+
if not any(word in password.lower() for word in common_passwords):
|
| 70 |
+
if dictionary:
|
| 71 |
+
if not any(word in password.lower() for word in dictionary_words):
|
| 72 |
+
return password
|
| 73 |
+
break
|
| 74 |
+
return password
|
| 75 |
+
break
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# Load model directly
|
| 79 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 80 |
+
|
| 81 |
+
tokenizer = AutoTokenizer.from_pretrained("DunnBC22/codebert-base-Password_Strength_Classifier")
|
| 82 |
+
model = AutoModelForSequenceClassification.from_pretrained("DunnBC22/codebert-base-Password_Strength_Classifier")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# Put the model in evaluation mode
|
| 87 |
+
#The model classifies passwords as one of the following:0.Weak 1.Medium 2.Strong
|
| 88 |
+
|
| 89 |
+
model.eval()
|
| 90 |
+
|
| 91 |
+
# Input the password you generate, you could also manually input
|
| 92 |
+
text = generate_password(12)
|
| 93 |
+
#text = input()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# Tokenize input text
|
| 97 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)
|
| 98 |
+
|
| 99 |
+
# Get logits
|
| 100 |
+
with torch.no_grad():
|
| 101 |
+
logits = model(**inputs).logits
|
| 102 |
+
|
| 103 |
+
# Get predictions
|
| 104 |
+
predictions = torch.argmax(logits, dim=1)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def strength_indicator(value):
|
| 108 |
+
# Define a mapping of index to label
|
| 109 |
+
index_to_label = {
|
| 110 |
+
0: "Weak",
|
| 111 |
+
1: "Medium",
|
| 112 |
+
2: "Strong"
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
# Check if the input value is within the valid range
|
| 116 |
+
if value not in index_to_label:
|
| 117 |
+
raise ValueError("Input should be an integer ranging from 0 to 2.")
|
| 118 |
+
|
| 119 |
+
# Return the corresponding label
|
| 120 |
+
return index_to_label[value]
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
strength_indicator(predictions.item())
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
##This is just a python function and no user interface
|
| 127 |
+
|
| 128 |
+
def AssessYourPassword(manual=True):
|
| 129 |
+
model.eval()
|
| 130 |
+
|
| 131 |
+
# Input the password you generate, you could also manually input
|
| 132 |
+
if manual:
|
| 133 |
+
text = input()
|
| 134 |
+
else:
|
| 135 |
+
print('Please input the length of your password:')
|
| 136 |
+
n = input()
|
| 137 |
+
text = generate_password(int(n))
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# Tokenize input text
|
| 142 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)
|
| 143 |
+
|
| 144 |
+
# Get logits
|
| 145 |
+
with torch.no_grad():
|
| 146 |
+
logits = model(**inputs).logits
|
| 147 |
+
|
| 148 |
+
# Get predictions
|
| 149 |
+
predictions = torch.argmax(logits, dim=1)
|
| 150 |
+
strength = strength_indicator(predictions.item())
|
| 151 |
+
if not manual:
|
| 152 |
+
print(f'This is the generated password: {text}')
|
| 153 |
+
return print(f'Password Strength: {strength}')
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
import gradio as gr
|
| 161 |
+
import random
|
| 162 |
+
import string
|
| 163 |
+
import torch
|
| 164 |
+
|
| 165 |
+
model.eval()
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def generate_password(length=12, use_uppercase=True, use_lowercase=True,
|
| 169 |
+
use_numbers=True, use_symbols=True, dictionary=True, easy_mode=False):
|
| 170 |
+
if easy_mode:
|
| 171 |
+
numbers = "1234567890"
|
| 172 |
+
special_chars = "!@#$%^&*"
|
| 173 |
+
chosen_words = [random.choice(topwords_list) for _ in range(2)]
|
| 174 |
+
chosen_words = [word.capitalize() if random.choice([True, False]) else word for word in chosen_words]
|
| 175 |
+
chosen_numbers = "".join(random.choice(numbers) for _ in range(3))
|
| 176 |
+
chosen_special_char = random.choice(special_chars)
|
| 177 |
+
components = chosen_words + [chosen_numbers, chosen_special_char]
|
| 178 |
+
random.shuffle(components)
|
| 179 |
+
password = "".join(components)
|
| 180 |
+
else:
|
| 181 |
+
uppercase = string.ascii_uppercase
|
| 182 |
+
lowercase = string.ascii_lowercase
|
| 183 |
+
numbers = string.digits
|
| 184 |
+
symbols = "!@#$%^&*()-_+=[]{}|:;<>,.?/"
|
| 185 |
+
character_pool = ""
|
| 186 |
+
if use_uppercase:
|
| 187 |
+
character_pool += uppercase
|
| 188 |
+
if use_lowercase:
|
| 189 |
+
character_pool += lowercase
|
| 190 |
+
if use_numbers:
|
| 191 |
+
character_pool += numbers
|
| 192 |
+
if use_symbols:
|
| 193 |
+
character_pool += symbols
|
| 194 |
+
if not character_pool:
|
| 195 |
+
raise ValueError("No character sets selected!")
|
| 196 |
+
password = ''.join(random.choice(character_pool) for _ in range(length))
|
| 197 |
+
return password
|
| 198 |
+
|
| 199 |
+
def AssessYourPassword_UI_v2(option, length, use_uppercase, use_lowercase,
|
| 200 |
+
use_numbers, use_symbols, dictionary_allowed, text_input):
|
| 201 |
+
model.eval()
|
| 202 |
+
|
| 203 |
+
if option == "Manual":
|
| 204 |
+
text = text_input
|
| 205 |
+
elif option == "Easy Mode":
|
| 206 |
+
text = generate_password(easy_mode=True)
|
| 207 |
+
else:
|
| 208 |
+
text = generate_password(length, use_uppercase, use_lowercase,
|
| 209 |
+
use_numbers, use_symbols, not dictionary_allowed)
|
| 210 |
+
|
| 211 |
+
inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=512)
|
| 212 |
+
with torch.no_grad():
|
| 213 |
+
logits = model(**inputs).logits
|
| 214 |
+
predictions = torch.argmax(logits, dim=1)
|
| 215 |
+
strength = strength_indicator(predictions.item())
|
| 216 |
+
|
| 217 |
+
if option == "Manual":
|
| 218 |
+
output_password = "You provided: " + text
|
| 219 |
+
else:
|
| 220 |
+
output_password = "Generated: " + text
|
| 221 |
+
|
| 222 |
+
return output_password, f'Password Strength: {strength}'
|
| 223 |
+
|
| 224 |
+
iface_v2 = gr.Interface(
|
| 225 |
+
fn=AssessYourPassword_UI_v2,
|
| 226 |
+
inputs=[
|
| 227 |
+
gr.Radio(["Manual", "Generated", "Easy Mode"], label="Password Input Method"),
|
| 228 |
+
gr.Slider(minimum=6, maximum=24, default=12, step=1, label="Password Length"),
|
| 229 |
+
gr.Checkbox(label="Use Uppercase Letters", default=True),
|
| 230 |
+
gr.Checkbox(label="Use Lowercase Letters", default=True),
|
| 231 |
+
gr.Checkbox(label="Use Numbers", default=True),
|
| 232 |
+
gr.Checkbox(label="Use Symbols", default=True),
|
| 233 |
+
gr.Checkbox(label="Allow Dictionary Words", default=False),
|
| 234 |
+
gr.Textbox(default="Type password or length here", label="Input")
|
| 235 |
+
],
|
| 236 |
+
outputs=[
|
| 237 |
+
"text",
|
| 238 |
+
"text"
|
| 239 |
+
]
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
iface_v2.launch(share=True, debug=True)
|