Skip to content

Commit 708f939

Browse files
committed
perf
1 parent 52ad04c commit 708f939

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ A library that gives you access to the powerful Parse Server backend from your J
3030
- [Getting Started](#getting-started)
3131
- [Using Parse on Different Platforms](#using-parse-on-different-platforms)
3232
- [Core Manager](#core-manager)
33-
= [Encrypt Local Storage](#encrypt-local-storage)
33+
- [Encrypt Local Storage](#encrypt-local-storage)
3434
- [3rd Party Authentications](#3rd-party-authentications)
3535
- [Experimenting](#experimenting)
3636
- [Contributing](#contributing)
@@ -125,7 +125,7 @@ such as logged in `Parse.User`.
125125
Parse.secret = 'MY_SECRET_KEY'; // or Parse.CoreManager.set('ENCRYPTED_KEY', 'MY_SECRET_KEY');
126126
```
127127

128-
The SDK has built-in encryption using the [Web Crypto API][webcrypto]. If your platform doesn't have Web Crypto support yet like react-native you will need to [polyfill](react-native-webview-crypto) Web Crypto.
128+
The SDK has built-in encryption using the [Web Crypto API][webcrypto]. If your platform doesn't have Web Crypto support yet like react-native you will need to [polyfill][react-native-webview-crypto] Web Crypto.
129129

130130
We recommend creating your own [CryptoController][crypto-controller].
131131

@@ -172,5 +172,4 @@ We really want Parse to be yours, to see it grow and thrive in the open source c
172172
[link-with]: https://docs.parseplatform.org/js/guide/#linking-users
173173
[open-collective-link]: https://opencollective.com/parse-server
174174
[react-native-webview-crypto]: https://www.npmjs.com/package/react-native-webview-crypto
175-
[types-parse]: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/parse
176175
[webcrypto]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

src/CryptoController.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@ if (typeof window !== 'undefined' && window.crypto && process.env.PARSE_BUILD !=
1212
decoder = new TextDecoder();
1313
}
1414

15-
const bufferToBase64 = buff =>
16-
btoa(new Uint8Array(buff).reduce((data, byte) => data + String.fromCharCode(byte), ''));
17-
18-
const base64ToBuffer = b64 => Uint8Array.from(atob(b64), c => c.charCodeAt(null));
15+
const bufferToBase64 = buff => {
16+
if (typeof window !== 'undefined') {
17+
return btoa(String.fromCharCode(...new Uint8Array(buff)));
18+
} else {
19+
return Buffer.from(buff).toString('base64');
20+
}
21+
};
22+
const base64ToBuffer = b64 => {
23+
if (typeof window !== 'undefined') {
24+
return Uint8Array.from(atob(b64), c => c.charCodeAt(0));
25+
} else {
26+
return new Uint8Array(Buffer.from(b64, 'base64'));
27+
}
28+
};
1929

2030
const importKey = async key =>
2131
webcrypto.subtle.importKey('raw', encoder.encode(key), 'PBKDF2', false, ['deriveKey']);

0 commit comments

Comments
 (0)