Limit entries in selection

This commit is contained in:
Michael Rodin 2023-02-15 17:11:00 +01:00
parent bc2e49a3ea
commit cabceaa7fd
2 changed files with 21 additions and 18 deletions

View file

@ -24,7 +24,6 @@ This project was written and tested on an Arch Linux-based distribution and pyth
* sqlite3 * sqlite3
## Downloading ## Downloading
There isn't an install script currently.
```sh ```sh
git clone https://gitlab.com/rodin_schule/image-index-py.git git clone https://gitlab.com/rodin_schule/image-index-py.git
cd ./image-index-py cd ./image-index-py
@ -36,4 +35,5 @@ The top of the file holds some very important variables that need to be looked a
* INDEX_FILE: The absolute path of the database file. * INDEX_FILE: The absolute path of the database file.
* LINUX_APP_STARTER: The linux command which can open a file in the default application. Most distributions use `xdg-open`. * LINUX_APP_STARTER: The linux command which can open a file in the default application. Most distributions use `xdg-open`.
* ENCRYPT: This setting tells the script whether to encrypt the added files by default or not. * ENCRYPT: This setting tells the script whether to encrypt the added files by default or not.
* If set to True, the ids of the categories will be fully random (fe627ea4-3fd60 instead of category-3fd60) for pretty much zero-knowledge storage on a remote server without access to the database. * If set to True, the ids of the categories will be randomly generated (fe627ea4-3fd60 instead of category-3fd60) for pretty much zero-knowledge storage on a remote server without access to the database.
* MAX_ITEMS: The maximal amount of entries shown in the selection menu.

View file

