1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/app-mobile/services/e2ee/RSA.react-native.ts
Laurent c758377188
All: Add support for public-private key pairs and improved master password support (#5438)
Also improved SCSS support, which was needed for the master password dialog.
2021-10-03 16:00:49 +01:00

44 lines
1.1 KiB
TypeScript

import { RSA } from '@joplin/lib/services/e2ee/types';
const RnRSA = require('react-native-rsa-native').RSA;
interface RSAKeyPair {
public: string;
private: string;
}
const rsa: RSA = {
generateKeyPair: async (keySize: number): Promise<RSAKeyPair> => {
const keys: RSAKeyPair = await RnRSA.generateKeys(keySize);
// Sanity check
if (!keys.private) throw new Error('No private key was generated');
if (!keys.public) throw new Error('No public key was generated');
return keys;
},
loadKeys: async (publicKey: string, privateKey: string): Promise<RSAKeyPair> => {
return { public: publicKey, private: privateKey };
},
encrypt: async (plaintextUtf8: string, rsaKeyPair: RSAKeyPair): Promise<string> => {
return RnRSA.encrypt(plaintextUtf8, rsaKeyPair.public);
},
decrypt: async (ciphertextBase64: string, rsaKeyPair: RSAKeyPair): Promise<string> => {
return RnRSA.decrypt(ciphertextBase64, rsaKeyPair.private);
},
publicKey: (rsaKeyPair: RSAKeyPair): string => {
return rsaKeyPair.public;
},
privateKey: (rsaKeyPair: RSAKeyPair): string => {
return rsaKeyPair.private;
},
};
export default rsa;