Convert Figma logo to code with AI

Legrandin logopycryptodome

A self-contained cryptographic library for Python

2,980
539
2,980
63

Top Related Projects

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.

Quick Overview

The Legrandin/pycryptodome repository is a Python library that provides a wide range of cryptographic primitives and high-level algorithms. It is a fork of the original PyCrypto library, with a focus on improving performance, security, and maintainability.

Pros

  • Comprehensive Cryptographic Functionality: The library provides a wide range of cryptographic algorithms, including symmetric ciphers, hashes, public-key cryptography, and more.
  • Performance Optimization: The library is designed for performance, with many algorithms implemented in C for improved speed.
  • Active Maintenance and Development: The project is actively maintained, with regular updates and bug fixes.
  • Extensive Documentation: The project has detailed documentation, including examples and usage guides.

Cons

  • Compatibility with PyCrypto: While the library is a fork of PyCrypto, it is not entirely compatible, which may cause issues for existing PyCrypto-based projects.
  • Complexity: The library offers a wide range of features, which can make it more complex to use for simple use cases.
  • Potential Security Concerns: As with any cryptographic library, there is a risk of potential security vulnerabilities, which users should be aware of and monitor.
  • Limited Support for Older Python Versions: The library may not provide full support for older versions of Python, which could be a concern for some users.

Code Examples

Here are a few examples of how to use the pycryptodome library:

  1. Symmetric Encryption:
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

# Generate a random 16-byte key
key = get_random_bytes(16)

# Create a cipher object with the key
cipher = AES.new(key, AES.MODE_ECB)

# Encrypt a message
message = b"Hello, Cryptography!"
encrypted_message = cipher.encrypt(message)

# Decrypt the message
decrypted_message = cipher.decrypt(encrypted_message)
print(decrypted_message.decode())
  1. Hashing:
from Crypto.Hash import SHA256

# Create a hash object
hash_obj = SHA256.new()

# Update the hash with some data
hash_obj.update(b"Hello, Cryptography!")

# Get the hexadecimal digest
digest = hash_obj.hexdigest()
print(digest)
  1. Public-Key Encryption:
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP

# Generate a new RSA key pair
key = RSA.generate(2048)

# Extract the public and private keys
public_key = key.publickey()
private_key = key

# Encrypt a message using the public key
message = b"Hello, Cryptography!"
cipher = PKCS1_OAEP.new(public_key)
encrypted_message = cipher.encrypt(message)

# Decrypt the message using the private key
cipher = PKCS1_OAEP.new(private_key)
decrypted_message = cipher.decrypt(encrypted_message)
print(decrypted_message.decode())

Getting Started

To get started with the pycryptodome library, follow these steps:

  1. Install the library using pip:
pip install pycryptodome
  1. Import the necessary modules from the library:
from Crypto.Cipher import AES
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
  1. Use the library's functions and classes to perform cryptographic operations, as shown in the code examples above.

  2. Refer to the project's documentation for more detailed information on the available features and usage.

Competitor Comparisons

cryptography is a package designed to expose cryptographic primitives and recipes to Python developers.

Pros of pyca/cryptography

  • Actively maintained and supported by a large community of security experts
  • Provides a high-level, Pythonic API that abstracts away the complexity of low-level cryptographic operations
  • Supports a wide range of cryptographic algorithms and protocols, including the latest standards

Cons of pyca/cryptography

  • May have a steeper learning curve compared to Legrandin/pycryptodome, especially for users new to cryptography
  • May have slightly more overhead in terms of dependencies and installation complexity

Code Comparison

Legrandin/pycryptodome:

from Crypto.Cipher import AES
cipher = AES.new(key, AES.MODE_ECB)
plaintext = cipher.decrypt(ciphertext)

pyca/cryptography:

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
decryptor = cipher.decryptor()
plaintext = decryptor.update(ciphertext) + decryptor.finalize()

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

.. image:: https://github.com/Legrandin/pycryptodome/workflows/Integration%20test/badge.svg :target: https://github.com/Legrandin/pycryptodome/actions

.. image:: https://badge.fury.io/py/pycryptodome.svg :target: https://pypi.org/project/pycryptodome

.. image:: https://badge.fury.io/py/pycryptodomex.svg :target: https://pypi.org/project/pycryptodomex

PyCryptodome

PyCryptodome is a self-contained Python package of low-level cryptographic primitives.

It supports Python 2.7, Python 3.7 and newer, and PyPy.

The installation procedure depends on the package you want the library to be in. PyCryptodome can be used as:

#. an almost drop-in replacement for the old PyCrypto library. You install it with::

   pip install pycryptodome

In this case, all modules are installed under the Crypto package.

One must avoid having both PyCrypto and PyCryptodome installed at the same time, as they will interfere with each other.

This option is therefore recommended only when you are sure that the whole application is deployed in a virtualenv.

#. a library independent of the old PyCrypto. You install it with::

   pip install pycryptodomex

In this case, all modules are installed under the Cryptodome package. PyCrypto and PyCryptodome can coexist.

For faster public key operations in Unix, you should install GMP_ in your system.

PyCryptodome is a fork of PyCrypto. It brings the following enhancements with respect to the last official version of PyCrypto (2.6.1):

  • Authenticated encryption modes (GCM, CCM, EAX, SIV, OCB)

  • Hybrid Public Key Encryption (HPKE)

  • Accelerated AES on Intel platforms via AES-NI

  • First class support for PyPy

  • Elliptic curves cryptography (NIST P-curves; Ed25519, Ed448, Curve25519, Curve448)

  • Better and more compact API (nonce and iv attributes for ciphers, automatic generation of random nonces and IVs, simplified CTR cipher mode, and more)

  • SHA-3 hash algorithms (FIPS 202) and derived functions (NIST SP-800 185):

    • SHAKE128 and SHA256 XOFs
    • cSHAKE128 and cSHAKE256 XOFs
    • KMAC128 and KMAC256
    • TupleHash128 and TupleHash256
  • KangarooTwelve, TurboSHAKE128, and TurboSHAKE256 XOFs

  • Truncated hash algorithms SHA-512/224 and SHA-512/256 (FIPS 180-4)

  • BLAKE2b and BLAKE2s hash algorithms

  • Salsa20 and ChaCha20/XChaCha20 stream ciphers

  • Poly1305 MAC

  • ChaCha20-Poly1305 and XChaCha20-Poly1305 authenticated ciphers

  • scrypt, bcrypt, HKDF, and NIST SP 800 108r1 Counter Mode key derivation functions

  • Deterministic (EC)DSA and EdDSA

  • Password-protected PKCS#8 key containers

  • Shamir's Secret Sharing scheme

  • Random numbers get sourced directly from the OS (and not from a CSPRNG in userspace)

  • Simplified install process, including better support for Windows

  • Cleaner RSA and DSA key generation (largely based on FIPS 186-4)

  • Major clean ups and simplification of the code base

PyCryptodome is not a wrapper to a separate C library like OpenSSL. To the largest possible extent, algorithms are implemented in pure Python. Only the pieces that are extremely critical to performance (e.g. block ciphers) are implemented as C extensions.

For more information, see the homepage_.

For security issues, please send an email to security@pycryptodome.org.

All the code can be downloaded from GitHub_.

.. _homepage: https://www.pycryptodome.org .. _GMP: https://gmplib.org .. _GitHub: https://github.com/Legrandin/pycryptodome