oscrypto

1.3.0 · active · verified Sun Mar 29

oscrypto is a compilation-free Python library that exposes cryptography primitives from the host operating system's crypto libraries, such as Windows CNG, macOS Security.framework, and OpenSSL/LibreSSL on Linux/BSD. It currently stands at version 1.3.0 and focuses on providing basic crypto functionality like TLS (SSL) sockets, key generation, encryption, decryption, signing, verification, and KDFs. The library relies on the OS for security patching, avoiding the need for recompilation with every new vulnerability.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate an RSA key pair, encrypt data using the public key with OAEP padding, and then decrypt it using the private key. This illustrates a fundamental asymmetric encryption workflow provided by `oscrypto.asymmetric`.

from oscrypto.asymmetric import generate_pair, rsa_oaep_encrypt, rsa_oaep_decrypt
from oscrypto.util import rand_bytes

# Generate an RSA key pair
public_key, private_key = generate_pair('rsa', bit_size=2048)

# Data to encrypt
original_data = b'This is a secret message.'

# Encrypt data using the public key
encrypted_data = rsa_oaep_encrypt(public_key, original_data)
print(f'Encrypted data length: {len(encrypted_data)} bytes')

# Decrypt data using the private key
decrypted_data = rsa_oaep_decrypt(private_key, encrypted_data)

print(f'Original data: {original_data.decode()}')
print(f'Decrypted data: {decrypted_data.decode()}')
assert original_data == decrypted_data

view raw JSON →