diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..db7bf8a --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,81 @@ +# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions + +name: Node CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + name: build, test, check coverage + runs-on: ubuntu-latest + strategy: + matrix: + node-version: [11, 12, 13] + steps: + - uses: actions/checkout@v2 + + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + + - name: install + run: npm ci + + - name: build + run: npm run build + + - name: test + run: npm test + + publication: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - uses: EndBug/version-check@v1 + id: check + + - name: check version changes + if: steps.check.outputs.changed == 'true' + run: 'echo "Version change found! New version: ${{ steps.check.outputs.version }} (${{ steps.check.outputs.type }})"' + + - uses: actions/setup-node@v1 + if: steps.check.outputs.changed == 'true' + with: + node-version: 12 + registry-url: https://registry.npmjs.org/ + + - name: install + if: steps.check.outputs.changed == 'true' + run: npm ci + + - name: build + if: steps.check.outputs.changed == 'true' + run: npm run build + + - name: test + if: steps.check.outputs.changed == 'true' + run: npm test + + - name: create code coverage report + if: steps.check.outputs.changed == 'true' + run: npm run coverage + + - name: send report to coveralls.io + if: steps.check.outputs.changed == 'true' + uses: coverallsapp/github-action@master + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + + - name: publish to NPM + if: steps.check.outputs.changed == 'true' + run: npm publish + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index f47fa05..153b79b 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,8 @@ node_modules/ .vscode # IntelliJ -.idea/ +.idea + +# MYC output +.nyc_output +coverage/* diff --git a/.npmignore b/.npmignore index b21863c..4e85723 100644 --- a/.npmignore +++ b/.npmignore @@ -20,3 +20,16 @@ npm-debug.log* # Visual Studio Code .vscode + +# IntelliJ +.idea + +# MYC output +.nyc_output +coverage/* + +# Travis +.travis.yml + +# GitHub +.github \ No newline at end of file diff --git a/README.md b/README.md index 7fa4d06..8b71e6a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,15 @@ +[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) +![Node CI](https://github.com/juanelas/bigint-crypto-utils/workflows/Node%20CI/badge.svg) +[![Coverage Status](https://coveralls.io/repos/github/juanelas/bigint-crypto-utils/badge.svg?branch=master)](https://coveralls.io/github/juanelas/bigint-crypto-utils?branch=master) # bigint-crypto-utils -Utils for working with cryptography using native JS ([ES-2020](https://tc39.es/ecma262/#sec-bigint-objects)) implementation of BigInt. It includes some extra functions to work with modular arithmetic along with secure random numbers and a fast strong probable prime generator/tester (parallelized multi-threaded Miller-Rabin primality tests if workers are supported). It can be used by any [Web Browser or webview supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) and with Node.js (>=10.4.0). +Arbitrary precision modular arithmetic, cryptographically secure random numbers and strong probable prime generation/testing. + +It relies on the native JS implementation of ([BigInt](https://tc39.es/ecma262/#sec-bigint-objects)). It can be used by any [Web Browser or webview supporting BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Browser_compatibility) and with Node.js (>=10.4.0). The bundles can be imported directly by the browser or in Angular projects, React apps, Node.js, etc. + +Secure random numbers are generated using the native crypto implementation of the browsers ([Web Cryptography API](https://w3c.github.io/webcrypto/)) or [Node.js Crypto](https://nodejs.org/dist/latest/docs/api/crypto.html)). Strong probable prime generation and testing use Miller-Rabin primality tests and are automatically sped up using parallel workers both in browsers and Node.js. > The operations supported on BigInts are not constant time. BigInt can be therefore **[unsuitable for use in cryptography](https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html).** Many platforms provide native support for cryptography, such as [Web Cryptography API](https://w3c.github.io/webcrypto/) or [Node.js Crypto](https://nodejs.org/dist/latest/docs/api/crypto.html). @@ -16,9 +23,7 @@ bigint-crypto-utils can be imported to your project with `npm`: npm install bigint-crypto-utils ``` -NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. - -For web browsers, you can also directly download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.iife.js) or the [ES6 bundle module](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.mod.js) from GitHub. +NPM installation defaults to the ES6 module for browsers and the CJS one for Node.js. For web browsers, you can also directly download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.iife.js) or the [ESM bundle](https://raw.githubusercontent.com/juanelas/bigint-crypto-utils/master/lib/index.browser.bundle.mod.js) from the repository. ## Usage examples @@ -29,13 +34,30 @@ Import your module as : const bigintCryptoUtils = require('bigint-crypto-utils') ... // your code here ``` - - JavaScript native or TypeScript project + - JavaScript native or TypeScript project (including React and Angular JS) ```javascript import * as bigintCryptoUtils from 'bigint-crypto-utils' ... // your code here ``` - > BigInt is [ES-2020](https://tc39.es/ecma262/#sec-bigint-objects). In order to use it with TypeScript you should set `lib` (and probably also `target` and `module`) to `esnext` in `tsconfig.json`. - - JavaScript native browser ES6 mod + BigInt is [ES-2020](https://tc39.es/ecma262/#sec-bigint-objects). In order to use it with TypeScript you should set `lib` (and probably also `target` and `module`) to `esnext` in `tsconfig.json`. + `bigint-crypto-utils` **CANNOT BE POLYFILLED** to suport older browsers. If you are using webpack/babel to create your production bundles, you should target only the most modern browsers. For instance, for **React** apps created with [`create-react-app`](https://create-react-app.dev/), you should edit your `package.json` and modify the `browserList` so that it only targets the latest browsers (supporting the latest features): + ```json + "browserslist": { + "production": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } + ``` + Also, notice that BigInt is [ES-2020](https://tc39.es/ecma262/#sec-bigint-objects). In order to use it with TypeScript you should set `lib` (and probably also `target` and `module`) to `esnext` in `tsconfig.json`. + + - JavaScript native browser ES module ```html