Further append code

main
Rebel Zhang 2025-09-03 18:51:29 +08:00
parent 3805f80b75
commit 72bfab99a1
3 changed files with 18 additions and 25 deletions

View File

@ -2,7 +2,7 @@
import ressenger_exceptions, ressenger_cryptography
import pathlib, shutil, pickle
def initialise(password, b32address, username='default', port=5273, force=False):
def initialise(password, b32address, username='default', port=5273, nick='John Doe', force=False):
profile_path=pathlib.Path('~/.ressenger/').expanduser()
if profile_path.exists():
if (profile_path.is_file() or profile_path.is_symlink()):
@ -20,5 +20,7 @@ def initialise(password, b32address, username='default', port=5273, force=False)
shutil.rmtree(user_path)
else:
raise FileExistsError(f'Cannot create ~/.ressenger/{username} file, is a file exists there?')
enc_pri, enc_pub=ressenger_cryptography.generate_keypair()
sig_pri, sig_pub=ressenger_cryptography.generate_keypair()
with open(user_path, 'wb') as file:
file.write(ressenger_cryptography.encrypt_bytes(pickle.dumps({'port':port, 'b32address':b32address}, protocol=pickle.HIGHEST_PROTOCOL), password))
file.write(ressenger_cryptography.encrypt_bytes(pickle.dumps({'port':port, 'b32address':b32address, 'enc_pri':enc_pri, 'enc_pub':enc_pub, 'sig_pri':sig_pri, 'sig_pub':sig_pub, 'keyring':[], 'events':{}, 'contacts':{}}, protocol=pickle.HIGHEST_PROTOCOL), password))

View File

@ -2,8 +2,9 @@
import ressenger_cryptography, ressenger_exceptions
import pickle, pathlib
#class Contact():
# def __init__(self, nickname, b32address):
class Contact():
def __init__(self, nickname, b32address, pub_enc, pub_sig):
def load_user(password, username='default'):
user_path=pathlib.Path(f'~/.ressenger/{username}').expanduser()

View File

@ -1,8 +1,18 @@
#!/usr/bin/python3
from Cryptodome.Cipher import AES
import struct
from typing import Optional, Tuple
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import AES, PKCS1_OAEP
from Cryptodome.Protocol.KDF import PBKDF2
from Cryptodome.Random import get_random_bytes
from Cryptodome.Util.Padding import pad, unpad
from Cryptodome.Signature import pss
from Cryptodome.Hash import SHA256
RSA_BITS = 4096 # change to 4096 if you want stronger RSA keys (slower)
AES_KEY_LEN = 32 # AES-256
AES_NONCE_LEN = 12 # recommended nonce length for GCM
TAG_LEN = 16 # GCM tag length
def encrypt_bytes(data: bytes, password: str, *, salt: bytes = None) -> bytes:
if salt is None:
@ -22,26 +32,6 @@ def decrypt_bytes(token: bytes, password: str) -> bytes:
pt = unpad(cipher.decrypt(ct), AES.block_size)
return pt
from typing import Optional, Tuple
import struct
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
from Cryptodome.Signature import pss
from Cryptodome.Hash import SHA256
# Configuration
RSA_BITS = 4096 # change to 4096 if you want stronger RSA keys (slower)
AES_KEY_LEN = 32 # AES-256
AES_NONCE_LEN = 12 # recommended nonce length for GCM
TAG_LEN = 16 # GCM tag length
# Wire format:
# [2 bytes rsa_ct_len][rsa_ct]
# [1 byte nonce_len][nonce]
# [4 bytes ct_len][ciphertext]
# [16 bytes tag]
# [2 bytes sig_len][signature]
def generate_keypair(bits: int = RSA_BITS) -> Tuple[bytes, bytes]:
"""
Generate an RSA keypair. Returns (private_pem_bytes, public_pem_bytes).