コード例 #1
0
def change_key(login: str, master_key: bytes) -> None:
    try:
        checker1 = db.info(login)
        if checker1 is None:
            raise Error(
                "Error in main.change_key(): A user with this login " +
                "is not registered")
    except db.db_Error as e:
        raise Error(str(e))
    info: Tuple[str, bytes, str, bytes, bytes] = checker1

    encrypted_data: Dict[str, bytes] = {"ciphertext": info[3], "iv": info[4]}

    try:
        checker2: bytes = security.decrypt(encrypted_data, master_key)
    except security.security_Error as e:
        raise Error(str(e))
    old_key: object = checker2

    key: bytes = security.gen_key()

    try:
        checker4 = security.encrypt_new(key, master_key)
    except security.security_Error as e:
        raise Error(str(e))
    enc_key: bytes = checker4["ciphertext"]
    iv: bytes = checker4["iv"]

    try:
        checker6 = db.update(login=login, enc_key=enc_key, iv=iv)
    except db.db_Error as e:
        raise Error(str(e))

    print("The key change was successful")
    return None
コード例 #2
0
def change_pass(login: str, old_master_key: bytes) -> None:
    try:
        checker1 = db.info(login)
        if checker1 is None:
            raise Error(
                "Error in main.change_pass(): A user with this login " +
                "is not registered")
    except db.db_Error as e:
        raise Error(str(e))
    info: Tuple[str, bytes, str, bytes, bytes] = checker1

    password: str = input("Enter your new password: "******"Error in main.change_pass(): Invalid password")
    error_list: List[str] = list()
    for elem in password:
        if elem not in _ALLOWED_CHARACTERS:
            error_list.append(elem)
    if len(error_list) != 0:
        raise Error(
            f"Error in main.change_pass(): '{''.join(error_list)}' " +
            "is not allowed")
    password_bytes: bytes = password.encode(encoding="utf-8")

    encrypted_data: Dict[str, bytes] = {"ciphertext": info[3], "iv": info[4]}

    try:
        checker2: bytes = security.decrypt(encrypted_data, old_master_key)
    except security.security_Error as e:
        raise Error(str(e))
    key: bytes = checker2

    try:
        checker3 = security.gen_master_key(password_bytes)
    except security.security_Error as e:
        raise Error(str(e))
    master_key_new: bytes = checker3

    try:
        checker4 = security.encrypt(key, master_key_new, info[4])
    except security.security_Error as e:
        raise Error(str(e))
    enc_key = checker4["ciphertext"]

    try:
        checker5 = security.hash(password_bytes)
    except security.security_Error as e:
        raise Error(str(e))
    password_bytes_hash: bytes = checker5

    try:
        checker6 = db.update(
            login=login, hash=password_bytes_hash, enc_key=enc_key)
    except db.db_Error as e:
        raise Error(str(e))

    print("Password change was successful")
    return None
コード例 #3
0
def delete_account(login: str) -> None:
    try:
        checker1 = db.info(login)
        if checker1 is None:
            raise Error(
                "Error in main.delete_account(): A user with this login " +
                "is not registered")
    except db.db_Error as e:
        raise Error(str(e))
    info: Tuple[str, bytes, str, bytes, bytes] = checker1

    dirr: str = info[2]

    if os.path.exists(dirr):
        shutil.rmtree(dirr)

    try:
        checker2 = db.cut(login)
    except db.db_Error as e:
        raise Error(str(e))

    print("Account deletion completed successfully")
    return None
コード例 #4
0
def _decrypt_text(login: str, master_key: bytes, ct: bytes) -> str:
    if not isinstance(login, str) or not isinstance(master_key, bytes) \
            or not isinstance(ct, bytes):
        raise notes_Error("Error in notes._decrypt_text(): Invalid input type")

    try:
        checker1: Union[Tuple[str, bytes, str, bytes, bytes],
                        None] = db.info(login)
        if checker1 is None:
            raise notes_Error(f"Error in notes._encrypt_text(): \
                        User with login {login} not found")
    except db.db_Error as e:
        raise notes_Error(str(e))
    info: Tuple[str, bytes, str, bytes, bytes] = checker1

    encrypted_data: Dict[str, bytes] = {"ciphertext": info[3], "iv": info[4]}

    try:
        checker2 = security.decrypt(encrypted_data, master_key)
    except security.security_Error as e:
        raise notes_Error(str(e))
    key: bytes = checker2

    encrypted_text = {"ciphertext": ct, "iv": info[4]}

    try:
        checker3 = security.decrypt(encrypted_text, key)
    except security.security_Error as e:
        raise notes_Error(str(e))
    text: bytes = checker3

    result = text.decode(encoding="utf-8")

    if not isinstance(result, str):
        raise notes_Error(
            "Error in notes._decrypt_text(): Invalid output type")
    return result
コード例 #5
0
ファイル: user_topics.py プロジェクト: Tracywangsw/TagRecs
import lda
import db
import datetime
import numpy as np
import scipy.stats as stats
import multiprocessing as mp
import math
import pdb
import json

# lda_loaded_model = lda.load_lda('model/50_topics_lda.txt')
db_info = db.info()

def movieid_plot_map(return_list):
  mv_plot = {}
  for r in return_list:
    # (mvid,plot) = r[:]
    (mvid,plot) = (r,return_list[r])
    if plot:
      if type(plot) is tuple:
        plot_str = ''
        for p in plot:
          if p: plot_str += p
        mv_plot[mvid] = plot_str
      else: mv_plot[mvid] = plot
    else: print "##!!!"
  return mv_plot

