I am trying to post two inputs with axios and I want to base64 encode them before I send them.
Deprecated since v6
const encodedString = new Buffer('your string here').toString('base64');
Use Instead
const encodedString = Buffer.from('your string here').toString('base64');
Consider using base-64 as well which is compatible with btoa and atob, worked for me in react and react native:
npm install base-64 --save
import {decode as base64_decode, encode as base64_encode} from 'base-64';
let encoded = base64_encode('YOUR_DECODED_STRING');
let decoded = base64_decode('YOUR_ENCODED_STRING');
In case you are using typescript, use #types/base-64 for typing
npm i --save-dev #types/base-64
Is better you use Buffer.from('Your String').toString('base64'), because new Buffer is deprecated
Related
I am trying to get unique ID for my device and I get is successfully with Constants.deviceId. However, I need it in 8 bytes (BigInt type). Is it possible somehow?
Device ID in string looks like this: "AAAAAAAA-BBBB-CCCC-DDDD-EEEEFFFFGGGG"
Is it better to use another approach and just generate a random 8 byte number and use it as unique ID for the device?
Can you encode it as base64?
yarn add Base64.
import Base64 from 'Base64';
const encoded = Base64.btoa('AAAAAAAA-BBBB-CCCC-DDDD-EEEEFFFFGGGG');
const decoded = Base64.atob(encoded);
// Should return QUFBQUFBQUEtQkJCQi1DQ0NDLUREREQtRUVFRUZGRkZHR0dH
npm package.
Blog with more info
I searched about [How to use qs in react] but can't find the answer.
Qs's official site https://www.npmjs.com/package/qs
Usage
qs = require('qs');
var assert = require('assert');
var obj = qs.parse('a=c');
assert.deepEqual(obj, { a: 'c' });
var str = qs.stringify(obj);
assert.equal(str, 'a=c');
But I never see require() used in react.
Does anybody know this answer?
I need this for passing array parameter in axios.
You don't have to uninstall #types/qs from your project. The correct solution is to use:
import * as qs from 'qs'
This solved my problem
npm i --save-dev #types/qs
Just a quick note, in case someone reaches this thread from 2022 on.
Using this syntax works flawlessly on both React.js v18+ and Next.js v12+
import qs from 'qs';
I had this identical problem in my React.
This would work:
const qs = require('qs')
This wouldnt work:
import qs from 'qs'
I solved it by deleting the "#types/qs" section from my package-lock.json, then running
npm install
Run:
npm install qs
In your script, use:
import Qs from "qs";
I'm trying to simply encrypt a message using a given key and iV. I've tried several libraries to achieve this but Expo isn't compatible with any of them. I couldn't find any encryption libraries for Expo (That support AES). I guess my question is : How do I encrypt data in React Native running Expo
Ps : I am not interested in expo-crypto
Another possibility (what I did) is to use the CryptoES library.
https://www.npmjs.com/package/crypto-es
After long search I found it, it is a continued development of the 3.1 version of the CryptoJS library and can be used with Expo.
npm i --save crypto-es crypto-js
import CryptoES from "crypto-es";
Then you should encrypt the text, for example:
var mytexttoEncryption = "Hello"
const encrypted = CryptoES.AES.encrypt(mytexttoEncryption ,"your password").toString();
var C = require("crypto-js");
var Decrypted = C.AES.decrypt(E, "your password");
var result =Decrypted.toString(C.enc.Utf8);
console.log(result)
Use crypto-js#3.1 , 3.1.x version use Math.random() and doesn't require node "crypto" package. It's not as safe as the latest version but works for me.
yarn add crypto-js#3.1
I only use it for decrypting. If you really need it for some security requirements I suggest you encrypt it in server node enviroment.
I decided to use jshashes for my React-native & Expo project. The goal of this module is to reimplement hash node's crypto functions in pure javascript without dependency on node:
yarn add jshashes
first use this command :
npm i crypto-es
then now you should import it with this command :
import CryptoES from "crypto-es";
then you should encrypt the text :
for example :
var mytexttoEncryption = "Hello"
const encrypted = CryptoES.AES.encrypt(mytexttoEncryption ,"your password").toString();
now for decryption :
install the package of crypto-js with this command :
npm i crypto-js
then lets decrypt it
var C = require("crypto-js");
var Decrypted = C.AES.decrypt(E, "your password");
var result =Decrypted.toString(C.enc.Utf8);
console.log(result)
so use this it will be ok
Anytime I add a pdf lib. on my Meteor/React app, I get Uncaught ReferenceError: Buffer is not defined at util.js. I've tried all the proposed methods: init.js, meteor npm i --save buffer,
import Buffer from 'buffer';
if (typeof this.Buffer === 'undefined') {
this.Buffer = Buffer.Buffer;
}
all of these with no solution. I'm using Meteor 1.8.0.2. This is very annoying.
I finally got it to work by:
1. install buffer -> meteor npm install buffer
2. Add the following lines of code:
if (Meteor.isClient) {
const buffer = require("buffer");
global.Buffer = buffer.Buffer;
}
In React Native I'm trying to load an image stored at a relative path as a base64 string, but the require returns 3 as response instead of the image source.
I'm sure the path is correct, and I the require command works elsewhere in my Reacy Native JSX code to load images from the same relative path (<Image source={require('../resources/examplecar.jpg')}>) without any problem.
How to get a local image source from the filesystem as base64 string to send it in JSON?
var body = {
path: '../resources/examplecar.jpg',
data: {
image: require('../resources/examplecar.jpg'),
}
}
Using RNFS plugin it is possible to access the React Native assets and convert the data into a range of formats including Base64.
imageData = await RNFS.readFile(RNFS.MainBundlePath+"/assets/resources/examplecar.jpg", 'base64').then();
You can't have a variable in require. It won't work for image source in rn.
Do
image: require('../resources/examplecar.jpg')