# RSAForPostman **Repository Path**: tools_czl/RSAForPostman ## Basic Information - **Project Name**: RSAForPostman - **Description**: add rsa encryt for postman forked from loveiset/RSAForPostman - **Primary Language**: JavaScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-08-05 - **Last Updated**: 2024-01-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # RSAForPostman ## usage you can skip following setting steps, and using example code(pre-script) directly to test a RSA encrypt (see how to use it) ## reference this project is based on [forge Project](https://github.com/digitalbazaar/forge) ## how to build First you need to install node and git on your pc. ### clone forge and install ``` git clone https://github.com/digitalbazaar/forge.git cd /path/to/your/dir npm install ``` ### modify configure modify`webpack.config.js`, replace `umd` with `var` ```javascript const bundle = Object.assign({}, common, { output: { path: path.join(__dirname, 'dist'), filename: info.filenameBase + '.js', library: info.library || '[name]', libraryTarget: info.libraryTarget || 'var' } }); if(info.library === null) { delete bundle.output.library; } if(info.libraryTarget === null) { delete bundle.output.libraryTarget; } // optimized and minified bundle const minify = Object.assign({}, common, { output: { path: path.join(__dirname, 'dist'), filename: info.filenameBase + '.min.js', library: info.library || '[name]', libraryTarget: info.libraryTarget || 'var' }, devtool: 'cheap-module-source-map', plugins: [ new webpack.optimize.UglifyJsPlugin({ sourceMap: true, compress: { warnings: true }, output: { comments: false } //beautify: true }) ] }); ``` ### repack ``` npm run build ``` then in `dist` folder, we'll find a file named `forge.js`, that's what we need. ## how to use it because Postman can not load js file from local disk, so we put `forge.js` on a web server(**url on github seems not work, a local server may works or add a varible `forgeJS` manully**). **EDIT: using github raw format works, so you guys can using this example directly** ### pre-script ``` //download forgeJS from web and set varible if (!pm.globals.has("forgeJS")) { pm.sendRequest("http://10.52.16.143/logs/forge.js", function (err, res) { if (err) { console.log(err); } else { pm.globals.set("forgeJS", res.text()); } }) } else { eval(pm.globals.get("forgeJS")) const public_key = '-----BEGIN PUBLIC KEY-----\n' + 'MIGfMA0GCSq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PL9t8AjkkZs3Zb0+BvDOpqP8CQQDZhfh4\n' + '/c/Qzp4szj7+GXTZ1cmGwAuFo2/9uiumUAS3f19EpgoV9u9eyJ4gZPEBDvAjO961\n' + 'kAjdja4DAy4SbCXy\n' + '-----END PRIVATE KEY-----' //encrypt text "plaintext" var publicKey = forge.pki.publicKeyFromPem(public_key); var encryptedText = forge.util.encode64(publicKey.encrypt("plaintext", 'RSA-OAEP', { md: forge.md.sha1.create(), mgf1: { md: forge.md.sha1.create() } })); console.log("encrypted text:" + encryptedText); // decrypt text var privateKey = forge.pki.privateKeyFromPem(private_key); var decryptedText = privateKey.decrypt(forge.util.decode64(encryptedText), 'RSA-OAEP', { md: forge.md.sha1.create(), mgf1: { md: forge.md.sha1.create() } }); console.log("dectypted text:" + decryptedText); } ``` you can use rsa encrypt in postman now! ![example](example.png) ## something else ### about web server if you don't want to use a web server, you an simply create a globle varible named `forgeJS` and copy content in `forge.js` to the value(**postman may be very slow because the file is big**) ### how to delete the varible because of `forgeJS` varible, it may be very diffcuilt to edit other varible in postman, to solve this, you can delete the `forgeJS` varible ``` pm.globals.unset("forgeJS"); ``` ### document about RSA encrypt [https://github.com/digitalbazaar/forge](https://github.com/digitalbazaar/forge) ### ps this thing is a temporary solution, we all hope postman can support RSA encrypt oneday officially.