Skip to content

Commit

Permalink
More robust and simpler hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
Kseen715 committed Aug 8, 2024
1 parent 30e76c0 commit 9a5e0e7
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 15 deletions.
2 changes: 1 addition & 1 deletion hashes/README.md.hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
|hi6�e*��O�� b.D�2��Ł}U�J&
�<�` $��o4@�Q�"$$.:M�i��ʔ5ð�#��lenM�������6��@ �w�
2 changes: 1 addition & 1 deletion hashes/chatgpt.com.csv.hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{p��[����8P �K���c֥ܹ `.>
��Ɛ̬}�ђ��}Tg���ʵŸx�������k���p�����������������z�Ҭ���Z=���
1 change: 0 additions & 1 deletion hashes/db.csv

This file was deleted.

2 changes: 1 addition & 1 deletion hashes/db.csv.hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
����}�v̵J����R��<kd�]EQc�d"�G
����Q� ۆݲ�Lf�j���K9 SɄJ�r�G7�m�n�F0���냰�PJ�+ wy���=)'
Expand Down
Binary file modified hashes/discord.csv.hash
Binary file not shown.
2 changes: 1 addition & 1 deletion hashes/facebook.com.csv.hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
�D�U$}v�q��έ�'tɗP㡔K��s۱U%
ݿiId����jP�tC���c��vf�Oo���nFAX��Y8�����ipj����uG5��yX���:u�{�
3 changes: 1 addition & 2 deletions hashes/instagram.com.csv.hash
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
�����=��V
���*�^���}תe-#d�
�^jh�y*���8;\EM^%��`-��>�+l��8J'��9Z8U%�li)�S��[��W\jGeB
2 changes: 1 addition & 1 deletion hashes/meta.csv.hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
��4��3廉[��U3-��P :gbُ���{K
v�ָ�����ƻ���yUz�����˼�����{x�xo�����������lImzv������������w$
2 changes: 1 addition & 1 deletion hashes/microsoft.csv.hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
�h�#��p�e����+'�P&����"� X���"
t�(�ѻ����Y�����>�c6�;Uڃ)^0�f�������|_�'o�`���U���?�
Binary file modified hashes/netflix.csv.hash
Binary file not shown.
Binary file modified hashes/nhentai.net.csv.hash
Binary file not shown.
Binary file modified hashes/x.com.csv.hash
Binary file not shown.
Binary file modified hashes/youtube.com.csv.hash
Binary file not shown.
37 changes: 32 additions & 5 deletions src/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import colorama
import datetime
from hashlib import sha256
# from hashlib import sha256


DB_FILE = 'db.csv'
Expand Down Expand Up @@ -124,6 +124,29 @@ def log_error(msg):
print(f'{colorama.Fore.RED}{datetime.datetime.now()} [ERROR] {msg}{colorama.Style.RESET_ALL}')


class FastHash:
"""Manual hashing class, limited to 512 bits (64 bytes)"""

def __init__(self):
self._hash_value = [0] * 64 # Initialize a list of 64 zeros
self._data = bytearray()

def update(self, data):
"""Update the hash with new data"""
self._data.extend(data)
for i, byte in enumerate(data):
self._hash_value[i % 64] = (self._hash_value[i % 64] + byte) % 256 # Simple hash update

def digest(self):
"""Return the digest of the data passed to the update() method so far"""
return bytes(self._hash_value)

def hexdigest(self):
"""Return the hexadecimal digest of the data passed to the update() method so far"""
return ''.join(f'{byte:02x}' for byte in self._hash_value)



def hash_file(filename):
"""Hash file
Expand All @@ -133,20 +156,24 @@ def hash_file(filename):
Returns:
bytes: Hash of the file in bytes
"""
hasher = FastHash()
with open(filename, 'rb') as f:
return sha256(f.read()).digest()
hasher.update(f.read())
return hasher.digest()


def hash_str(string):
"""_summary_ Hash string
"""Hash string
Args:
string (str): String to hash
Returns:
str: Hash of the string
bytes: Hash of the string
"""
return sha256(string.encode()).digest()
hasher = FastHash()
hasher.update(string.encode())
return hasher.digest()


def save_hash_binary(new_hash_bytes, new_hash_filename):
Expand Down
2 changes: 1 addition & 1 deletion src/sort_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def sort_db():
data = drop_duplicates(data)
write_csv(data, DB_FILE)
log_happy('Database sorted')
save_hash_binary(hash_file(DB_FILE), './hashes/' + DB_FILE)
save_hash_binary(hash_file(DB_FILE), './hashes/' + DB_FILE + '.hash')
log_info('sort_db: Finished')


Expand Down

0 comments on commit 9a5e0e7

Please sign in to comment.