Skip to content

Commit

Permalink
py backend table encryption, fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
smurfd committed Oct 25, 2024
1 parent a9a72d1 commit 5231020
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
32 changes: 31 additions & 1 deletion lotordb/tables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
import struct, gzip, threading, mmap, socket, secrets, io
import struct, gzip, threading, mmap, socket, secrets, io, os
from typing import List, Union, BinaryIO, IO
from lotordb.vars import DbIndex, DbData, Vars
from lotordb.cipher import Cipher
Expand Down Expand Up @@ -161,3 +161,33 @@ def decrypt_bytearray_to_data_segmented(self, data):

def decrypt_bytearray_to_data(self, data):
return self.decrypt_data(data)

def table_temp(self):
with open('lotordb/src/.build/bin.b', 'ab') as f:
for i in range(20):
packedheader = 123456789 # len = 27
name = 'John'.ljust(20)[:20] # len = 20
age = 32 + i # len = 6
height = 6.0 # len = 8
data = packedheader.to_bytes(packedheader.bit_length() + 7 // 8) + name.encode() + age.to_bytes(age.bit_length() + 7 // 8) + bytes(struct.pack('d', height)) + b' ' * (512-(27+6+8+20))
# TODO: encrypt data before write, data = 512 bytes
f.write(data)
with open('lotordb/src/.build/bin.b', 'rb') as f:
len = 512 # 27+6+8+20
fs = os.path.getsize('lotordb/src/.build/bin.b')
chunk = fs // len
print(f'size of the file: {fs} and number of chunks: {fs // len}')
f.seek(len * 10, 0)
data = f.read(len)
# Decrypt data
pkh, name, age, h = data[0:27], data[27:47], data[47:53], data[53:61]
#print(f'11th entry: {name} {int.from_bytes(age, 'big')} {struct.unpack('d', h)[0]} {int.from_bytes(pkh, 'big')}')
print('Searching for age 42: ', end='')
f.seek(0, 0)
for i in range(fs // len):
data = f.read(len)
pkh, name, age, h = data[0:27], data[27:47], data[47:53], data[53:61]
if int.from_bytes(age, 'big') == 42:
print('found')
return 1
return 0
5 changes: 5 additions & 0 deletions lotordb/test/test_lotordb.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ def test_lotordb_table_encrypt_decrypt():
tables.decrypt_bytearray_to_data_segmented(tables.data_to_bytearray_encrypt_segment(dad, ind))
print('time {:.4f}'.format(time.perf_counter() - t))

def test_lotordb_table():
t = time.perf_counter()
tables = Tables('.lib/db39')
tables.table_temp()
print('time {:.4f}'.format(time.perf_counter() - t))

if __name__ == '__main__':
print('OK')

0 comments on commit 5231020

Please sign in to comment.