Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ exports.generate = (sizeInBits) => {
return keypair({ bits: sizeInBits })
}

exports.encrypt = (payloadString, publicKey) => {
if (typeof payloadString !== 'string' || typeof publicKey !== 'string') throw Error("Error encrypting. Payload and Public Key should be in text format. Example usage: ` let encryptedText = QuickEncrypt.encrypt('Some secret text here!', 'the public RSA key in text format here'); ` ")
exports.encrypt = (payloadString, key, usePublicKey = true) => {
if (typeof payloadString !== 'string' || typeof key !== 'string') throw Error("Error encrypting. Payload and Public/Private Key should be in text format. Example usage: ` let encryptedText = QuickEncrypt.encrypt('Some secret text here!', 'the public RSA key in text format here'); ` ")
try {
return crypto.publicEncrypt(publicKey, Buffer.from(payloadString, 'utf8')).toString('hex')
return usePublicKey ? crypto.publicEncrypt(key, Buffer.from(payloadString, 'utf8')).toString('hex') : crypto.privateEncrypt(key, Buffer.from(payloadString, 'utf8'))
} catch (error) {
throw UnknownError('encryption')
}
}

exports.decrypt = (encryptedString, privateKey) => {
if (typeof encryptedString !== 'string' || typeof privateKey !== 'string') throw Error("Error decrypting. Decrypted Text and Private Key should be in text format. Example usage: ` let decryptedText = QuickEncrypt.decrypt('asddd213d19jenacanscasn', 'the private RSA key in text format here'); ` ")
exports.decrypt = (encryptedString, key,usePrivateKey=true) => {
if (typeof encryptedString !== 'string' || typeof key !== 'string') throw Error("Error decrypting. Decrypted Text and Public/Private Key should be in text format. Example usage: ` let decryptedText = QuickEncrypt.decrypt('asddd213d19jenacanscasn', 'the private RSA key in text format here'); ` ")
try {
return crypto.privateDecrypt({ key: privateKey }, Buffer.from(encryptedString, 'hex')).toString()
return usePrivateKey ? crypto.privateDecrypt({ key }, Buffer.from(encryptedString, 'hex')).toString() : crypto.publicDecrypt({ key }, Buffer.from(encryptedString, 'hex')).toString()
} catch (error) {
throw UnknownError('decryption')
}
Expand Down