plot_map = movieid_plot_map(db_info.mv_plots_set)

def item_lda_model(num_topics=500,path='model/whole_movie_lda.txt'):
コード例 #6
0
ファイル: bot.py プロジェクト: renatick321/bot
def info(message):
    if message.chat.id == ADMIN:
        bot.send_message(message.chat.id, db.info(), reply_markup=menu())
コード例 #7
0
def auth() -> Tuple[str, bytes]:
    print("AUTHENTICATION")
    login: str = input("Enter your login: "******"Error in main.auth(): Invalid login")
    error_list: List[str] = list()
    for elem in login:
        if elem not in _ALLOWED_CHARACTERS:
            error_list.append(elem)
    if len(error_list) != 0:
        raise Error(
            f"Error in main.auth(): '{''.join(error_list)}' is not allowed")

    try:
        checker1 = db.info(login)
        if checker1 is None:
            raise Error(
                "Error in main.auth(): A user with this login " +
                " is not registered")
    except db.db_Error as e:
        raise Error(str(e))

    password: str = input("Enter your password: "******"Error in main.auth(): Invalid password")
    for elem in password:
        if elem not in _ALLOWED_CHARACTERS:
            error_list.append(elem)
    if len(error_list) != 0:
        raise Error(
            f"Error in main.auth(): '{''.join(error_list)}' is not allowed")

    if os.name == "posix" or os.name == "nt":
        dirr: str = os.path.join("authentication", "notes", login)
    else:
        raise Error("Error in main.auth(): Operating system not supported")

    if not os.path.exists(dirr):
        os.mkdir(dirr)

    password_bytes: bytes = password.encode(encoding="utf-8")

    try:
        checker2 = security.gen_master_key(password_bytes)
    except security.security_Error as e:
        raise Error(str(e))
    master_key: bytes = checker2

    try:
        checker3 = security.hash(password_bytes)
    except security.security_Error as e:
        raise Error(str(e))
    password_bytes_hash: bytes = checker3

    info: Tuple[str, bytes, str, bytes, bytes] = checker1
    password_bytes_hash_orig: bytes = info[1]

    if password_bytes_hash != password_bytes_hash_orig:
        raise Error("Error in main.auth(): Wrong password")
    print("Authorization completed successfully")

    if not isinstance(login, str) or not isinstance(master_key, bytes):
        raise Error("Error in main.auth(): Invalid output type")
    return (login, master_key)
コード例 #8
0
def create_account() -> None:
    print("REGISTRATION")
    login: str = input("Enter your login: "******"Error in main.create_account(): Invalid login")
    if len(login) > 100:
        raise Error(
            f"Error in main.create_account(): Login too long " +
            "(len: {len(login)}).\nMax length 100")
    error_list: List[str] = list()
    for elem in login:
        if elem not in _ALLOWED_CHARACTERS:
            error_list.append(elem)
    if len(error_list) != 0:
        raise Error(
            f"Error in main.create_account(): '{''.join(error_list)}' " +
            "is not allowed")

    try:
        checker1 = db.info(login)
        if checker1 is not None:
            raise Error(
                "Error in main.create_account(): A user with this login " +
                "is already registered")
    except db.db_Error as e:
        raise Error(str(e))

    password: str = input("Enter your password: "******"Error in main.create_account(): Invalid password")
    for elem in password:
        if elem not in _ALLOWED_CHARACTERS:
            error_list.append(elem)
    if len(error_list) != 0:
        raise Error(
            f"Error in main.create_account(): '{''.join(error_list)}' " +
            "is not allowed")

    password_bytes: bytes = password.encode(encoding="utf-8")

    try:
        checker2 = security.gen_master_key(password_bytes)
    except security.security_Error as e:
        raise Error(str(e))
    master_key: bytes = checker2

    try:
        checker3 = security.hash(password_bytes)
    except security.security_Error as e:
        raise Error(str(e))
    password_bytes_hash: bytes = checker3

    if os.name == "posix" or os.name == "nt":
        dirr: str = os.path.join("authentication", "notes", login)
    else:
        raise Error(
            "Error in main.create_account(): Operating system not supported")

    if os.path.exists(dirr):
        shutil.rmtree(dirr)
    os.mkdir(dirr)

    key: bytes = security.gen_key()
    try:
        checker4 = security.encrypt_new(key, master_key)
    except security.security_Error as e:
        raise Error(str(e))
    enc_key_with_iv: Dict[str, bytes] = dict(checker4)
    enc_key: bytes = enc_key_with_iv["ciphertext"]
    iv: bytes = enc_key_with_iv["iv"]

    db.insert(login, password_bytes_hash, dirr, enc_key, iv)
    print("Account creation completed successfully")
    return None
コード例 #9
0
import db
from math import sqrt
import multiprocessing as mp
import json

db_info = db.info()


def hash2list(map):
    sort_tag = []
    for tag in map:
        sort_tag.append([map[tag], tag])
    sort_tag.sort()
    sort_tag.reverse()
    return sort_tag


def list2hash(list):
    hashmap = {}
    for l in list:
        (count, item) = l[:]
        hashmap[item] = count
    return hashmap


class generate_tag():
    def __init__(self, userid):
        self.mv_list = db_info.user_train_movies(userid)

    def mv_top_tags(self, mvid, top=10):
        hash_tag = db_info.movie_tags(mvid)