├── .eslintrc ├── .github ├── FUNDING.yml ├── SECURITY.md └── workflows │ ├── node-aught.yml │ ├── node-pretest.yml │ ├── node-tens.yml │ ├── rebase.yml │ └── require-allow-edits.yml ├── .gitignore ├── .npmrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test ├── fixtures.json └── index.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | 4 | "extends": "@ljharb", 5 | 6 | "rules": { 7 | "func-style": "off", 8 | }, 9 | } 10 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [ljharb] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: npm/browserify-rsa 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security 2 | 3 | Please email [@ljharb](https://github.com/ljharb) or see https://tidelift.com/security if you have a potential security vulnerability to report. 4 | -------------------------------------------------------------------------------- /.github/workflows/node-aught.yml: -------------------------------------------------------------------------------- 1 | name: 'Tests: node.js < 10' 2 | 3 | on: [pull_request, push] 4 | 5 | jobs: 6 | tests: 7 | uses: ljharb/actions/.github/workflows/node.yml@main 8 | with: 9 | range: '>= 0.10 < 10' 10 | type: minors 11 | command: npm run tests-only 12 | -------------------------------------------------------------------------------- /.github/workflows/node-pretest.yml: -------------------------------------------------------------------------------- 1 | name: 'Tests: pretest/posttest' 2 | 3 | on: [pull_request, push] 4 | 5 | jobs: 6 | tests: 7 | uses: ljharb/actions/.github/workflows/pretest.yml@main 8 | -------------------------------------------------------------------------------- /.github/workflows/node-tens.yml: -------------------------------------------------------------------------------- 1 | name: 'Tests: node.js >= 10' 2 | 3 | on: [pull_request, push] 4 | 5 | jobs: 6 | tests: 7 | uses: ljharb/actions/.github/workflows/node.yml@main 8 | with: 9 | range: '>= 10' 10 | type: minors 11 | command: npm run tests-only 12 | -------------------------------------------------------------------------------- /.github/workflows/rebase.yml: -------------------------------------------------------------------------------- 1 | name: Automatic Rebase 2 | 3 | on: [pull_request_target] 4 | 5 | jobs: 6 | _: 7 | uses: ljharb/actions/.github/workflows/rebase.yml@main 8 | secrets: 9 | token: ${{ secrets.GITHUB_TOKEN }} 10 | -------------------------------------------------------------------------------- /.github/workflows/require-allow-edits.yml: -------------------------------------------------------------------------------- 1 | name: Require “Allow Edits” 2 | 3 | on: [pull_request_target] 4 | 5 | jobs: 6 | _: 7 | name: "Require “Allow Edits”" 8 | 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: ljharb/require-allow-edits@main 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | 3 | npm-debug.log 4 | 5 | # Only apps should have lockfiles 6 | npm-shrinkwrap.json 7 | package-lock.json 8 | yarn.lock 9 | 10 | .npmignore 11 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | allow-same-version=true 3 | message=v%s 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [v4.1.1](https://github.com/browserify/browserify-rsa/compare/v4.1.0...v4.1.1) - 2024-09-26 9 | 10 | ### Commits 11 | 12 | - Only apps should have lockfiles [`9d8732b`](https://github.com/browserify/browserify-rsa/commit/9d8732b17f25a24aa1c367bf244c1e95f3a4c485) 13 | - [eslint] switch to eslint [`7e391c4`](https://github.com/browserify/browserify-rsa/commit/7e391c4dec3407ca1a7af4284f2c5f8c4a745ff2) 14 | - [meta] add `auto-changelog` [`2e3fdef`](https://github.com/browserify/browserify-rsa/commit/2e3fdeff72d0695b7d9b4b6dc056404ad1d9da96) 15 | - [Tests] migrate from travis to GHA [`9d29368`](https://github.com/browserify/browserify-rsa/commit/9d29368f3b76a808cc7575533b135b113c96a219) 16 | - [Tests] improve test organization [`21b8fe5`](https://github.com/browserify/browserify-rsa/commit/21b8fe58085fed8f0b8bd2f111f25079152fa905) 17 | - [Tests] `crypto.privateDecrypt` is only in node 0.12+ [`52a4253`](https://github.com/browserify/browserify-rsa/commit/52a4253a5f5b19401bab450f2f29e68480c56077) 18 | - [meta] add `npmignore` and `safe-publish-latest` [`bd5d1c2`](https://github.com/browserify/browserify-rsa/commit/bd5d1c222daafeb2121b2b79b8feb536b565f4e7) 19 | - [Fix] use `safe-buffer` [`28d2560`](https://github.com/browserify/browserify-rsa/commit/28d256045487adb3b88f7b958fa6728702f9f4f6) 20 | - [meta] add `exports` [`50464d7`](https://github.com/browserify/browserify-rsa/commit/50464d70d37a1c771268cd336de6424eafa4f42d) 21 | - [Deps] update `bn.js`, `randombytes` [`d833e49`](https://github.com/browserify/browserify-rsa/commit/d833e49e520f502cf8dfce1fe159041b6a1c6005) 22 | - [meta] raise `engines.node` to 0.10, due to `parse-asn1` [`063d27b`](https://github.com/browserify/browserify-rsa/commit/063d27b4476dd7d833a57014cf9c6c786005c5dd) 23 | - [Dev Deps] update `parse-asn1`, `tape` [`7d19782`](https://github.com/browserify/browserify-rsa/commit/7d197823631bcb274efe52970af0f42188b9b7fe) 24 | - [meta] add missing `engines.node` [`a2e5e94`](https://github.com/browserify/browserify-rsa/commit/a2e5e9411b902439afb1fd9e61d853469a6d1a59) 25 | - [meta] correct git URL [`f235983`](https://github.com/browserify/browserify-rsa/commit/f23598305d24c2fb1c76f582b1cb22ffba20f374) 26 | - [Dev Deps] add missing deps from 2e3fdef [`5ea31da`](https://github.com/browserify/browserify-rsa/commit/5ea31da9edf0ec7788c0361bc1a8a90b040f6fe2) 27 | - [meta] add `sideEffects` flag [`4a2d873`](https://github.com/browserify/browserify-rsa/commit/4a2d8730ea1725651103b0970a6af0b3871fd9be) 28 | 29 | ## [v4.1.0](https://github.com/browserify/browserify-rsa/compare/v4.0.1...v4.1.0) - 2020-11-12 30 | 31 | ### Merged 32 | 33 | - bump bn.js to 5.0.0 [`#13`](https://github.com/browserify/browserify-rsa/pull/13) 34 | - update loop in ger [`#10`](https://github.com/browserify/browserify-rsa/pull/10) 35 | 36 | ### Commits 37 | 38 | - update tests [`7b34cc7`](https://github.com/browserify/browserify-rsa/commit/7b34cc75cda375fcfb2577f6c2334b273728a632) 39 | - update index.js [`6c2c290`](https://github.com/browserify/browserify-rsa/commit/6c2c290a5b3af41f8a2f68ca7f8d340bfb6d38ec) 40 | - update package.json [`9f906f8`](https://github.com/browserify/browserify-rsa/commit/9f906f8f76fcbca9f0c0a412f76eab03b01976cc) 41 | - update README.md [`f55b31a`](https://github.com/browserify/browserify-rsa/commit/f55b31aa0d15a7bf1dd6dd222314fbd0d7a5fc2e) 42 | - update travis config [`7342836`](https://github.com/browserify/browserify-rsa/commit/73428369fe9d976f41cbbac2b60421d65ea60516) 43 | - add .gitignore [`d0a4613`](https://github.com/browserify/browserify-rsa/commit/d0a4613bb72d8874171cf9d666ea272cc3c7bc20) 44 | 45 | ## [v4.0.1](https://github.com/browserify/browserify-rsa/compare/v4.0.0...v4.0.1) - 2016-02-26 46 | 47 | ### Merged 48 | 49 | - package.json: fix repository URL [`#8`](https://github.com/browserify/browserify-rsa/pull/8) 50 | 51 | ## [v4.0.0](https://github.com/browserify/browserify-rsa/compare/v3.0.0...v4.0.0) - 2015-10-29 52 | 53 | ### Merged 54 | 55 | - bump bn.js and parse-asn1 [`#6`](https://github.com/browserify/browserify-rsa/pull/6) 56 | 57 | ### Commits 58 | 59 | - fix dev dep [`3acf6c7`](https://github.com/browserify/browserify-rsa/commit/3acf6c7729e254429cb6485747ebf54fa9ccfb29) 60 | 61 | ## [v3.0.0](https://github.com/browserify/browserify-rsa/compare/v2.0.1...v3.0.0) - 2015-10-26 62 | 63 | ### Merged 64 | 65 | - bump bn-js [`#5`](https://github.com/browserify/browserify-rsa/pull/5) 66 | - Adding license and updating package.json [`#4`](https://github.com/browserify/browserify-rsa/pull/4) 67 | 68 | ### Commits 69 | 70 | - Add LICENSE [`3ee2b4d`](https://github.com/browserify/browserify-rsa/commit/3ee2b4d3428c9504860e62d7c070d75dd2710f48) 71 | - package: adds description [`af8556a`](https://github.com/browserify/browserify-rsa/commit/af8556a0d5355f6921dbdfee4bf4ff1190b05c10) 72 | 73 | ## [v2.0.1](https://github.com/browserify/browserify-rsa/compare/v2.0.0...v2.0.1) - 2015-05-20 74 | 75 | ### Merged 76 | 77 | - update bn.js [`#3`](https://github.com/browserify/browserify-rsa/pull/3) 78 | 79 | ### Commits 80 | 81 | - Fix badge URL to point to crypto-browserify org [`823888a`](https://github.com/browserify/browserify-rsa/commit/823888a93b513724a43e0d56bfe9c951e84c0e60) 82 | - update readme [`0f404ac`](https://github.com/browserify/browserify-rsa/commit/0f404ac2235c4fa4a2124a29cb6cd9a6dbb25d22) 83 | 84 | ## [v2.0.0](https://github.com/browserify/browserify-rsa/compare/v1.2.0...v2.0.0) - 2015-01-28 85 | 86 | ## [v1.2.0](https://github.com/browserify/browserify-rsa/compare/v1.1.1...v1.2.0) - 2015-01-28 87 | 88 | ### Commits 89 | 90 | - modularize [`de640bf`](https://github.com/browserify/browserify-rsa/commit/de640bff7f5fcdc1ed68ce420d972c5c4005f70f) 91 | 92 | ## v1.1.1 - 2015-01-06 93 | 94 | ### Commits 95 | 96 | - left pad here and clean up tests [`240e16c`](https://github.com/browserify/browserify-rsa/commit/240e16c3b116dca1a63e463f494bd3447abb3b8a) 97 | - first [`ceb731f`](https://github.com/browserify/browserify-rsa/commit/ceb731f7d56e4aba5440b99709dda6c2cb38b5dc) 98 | - Update bn.js [`dfcf387`](https://github.com/browserify/browserify-rsa/commit/dfcf38757fc28a7d929a208f1775c59c9ab7f008) 99 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2016 Calvin Metcalf & contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # browserify-rsa 2 | 3 | [![NPM Package](https://img.shields.io/npm/v/browserify-rsa.svg?style=flat-square)](https://www.npmjs.org/package/browserify-rsa) 4 | [![Build Status](https://img.shields.io/travis/crypto-browserify/browserify-rsa.svg?branch=master&style=flat-square)](https://travis-ci.org/crypto-browserify/browserify-rsa) 5 | [![Dependency status](https://img.shields.io/david/crypto-browserify/browserify-rsa.svg?style=flat-square)](https://david-dm.org/crypto-browserify/browserify-rsa#info=dependencies) 6 | 7 | RSA private decryption/signing using chinese remainder and blinding. 8 | 9 | ## API 10 | 11 | Give it a message as a Buffer and a private key (as decoded by `ASN.1`) and it returns encrypted data as a Buffer. 12 | 13 | ## LICENSE 14 | 15 | MIT 16 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var BN = require('bn.js'); 4 | var randomBytes = require('randombytes'); 5 | var Buffer = require('safe-buffer').Buffer; 6 | 7 | function getr(priv) { 8 | var len = priv.modulus.byteLength(); 9 | var r; 10 | do { 11 | r = new BN(randomBytes(len)); 12 | } while (r.cmp(priv.modulus) >= 0 || !r.umod(priv.prime1) || !r.umod(priv.prime2)); 13 | return r; 14 | } 15 | 16 | function blind(priv) { 17 | var r = getr(priv); 18 | var blinder = r.toRed(BN.mont(priv.modulus)).redPow(new BN(priv.publicExponent)).fromRed(); 19 | return { blinder: blinder, unblinder: r.invm(priv.modulus) }; 20 | } 21 | 22 | function crt(msg, priv) { 23 | var blinds = blind(priv); 24 | var len = priv.modulus.byteLength(); 25 | var blinded = new BN(msg).mul(blinds.blinder).umod(priv.modulus); 26 | var c1 = blinded.toRed(BN.mont(priv.prime1)); 27 | var c2 = blinded.toRed(BN.mont(priv.prime2)); 28 | var qinv = priv.coefficient; 29 | var p = priv.prime1; 30 | var q = priv.prime2; 31 | var m1 = c1.redPow(priv.exponent1).fromRed(); 32 | var m2 = c2.redPow(priv.exponent2).fromRed(); 33 | var h = m1.isub(m2).imul(qinv).umod(p).imul(q); 34 | return m2.iadd(h).imul(blinds.unblinder).umod(priv.modulus).toArrayLike(Buffer, 'be', len); 35 | } 36 | crt.getr = getr; 37 | 38 | module.exports = crt; 39 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-rsa", 3 | "version": "4.1.1", 4 | "description": "RSA for browserify", 5 | "bugs": { 6 | "url": "https://github.com/crypto-browserify/browserify-rsa/issues" 7 | }, 8 | "license": "MIT", 9 | "main": "index.js", 10 | "exports": { 11 | ".": "./index.js", 12 | "./package.json": "./package.json" 13 | }, 14 | "sideEffects": false, 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/crypto-browserify/browserify-rsa.git" 18 | }, 19 | "scripts": { 20 | "prepack": "npmignore --auto --commentLines=autogenerated", 21 | "prepublish": "not-in-publish || npm run prepublishOnly", 22 | "prepublishOnly": "safe-publish-latest", 23 | "lint": "eslint --ext=js,mjs .", 24 | "pretest": "npm run lint", 25 | "test": "npm run tests-only", 26 | "tests-only": "tape 'test/**/*.js'", 27 | "posttest": "npx npm@'>=10.2' audit --production", 28 | "version": "auto-changelog && git add CHANGELOG.md", 29 | "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" 30 | }, 31 | "dependencies": { 32 | "bn.js": "^5.2.1", 33 | "randombytes": "^2.1.0", 34 | "safe-buffer": "^5.2.1" 35 | }, 36 | "devDependencies": { 37 | "@ljharb/eslint-config": "^21.1.1", 38 | "auto-changelog": "^2.5.0", 39 | "encoding": "^0.1.13", 40 | "eslint": "=8.8.0", 41 | "in-publish": "^2.0.1", 42 | "npmignore": "^0.3.1", 43 | "parse-asn1": "^5.1.7", 44 | "safe-publish-latest": "^2.0.0", 45 | "tape": "^5.9.0" 46 | }, 47 | "engines": { 48 | "node": ">= 0.10" 49 | }, 50 | "auto-changelog": { 51 | "output": "CHANGELOG.md", 52 | "template": "keepachangelog", 53 | "unreleased": false, 54 | "commitLimit": false, 55 | "backfillLimit": false, 56 | "hideCredit": true 57 | }, 58 | "publishConfig": { 59 | "ignore": [ 60 | ".github/workflows", 61 | "test", 62 | ".eslintrc", 63 | ".github" 64 | ] 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /test/fixtures.json: -------------------------------------------------------------------------------- 1 | [ 2 | "2d2d2d2d2d424547494e2050524956415445204b45592d2d2d2d2d0a4d494943647749424144414e42676b71686b6947397730424151454641415343416d457767674a6441674541416f4742414b756c55545a3842317163635a38630a44585247535930386757384b764c6c63787878474334675a484e543343425546386e3552344b453330615a79595a2f727473515a7530356a755a4a78614a30710a6d62653735646c5135642b586339424d586551672f4d70545a773554414e374f4964475959704642652b31504c5a367745666a6b59724d714d55636671324c710a68544c64416276424a6e755263595a4c716d42654f51384654724b7241674d4241414543675945416e6b485262455055332f57495353517250333669794362320a532f53425a774b6b7a6d764372427844576850654473777039632f324a593736724e57664c7a793869586755473857557a76486a653631516833676d42634b650a62556154476c34567938486131594241446f3552665272646d3046453474766776752f546b7146717042425a7765753534323835686b357a6c47376e2f4437590a646e4e58557075354d6c4e623578336757306b43515144554c2f2f637763585578592f6576614a50346a53652b5a7745515a6f2b7a58524c695055756c426f560a6177323843564d757864677771416f315831494b65665065556166375251753867434b61526e704775457558416b45417a785a54664d6d766d435544496577340a35476b36624b3236355851576468636769713235346c7042474f596d446a397943453779412b7a6d415351774d73585464514f6931684f434579725875534a350a632b2b4544514a4146683357726e7a6f455042797559584d6d45543874534652574d51357670674e716833686148523562346755433268786169756e43424e4c0a315270565939416f55694479774763472f5350683933436e4b42336e69774a42414b503741747369665a6756587469697a4234614d5468546a565961535a727a0a44304b6739447548796c706b4443686d467537375447724e55516741567559746668622f6252626c56612f4630684a3465514854334a554351425654363874620a4f6752556b30615039744333303231564e383258362b6b6c6f7753514e386f425058382b546644575355696c702f2b6a3234486b792b5a3239446f3779522f520a7175746e4c39324376426c564c56343d0a2d2d2d2d2d454e442050524956415445204b45592d2d2d2d2d0a", 3 | "2d2d2d2d2d424547494e205253412050524956415445204b45592d2d2d2d2d0a4d4949435641494241414a2f4f77737762466f2f757943386c7447662f794131412b6756354947646e4167506255534933477a624843412b782b544c472f744c0a76625277337231736d7070592f6a6b6b70695657314572534d754e307569787035676237385a39724831587057623557576770335761592f3945484d6a4d644f0a6b512f394c565a7652766c2f4d2f4669366f77502b712b616d4a493142456a454359666268474c33726d6c5664713471586334305177494441514142416e38490a565a3042506f414f68794633334b464d4878793872323866735667784a5559674d334e715167647634664661774359586a684a7a3964755535594a47464a474a0a57554765486c6b7959466c70693466336d377459374a61776d51555742304d4e536f4b48493363674458342f7466424e386e692b634f3065536f5235637a42590a4573414842553437703161774e46414877642b5a457576394834526d4d6e37703237397251547470416b4148334e7173322f7672524632635a554e34664958660a347848735142427955617947713861334a305547615346577636387a54554b466865727239755a6f744e70374e4a346a425869415277307138646f63585547310a416b4148676d4f4b486f4f5274416d696b71706d46454a5a4f7473584d614c43496d3445737a506f356369596f4c4d42635669743039416469516c74375a4a4c0a445930327376553162306167435a39376b446b6d48446b58416b414361384d394a454c7544732f502f76494759446b4d566174494666573662574630326546470a746157774d71436353457357766277307871597433346a5552704e62436a6d4379515677596641772f2b544c68503964416b414677526a64776a77333771706a0a646467316d4e697533376237737746786d6b694d4f585a5278614e4e736662353641313452704e337a6f6233516447557962476f644d494b5446626d552f6c750a436a71417861664a416b41473279663652576277464957664d7974375759436830566147424363677935373441696e566965456f335a5a7946664336332b786d0a33756f614e7934694c6f4a763447436a7155427a335a666356614f2f444457470a2d2d2d2d2d454e44205253412050524956415445204b45592d2d2d2d2d0a", 4 | "2d2d2d2d2d424547494e205253412050524956415445204b45592d2d2d2d2d0a4d4949456a77494241414b422f6779376d6a615767506546645659445a5752434139424e69763370506230657332372b464b593068737a4c614f7734374578430a744157704473483438545841667948425977424c67756179666b344c4749757078622b43474d62526f337845703043626659314a62793236543976476a5243310a666f484444554a4738347561526279487161663469367a74346756522b786c4145496a6b614641414b38634f6f58415431435671474c4c6c6a554363684c38500a6a61486a2f7972695a2f53377264776c49334c6e41427877776d4c726d522f7637315774706d4f2f614e47384e2b31706f2b5177616768546b79513539452f5a0a7641754f6b4657486f6b32712f523650594161326a645a397a696d3046714f502b6e6b5161454452624246426d4271547635664647666b32577341664b662f520a47302f5646642b5a654d353235315465547658483639356e6c53476175566c3941674d42414145436766344c725748592f6c35346f7554685a577676627275670a70667a36734a583267396c3779586d576c455773504543566f2f375355627059467074364f5a7939397a53672b494b624771574b6664686f4b725477495674430a4c30595a304e6c6d646e414e53497a30726f785147375a786b4c352b764853772f506d443978345577662b437a38684154436d4e42763171633630646b7975570a34434c71653732716154695657526f4f316961675167684e634c6f6f36765379363545784c614344545068613779753276773468465a705769456a57346478660a7246644c696978353242433836596c416c784d452f724c6738494a5676696c62796f39615764586d784f6155544c527636506b4644312f6756647738563951720a534c4e39466c4b326b6b6a695830647a6f6962765a7733744d6e74337979644178305838372b734d5256616843316270336b56507a3448793045575834514a2f0a504d33317647697549546b324e43643531445874314c746e324f503546614a536d4361456a6830586b5534716f7559796a585774384275364254436c327675610a466730556a6939432b496b504c6d61554d624d494f7761546b386357714c74685378734c6537304a354f6b477267664b554d2f772b4248483150742f506a7a6a0a432b2b6c306b6946614f5644566141563947704c504c43426f4b2f50433952622f72784d4d6f43434e774a2f4e5a756564496e793277334c4d69693737682f540a7a53766572674e47686a5936526e7661386c4c584a36646c726b6350417970733367577778716a344e5230542b474d3062445550564c62374d303758563753580a7637564a476d35324a625247774d3173732b72385854544e656d65476b2b5752784737546774734d715947584c66423851786b2f66352f4d63633030546c38750a7758464e7366784a786d7436416273547233673336774a2f49684f6e69627a3941642b6e63686c426e4e3351655733434b48717a61523138766f717674566d320a6b4a66484b31357072482f7353476d786d6945476772434a545a78744462614e434f372f56426a6e4b756455554968434177734c747571302f7a7562397641640a384731736366497076357161534e7a6d4b6f5838624f77417276725336775037794b726354737557496c484438724a5649374945446e516f5470354738664b310a68774a2f4d4968384d35763072356455594576366f494a5747636c65364148314a6d73503557496166677137325a32323838704863434648774e59384467394a0a3736517377564c6e556850546c6d6d33454f4f50474574616d32694144357230416679746c62346c624e6f51736a32737a65584f4e4458422b366f7565616a680a564e454c55723848635350356c677a525a6a4a57366146497a6a394c44526d516e55414f6a475358564f517445774a2f4d43515a374e2f763464494b654452410a3864385545785a332b674748756d7a697a7447524a30745172795a483250616b50354937562b316c377145556e4a3263336d462b65317634314570394c4376680a627a72504b773964786831386734622b37624d707357506e7372614b6836697078633761614f615a5630447867657a347a635a753050316f6c4f30634e334b4d0a6e784a305064733352386241684e43446453324a5a61527035513d3d0a2d2d2d2d2d454e44205253412050524956415445204b45592d2d2d2d2d0a" 5 | ] 6 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var crypto = require('crypto'); 4 | var constants = require('constants'); 5 | var parseKey = require('parse-asn1'); 6 | var BN = require('bn.js'); 7 | var Buffer = require('safe-buffer').Buffer; 8 | var test = require('tape'); 9 | 10 | var crt = require('../'); 11 | 12 | var fixtures = require('./fixtures'); 13 | 14 | test('browserify-rsa', function (t) { 15 | fixtures.forEach(function (fixture, i) { 16 | var key = new Buffer(fixture, 'hex'); 17 | var priv = parseKey(key); 18 | 19 | t.test('fixture ' + (i + 1), function (st) { 20 | var r; 21 | 22 | for (var j1 = 1; j1 < 31; ++j1) { 23 | r = crt.getr(priv); 24 | st.equal( 25 | String(r.gcd(priv.modulus)), 26 | '1', 27 | 'r is coprime with n ' + (i + 1) + ' run ' + j1 28 | ); 29 | } 30 | 31 | st.test('compared to node encryption', { skip: !crypto.privateDecrypt }, function (s2t) { 32 | var len = priv.modulus.byteLength(); 33 | for (var j2 = 1; j2 < 41; ++j2) { 34 | do { 35 | r = new BN(crypto.randomBytes(len)); 36 | } while (r.cmp(priv.modulus) >= 0); 37 | var buf = r.toArrayLike(Buffer, 'be'); 38 | if (buf.byteLength < priv.modulus.byteLength()) { 39 | var tmp = new Buffer(priv.modulus.byteLength() - buf.byteLength); 40 | tmp.fill(0); 41 | buf = Buffer.concat([tmp, buf]); 42 | } 43 | var nodeEncrypt = crypto.privateDecrypt({ 44 | padding: constants.RSA_NO_PADDING, 45 | key: key 46 | }, buf).toString('hex'); 47 | s2t.equal( 48 | crt(buf, priv).toString('hex'), 49 | nodeEncrypt, 50 | 'round trip key ' + (i + 1) + ' run ' + j2 + ' equal encrypts' 51 | ); 52 | } 53 | 54 | s2t.end(); 55 | }); 56 | 57 | st.end(); 58 | }); 59 | }); 60 | }); 61 | --------------------------------------------------------------------------------