Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should support any buffer objects, not just bytes #707

Open
covert-encryption opened this issue Nov 21, 2021 · 2 comments
Open

Should support any buffer objects, not just bytes #707

covert-encryption opened this issue Nov 21, 2021 · 2 comments

Comments

@covert-encryption
Copy link

Use ffi.from_buffer(...) for conversions, so that bytearray, memoryview and other things work too, instead of checking that the arguments are bytes instances.

I would also prefer being able to give output buffers to low level functions, for instance I had to do this hack to enable encryption without creating a new buffer. It works even in place, if ciphertext and message are the same buffer:

from nacl._sodium import ffi, lib

def encrypt_into(ciphertext, message, aad, nonce, key):
  mlen = len(message)
  clen = ffi.new("unsigned long long *")
  ciphertext = ffi.from_buffer(ciphertext)
  message = ffi.from_buffer(message)
  if aad:
    _aad = ffi.from_buffer(aad)
    aalen = len(aad)
  else:
    _aad = ffi.NULL
    aalen = 0

  return lib.crypto_aead_chacha20poly1305_ietf_encrypt(
    ciphertext, clen, message, mlen, _aad, aalen, ffi.NULL, nonce, key
  )

While for most things it does not matter if copies are made and new buffers are being allocated, stream ciphers can be much faster with proper buffer management.

@reaperhulk
Copy link
Member

Yes, this library predates from_buffer in cffi, but it should use it. pyca/cryptography switched long ago but no one has done the work on pynacl yet.

@bendem
Copy link

bendem commented Feb 20, 2023

I couldn't find any way to use pynacl to encrypt large files as all the public API takes is bytes. It's a pretty big limitation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants