Skip to content

Commit

Permalink
fix(resolve issue with enctypt and descrpt): resolve issue
Browse files Browse the repository at this point in the history
  • Loading branch information
miladezzat committed May 22, 2024
1 parent a5af64b commit 8d01c97
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import * as crypto from 'crypto';
import { decode, encode } from './utils/helpers';
import {
parametersOfDecrypt,
parametersOfDecryptPublic,
parametersOfEncrypt,
parametersOfEncryptPrivate,
returnCreateKeys,
} from './utils/types';

Expand Down Expand Up @@ -30,14 +32,13 @@ class NodeRSA {
/**
*
* @param {Object} args
* @param {String} args.privateKey
* @param {String} args.publicKey
* @param {String} args.text the text that you need to encrypt
* @deprecated
* @returns {String}
*/
public encryptStringWithRsaPublicKey(args: parametersOfEncrypt): string {
const { text, privateKey = this.privateKey } = args;
const publicKeyDecoded: string = decode(this.convertKetToBase64(privateKey as string));
const { text, publicKey = this.publicKey } = args;
const publicKeyDecoded: string = decode(this.convertKetToBase64(publicKey as string));

const buffer: Buffer = Buffer.from(text);
const encrypted: Buffer = crypto?.publicEncrypt(publicKeyDecoded, buffer);
Expand All @@ -50,13 +51,12 @@ class NodeRSA {
* @param {Object} args
* @param {String} args.privateKey
* @param {String} args.text the text that you need to decrypt
* @deprecated
* @returns {String}
*/
public decryptStringWithRsaPrivateKey(args: parametersOfDecrypt): string {
const { text, publicKey = this.publicKey } = args;
const { text, privateKey = this.privateKey } = args;

const publicKeyDecoded: string = decode(this.convertKetToBase64(publicKey as string));
const publicKeyDecoded: string = decode(this.convertKetToBase64(privateKey as string));

const buffer: Buffer = Buffer.from(text, 'base64');
const decrypted: Buffer = crypto?.privateDecrypt(publicKeyDecoded as string, buffer);
Expand All @@ -72,7 +72,7 @@ class NodeRSA {
*
* @returns {String}
*/
public encrypt(args: parametersOfEncrypt): string {
public encrypt(args: parametersOfEncryptPrivate): string {
const { text, privateKey = this.privateKey } = args;
const publicKeyDecoded: string = decode(this.convertKetToBase64(privateKey as string));

Expand All @@ -90,7 +90,7 @@ class NodeRSA {
*
* @returns {String}
*/
public decrypt(args: parametersOfDecrypt): string {
public decrypt(args: parametersOfDecryptPublic): string {
const { text, publicKey = this.publicKey } = args;

const publicKeyDecoded: string = decode(this.convertKetToBase64(publicKey as string));
Expand Down
22 changes: 16 additions & 6 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
export type returnCreateKeys = {
privateKey:string,
publicKey:string
privateKey: string,
publicKey: string
}

export type parametersOfEncrypt = {
text: string,
privateKey?: string
text: string,
publicKey?: string
}

export type parametersOfDecrypt = {
text: string,
publicKey?: string
text: string,
privateKey?: string
}

export type parametersOfEncryptPrivate = {
text: string,
privateKey?: string
}

export type parametersOfDecryptPublic = {
text: string,
publicKey?: string
}
8 changes: 4 additions & 4 deletions tests/functionalty.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Functionality', () => {
const encryptedText = nodeRSA.encryptStringWithRsaPublicKey({
text,
// eslint-disable-next-line max-len
privateKey: '-----BEGIN PUBLIC KEY-----\nMIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgEzCqLqxWSiOTkWFQaPhY6X+qom8\nzzbidCpNu/zxwTieMvnBE4yPCeSRwJMFjJD2UGr7I/WunOsx+rAxYbzoMELw6TdZ\naaKygSLfkncUmbL6MQ1ZCSQQR6weaQj8VeYKNaA3QSqJYXCRPky6LI/o73brTCpE\nsWuVWp577q2PbTDbAgMBAAE=\n-----END PUBLIC KEY-----',
publicKey: '-----BEGIN PUBLIC KEY-----\nMIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgEzCqLqxWSiOTkWFQaPhY6X+qom8\nzzbidCpNu/zxwTieMvnBE4yPCeSRwJMFjJD2UGr7I/WunOsx+rAxYbzoMELw6TdZ\naaKygSLfkncUmbL6MQ1ZCSQQR6weaQj8VeYKNaA3QSqJYXCRPky6LI/o73brTCpE\nsWuVWp577q2PbTDbAgMBAAE=\n-----END PUBLIC KEY-----',
});
encryptedString = encryptedText;
expect(encryptedText).to.be.a('string').and.not.equal(text);
Expand All @@ -44,7 +44,7 @@ describe('Functionality', () => {

const encryptedText = nodeRSA.decryptStringWithRsaPrivateKey({
text: encryptedString,
publicKey: key,
privateKey: key,
});

expect(encryptedText).to.be.a('string').and.equal(text);
Expand All @@ -64,15 +64,15 @@ describe('Functionality', () => {

const encryptedText = nodeRSA.encryptStringWithRsaPublicKey({
text: 'hello world',
privateKey: publicKey,
publicKey,
});

encryptedString = encryptedText;
expect(encryptedText).to.be.a('string').and.not.equal(text);

const decryptText = nodeRSA.decryptStringWithRsaPrivateKey({
text: encryptedText,
publicKey: privateKey,
privateKey,
});

expect(decryptText).be.a.string('hello world');
Expand Down

0 comments on commit 8d01c97

Please sign in to comment.