rsa.js
904 Bytes
import cryptico from 'cryptico';
class Rsa {
constructor() {
this.mattsRSAkey = cryptico.generateRSAKey('d7b7ac4b491fd2b1b9e27bc2ca9bf5d0', 1024);
this.mattsPublicKeyString = cryptico.publicKeyString(this.mattsRSAkey);
}
encrypt(plainText) {
if (typeof plainText === 'object') {
plainText = encodeURIComponent(JSON.stringify(plainText));
}
return cryptico.encrypt(plainText, this.mattsPublicKeyString).cipher;
}
decrypt(cipherText, Type) {
let plainText = cryptico.decrypt(cipherText, this.mattsRSAkey).plaintext;
if (this.getType(Type) === 'Object') {
return JSON.parse(decodeURIComponent(plainText));
}
return plainText;
}
getType(fn) {
let match = fn && fn.toString().match(/^\s*function (\w+)/);
return match && match[1];
}
}
export default new Rsa();