@ -9,6 +9,7 @@ ROOT_DIR=os.getcwd() # The directory where all directories and files of the inde
INDEX_FILE=ROOT_DIR+"/index.db" # The database file INDEX_FILE=ROOT_DIR+"/index.db" # The database file
LINUX_APP_STARTER="xdg-open" # The command which opens the files in the default application LINUX_APP_STARTER="xdg-open" # The command which opens the files in the default application
ENCRYPT=True # True or False; Whether the default is to encrypt the file or to save it as a plain file ENCRYPT=True # True or False; Whether the default is to encrypt the file or to save it as a plain file
MAX_ITEMS=10 # Maximal amount of shown entries for selection
class database(): class database():
def __init__(self,filepath = INDEX_FILE): def __init__(self,filepath = INDEX_FILE):
@ -116,7 +117,7 @@ class database():
if len(sel_list) > 1: if len(sel_list) > 1:
n=0 n=0
print("Found several matches:") print("Found several matches:")
for tup in sel_list: for tup in sel_list[:MAX_ITEMS]:
temp_list=[] temp_list=[]
for j in tup: for j in tup:
temp_list.append(j) temp_list.append(j)
@ -141,6 +142,8 @@ class database():
print("\tAlias:\t ",temp_list[1]) print("\tAlias:\t ",temp_list[1])
print("\tDescription: ",temp_list[2]) print("\tDescription: ",temp_list[2])
n+=1 n+=1
if len(sel_list) > MAX_ITEMS:
print(f"The list was too long, so it was reduced to {MAX_ITEMS} entries.")
eingabe=input("Enter number(s) (0-{}; '*' for all entries): ".format(n-1)) eingabe=input("Enter number(s) (0-{}; '*' for all entries): ".format(n-1))
if not eingabe: if not eingabe:
return [""] return [""]
@ -179,6 +182,8 @@ class database():
if not secondlist: if not secondlist:
n=0 n=0
for i in self.get_col(typ): for i in self.get_col(typ):
if len(temp_list) > MAX_ITEMS:
break
aliases=[] aliases=[]
if i == "" and self.name == "FILES": if i == "" and self.name == "FILES":
print("NO ENTRIES IN THE INDEX!") print("NO ENTRIES IN THE INDEX!")
@ -233,7 +238,7 @@ class database():
return secondlist return secondlist
if not temp_list: if not temp_list:
return [""] return [""]
return temp_list return temp_list[:MAX_ITEMS+1]
return secondlist return secondlist
def update_index(self,typ,update,where,val): def update_index(self,typ,update,where,val):
@ -250,7 +255,6 @@ class enctable(database): # https://www.thesecuritybuddy.com/cryptography-and-py
def derive_key_and_iv(self, password, salt, key_length, iv_length): # derive key and IV from password and salt. def derive_key_and_iv(self, password, salt, key_length, iv_length): # derive key and IV from password and salt.
d = d_i = b'' d = d_i = b''
while len(d) < key_length + iv_length: while len(d) < key_length + iv_length:
#d_i = hashlib.md5(d_i + str.encode(password) + salt).digest() #obtain the md5 hash value
d_i = hashlib.md5(d_i + password + salt).digest() # obtain the md5 hash value d_i = hashlib.md5(d_i + password + salt).digest() # obtain the md5 hash value
d += d_i d += d_i
return d[:key_length], d[key_length:key_length+iv_length] return d[:key_length], d[key_length:key_length+iv_length]
@ -273,8 +277,6 @@ class enctable(database): # https://www.thesecuritybuddy.com/cryptography-and-py
else: else:
print("ERROR: MULTIPLE PASSWORD ENTRIES FOUND!") print("ERROR: MULTIPLE PASSWORD ENTRIES FOUND!")
return False return False
#print("ERROR: ENTRY FOR UUID {} ALREADY EXISTS!".format(out_uuid))
#return False
salt = os.urandom(bs) # return a string of random bytes salt = os.urandom(bs) # return a string of random bytes
key, iv = self.derive_key_and_iv(password, salt, key_length, bs) key, iv = self.derive_key_and_iv(password, salt, key_length, bs)
cipher = AES.new(key, AES.MODE_CBC, iv) cipher = AES.new(key, AES.MODE_CBC, iv)
@ -304,7 +306,6 @@ class enctable(database): # https://www.thesecuritybuddy.com/cryptography-and-py
out_temp=tempfile.mkstemp(prefix="image-index-") out_temp=tempfile.mkstemp(prefix="image-index-")
filepath=out_temp[1] filepath=out_temp[1]
out_file=open(filepath,"wb") out_file=open(filepath,"wb")
#out_file=temp_file
else: else:
filepath=out_file filepath=out_file
out_file=open(out_file,"wb") out_file=open(out_file,"wb")
@ -313,12 +314,14 @@ class enctable(database): # https://www.thesecuritybuddy.com/cryptography-and-py
if password_list[0] != "": if password_list[0] != "":
if len(password_list) == 1: if len(password_list) == 1:
password=b64decode(password_list[0][2].encode()) password=b64decode(password_list[0][2].encode())
else: elif len(password_list) > 1:
print("ERROR: MULTIPLE PASSWORD ENTRIES FOUND!") print("ERROR: MULTIPLE PASSWORD ENTRIES FOUND!")
return False return False
else: else:
print("ERROR: NO PASSWORD FOUND FOR DECRYPTION!") print("ERROR: NO PASSWORD FOUND FOR DECRYPTION!")
return False return False
else:
password=b64decode(password.encode())
bs = AES.block_size bs = AES.block_size
salt = in_file.read(bs) salt = in_file.read(bs)
key, iv = self.derive_key_and_iv(password, salt, key_length, bs) key, iv = self.derive_key_and_iv(password, salt, key_length, bs)
@ -349,9 +352,9 @@ class metatable(database):
def add_index(self,val,alias,randhex=None): def add_index(self,val,alias,randhex=None):
if not randhex: if not randhex:
randhex=get_randhex() randhex=get_randchar()
if self.name == "TAGS" or bencrypt: if self.name == "TAGS" or bencrypt:
val=get_randhex(8) val=get_randchar(8)
else: else:
val=re.sub('[ ,?!/\\:!*"<>|]', '', val) val=re.sub('[ ,?!/\\:!*"<>|]', '', val)
super().add_index([val[:8] + "-" + randhex,alias]) super().add_index([val[:8] + "-" + randhex,alias])
@ -663,10 +666,10 @@ class filestable(database):
return self.select_index(selection,quiet) return self.select_index(selection,quiet)
def get_randhex(count=5): def get_randchar(count=5):
randhex="" randhex=""
for i in range(count): for i in range(count):
randhex+=random.choice("0123456789abcdef") randhex+=random.choice("0123456789abcdefghijklmnopqrstuvwxyz")
return randhex return randhex
def add(args): def add(args):