Skip to content

fix: return Buffer from decrypt instead of string#3

Open
elyobo wants to merge 1 commit intozoran-php:mainfrom
elyobo:fix/return-buffer-from-decrypt
Open

fix: return Buffer from decrypt instead of string#3
elyobo wants to merge 1 commit intozoran-php:mainfrom
elyobo:fix/return-buffer-from-decrypt

Conversation

@elyobo
Copy link

@elyobo elyobo commented Dec 15, 2025

Summary

Converting decrypted data to UTF-8 corrupts binary data because invalid byte sequences are replaced with the U+FFFD replacement character. This makes it impossible to decrypt non-text content (e.g. binary salts used for key derivation).

This PR changes decrypt() to return a Buffer instead of a string, allowing callers to interpret the data as needed:

  • For text: fernet.decrypt(token).toString('utf-8')
  • For binary: use the Buffer directly

Changes

  • Fernet.decrypt() now returns Buffer instead of string
  • Updated tests to handle Buffer return type
  • Added test to verify Buffer is returned

Fixes #2

Converting decrypted data to UTF-8 corrupts binary data because invalid
byte sequences are replaced with the U+FFFD replacement character. This
makes it impossible to decrypt non-text content (e.g. binary salts).

Returning a Buffer allows callers to interpret the data as needed -
they can call .toString('utf-8') for text, or use the raw bytes for
binary data.

Fixes zoran-php#2
@elyobo
Copy link
Author

elyobo commented Dec 15, 2025

Note: change is straightforward but FYI was written by Claude.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decryption of non-string content fails due to utf8 stringification

1 participant