From 4758f28ef07cefff730141d42740fa6c595ef367 Mon Sep 17 00:00:00 2001 From: juanelas Date: Mon, 6 Apr 2020 14:17:24 +0200 Subject: [PATCH] shorter but better readme --- README.md | 108 +++++++++++++++++++------------------ src/doc/readme-template.md | 108 +++++++++++++++++++------------------ 2 files changed, 110 insertions(+), 106 deletions(-) diff --git a/README.md b/README.md index 6d286f7..ec03ed2 100644 --- a/README.md +++ b/README.md @@ -22,75 +22,77 @@ For web browsers, you can also directly download the [IIFE file](https://raw.git ## Usage examples -### Node.js: +Import your module as : + + - Node.js + ```javascript + const bigintCryptoUtils = require('bigint-crypto-utils') + ... // your code here + ``` + - Javascript native project + ```javascript + import * as bigintCryptoUtils from 'bigint-crypto-utils' + ... // your code here + ``` + - Javascript native browser ES6 mod + ```html + + import as bcu from 'bigint-crypto-utils' + ... // your code here + ``` + - Javascript native browser IIFE + ```html + + + - TypeScript + ```typescript + 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 -const bigintCryptoUtils = require("bigint-crypto-utils"); - /* Stage 3 BigInts with value 666 can be declared as BigInt('666') or the shorter new no-so-linter-friendly syntax 666n. Notice that you can also pass a number, e.g. BigInt(666), but it is not recommended since values over 2**53 - 1 won't be safe but no warning will be raised. */ -let a = BigInt("5"); -let b = BigInt("2"); -let n = BigInt("19"); +const a = BigInt('5') +const b = BigInt('2') +const n = BigInt('19') -console.log(bigintCryptoUtils.modPow(a, b, n)); // prints 6 +console.log(bigintCryptoUtils.modPow(a, b, n)) // prints 6 -console.log(bigintCryptoUtils.modInv(BigInt("2"), BigInt("5"))); // prints 3 +console.log(bigintCryptoUtils.modInv(BigInt('2'), BigInt('5'))) // prints 3 -console.log(bigintCryptoUtils.modInv(BigInt("3"), BigInt("5"))); // prints 2 +console.log(bigintCryptoUtils.modInv(BigInt('3'), BigInt('5'))) // prints 2 -// Generation of a probable prime of 2048 bits -const prime = await bigintCryptoUtils.prime(2048); +console.log(bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256))) // Prints a cryptographically secure random number between 1 and 2**256 bits. -// Testing if a prime is a probable prime (Miller-Rabin) -if (await bigintCryptoUtils.isProbablyPrime(prime)) - // code if is prime +async function primeTesting () { + // Output of a probable prime of 2048 bits + console.log(await bigintCryptoUtils.prime(2048)) + + // Testing if a number is a probable prime (Miller-Rabin) + const number = 27 + const isPrime = await bigintCryptoUtils.isProbablyPrime(number) + if (isPrime) { + console.log(`${number} is prime`) + } else { + console.log(`${number} is composite`) + } +} + +primeTesting() - // Get a cryptographically secure random number between 1 and 2**256 bits. - const rnd = bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256)); ``` -### Javascript native from a browser - -You can just load the module in a html page as: - -```html - -``` - -### TypeScript - -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 JS Doc ### Functions diff --git a/src/doc/readme-template.md b/src/doc/readme-template.md index c4a2985..37613e0 100644 --- a/src/doc/readme-template.md +++ b/src/doc/readme-template.md @@ -22,75 +22,77 @@ For web browsers, you can also directly download the [IIFE file](https://raw.git ## Usage examples -### Node.js: +Import your module as : + + - Node.js + ```javascript + const bigintCryptoUtils = require('bigint-crypto-utils') + ... // your code here + ``` + - Javascript native project + ```javascript + import * as bigintCryptoUtils from 'bigint-crypto-utils' + ... // your code here + ``` + - Javascript native browser ES6 mod + ```html + + import as bcu from 'bigint-crypto-utils' + ... // your code here + ``` + - Javascript native browser IIFE + ```html + + + - TypeScript + ```typescript + 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 -const bigintCryptoUtils = require("bigint-crypto-utils"); - /* Stage 3 BigInts with value 666 can be declared as BigInt('666') or the shorter new no-so-linter-friendly syntax 666n. Notice that you can also pass a number, e.g. BigInt(666), but it is not recommended since values over 2**53 - 1 won't be safe but no warning will be raised. */ -let a = BigInt("5"); -let b = BigInt("2"); -let n = BigInt("19"); +const a = BigInt('5') +const b = BigInt('2') +const n = BigInt('19') -console.log(bigintCryptoUtils.modPow(a, b, n)); // prints 6 +console.log(bigintCryptoUtils.modPow(a, b, n)) // prints 6 -console.log(bigintCryptoUtils.modInv(BigInt("2"), BigInt("5"))); // prints 3 +console.log(bigintCryptoUtils.modInv(BigInt('2'), BigInt('5'))) // prints 3 -console.log(bigintCryptoUtils.modInv(BigInt("3"), BigInt("5"))); // prints 2 +console.log(bigintCryptoUtils.modInv(BigInt('3'), BigInt('5'))) // prints 2 -// Generation of a probable prime of 2048 bits -const prime = await bigintCryptoUtils.prime(2048); +console.log(bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256))) // Prints a cryptographically secure random number between 1 and 2**256 bits. -// Testing if a prime is a probable prime (Miller-Rabin) -if (await bigintCryptoUtils.isProbablyPrime(prime)) - // code if is prime +async function primeTesting () { + // Output of a probable prime of 2048 bits + console.log(await bigintCryptoUtils.prime(2048)) + + // Testing if a number is a probable prime (Miller-Rabin) + const number = 27 + const isPrime = await bigintCryptoUtils.isProbablyPrime(number) + if (isPrime) { + console.log(`${number} is prime`) + } else { + console.log(`${number} is composite`) + } +} + +primeTesting() - // Get a cryptographically secure random number between 1 and 2**256 bits. - const rnd = bigintCryptoUtils.randBetween(BigInt(2) ** BigInt(256)); ``` -### Javascript native from a browser - -You can just load the module in a html page as: - -```html - -``` - -### TypeScript - -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 JS Doc {{>main}}