-
Notifications
You must be signed in to change notification settings - Fork 173
homework_03 #1262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
homework_03 #1262
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,3 +15,107 @@ | |
| Введите пароль еще раз для проверки: 123 | ||
| Вы ввели правильный пароль | ||
| """ | ||
|
|
||
| import sqlite3 | ||
| import sys | ||
| from textwrap import dedent | ||
| from random import choice, randint | ||
| from hashlib import pbkdf2_hmac | ||
|
|
||
|
|
||
| # Класс для удобства работы с базой данных | ||
| class SQLite(): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SQLite- не самое удачное имя для СВОЕГО класса |
||
| def __init__(self, file='passwd.db'): | ||
| self.file = file | ||
|
|
||
| def __enter__(self): | ||
| self.conn = sqlite3.connect(self.file) | ||
| self.conn.row_factory = sqlite3.Row | ||
| return self.conn.cursor() | ||
|
|
||
| def __exit__(self, type, value, traceback): | ||
| self.conn.commit() | ||
| self.conn.close() | ||
|
|
||
|
|
||
| # Генерирует имя пользователя из букв, | ||
| # следит за чередованием гласных и согласных | ||
| def gen_login(maxlen=10): | ||
| SONANTS = 'aeiou' | ||
| CONSONANTS = 'bcdfghjklmnpqrstvwxyz' | ||
| i1 = randint(0, 1) | ||
| i2 = randint(i1 + 3, maxlen) | ||
| result = [] | ||
| for i in range(i1, i2+1): | ||
| arr = SONANTS if i % 2 == 0 else CONSONANTS | ||
| result.append(choice(arr)) | ||
| return "".join(result) | ||
|
|
||
|
|
||
| # Генерирует пароль из произвольных символов | ||
| def gen_passwd(n=5): | ||
| return "".join(chr(randint(33, 126)) for _ in range(n)) | ||
|
|
||
|
|
||
| # Генерирует таблицу (логин, пароль) | ||
| def gen_db(size=10): | ||
| return [ | ||
| (gen_login(), gen_passwd()) | ||
| for i in range(size) | ||
| ] | ||
|
|
||
|
|
||
| def hashit(login, passwd): | ||
| scrambled = "".join( | ||
| [x for i, c in enumerate(login) for x in (c, login[-i-1])]*3) | ||
| saltb = scrambled.encode() | ||
| passb = passwd.encode() | ||
| return pbkdf2_hmac( | ||
| hash_name='sha256', | ||
| password=passb, | ||
| salt=saltb, | ||
| iterations=100000 | ||
| ).hex() | ||
|
|
||
|
|
||
| def reset_db(): | ||
| db = gen_db() | ||
| with open("passwd.txt", "w") as fo: | ||
| for login, passwd in db: | ||
| fo.write(f"{login}\t{passwd}\n") | ||
| data = [ | ||
| (login, hashit(login, passwd)) | ||
| for login, passwd in db | ||
| ] | ||
|
|
||
| with SQLite() as cur: | ||
| try: | ||
| cur.execute(dedent("""\ | ||
| DELETE FROM users""")) | ||
| except sqlite3.OperationalError: | ||
| cur.execute(dedent("""\ | ||
| CREATE TABLE users | ||
| (login text, hash text)""")) | ||
|
|
||
| cur.executemany(dedent("""\ | ||
| INSERT INTO users | ||
| VALUES (?, ?)"""), data) | ||
|
|
||
|
|
||
| def check_passwd(login, passwd): | ||
| with SQLite() as cur: | ||
| cur.execute('SELECT * FROM users WHERE login=?', (login,)) | ||
| res = cur.fetchone() | ||
| if res is not None: | ||
| if hashit(login, passwd) == res[1]: | ||
| print("Успешный вход в систему") | ||
| return None | ||
| print("Неправильный логин или пароль") | ||
|
|
||
|
|
||
| if len(sys.argv) > 2: | ||
| check_passwd(sys.argv[1], sys.argv[2]) | ||
| elif len(sys.argv) > 1 and sys.argv[1] == "--reset": | ||
| reset_db() | ||
| else: | ||
| print("usage: task_2.py <uname> <passwd> OR task_2.py --reset") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,20 @@ | |
| р | ||
| а | ||
| """ | ||
|
|
||
|
|
||
| def count_substrings(s): | ||
| bag = set() | ||
| for i in range(len(s)): | ||
| for j in range(i+1, len(s)+1): | ||
| bag.add(hash(s[i:j])) | ||
| return len(bag)-1 | ||
|
|
||
|
|
||
| for s in ("bbbb", "baba", "bcde"): | ||
| print(f"{s} -> {count_substrings(s)}") | ||
|
|
||
| # ---output--- | ||
| # bbbb -> 3 | ||
| # baba -> 6 | ||
| # bcde -> 9 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. выполнено |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а как же операции поиска элементов и их получения?