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

writeFile doesn't work properly with ciphered data #82

Open
MaksymKuzmych opened this issue Apr 1, 2024 · 3 comments
Open

writeFile doesn't work properly with ciphered data #82

MaksymKuzmych opened this issue Apr 1, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@MaksymKuzmych
Copy link

react-native: 0.73.6
react-native-file-access: 3.0.7
Platform: Android

Bug
I use node-forge to cipher my data. When I try to writeFile, it doesn't write all cipheredBytes. For example, there are 2 cases, when I try to readFile that was written by react-native-file-access (1 photo) and by react-native-fs (2 photo). Of course after that I can't properly decipher it when I want to see my image. Any ideas why it's happening?

Details
Screenshot 2024-04-01 at 15 41 39

photo_2024-04-01_15-57-38

@MaksymKuzmych MaksymKuzmych added the bug Something isn't working label Apr 1, 2024
@alpha0010
Copy link
Owner

When reading/writing the data, are you using base64 encoding? If you have not specified, this library defaults to utf8, which likely truncates on the first null character in the binary data.

@MaksymKuzmych
Copy link
Author

MaksymKuzmych commented Apr 1, 2024

When reading/writing the data, are you using base64 encoding? If you have not specified, this library defaults to utf8, which likely truncates on the first null character in the binary data.

With react-native-fs I used 'utf8' (your lib works the same, if you don't pass any encoding, it'll be utf8). Any opportunity to not "truncates on the first null character in the binary data"? As I see it truncates after '§' symbol.

@alpha0010
Copy link
Owner

It looks like node-forge's API supports outputting data in base 64, which this library should be able to handle fine (if you set the encoding parameter). Does that meet your needs?


The way React Native passes strings between JS and native code is unsafe for binary data (strings containing null characters). The workaround for this is to base 64 encode the data for the round trip. react-native-fs automatically encodes/decodes all strings passed through its API in base 64, which allows this to work for binary data (almost; it still processes as strings with js polyfilled atob/btoa implementations, which are not binary safe, but handles more cases than doing nothing, so if you have a small sample set, you are unlikely to notice problems).

In this library, I avoided the automatic internal conversions, since it is a performance cost that might not be need if the app is only working with text data. Instead, the app explicitly chooses when to pay the extra cost of base 64 encoding/decoding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants