pagecrypt
Advanced tools
Comparing version 3.1.0 to 3.2.0
# Changelog for `pagecrypt` | ||
## 3.2.0 - 2021-04-24 | ||
### Features | ||
- Add password generator, built with the Node.js `crypto` module and without any external dependencies. Convenient, performant and secure. | ||
- CLI: Add `--generate-password` (alias `-g`) option to encrypt using a generated password of given length. See `README.md` for more info. | ||
- The password generator works well together with the JS API too. | ||
### Fixes | ||
- Update tests and docs to describe new password generator feature. | ||
--- | ||
## 3.1.0 - 2021-04-24 | ||
@@ -4,0 +18,0 @@ |
38
cli.js
@@ -5,15 +5,41 @@ #!/usr/bin/env node | ||
const { encrypt } = require('./index') | ||
const { encrypt, generatePassword } = require('./index') | ||
const pkg = require('./package.json') | ||
sade(`${pkg.name} <src> <dest> <password>`, true) | ||
sade(`${pkg.name} <src> <dest> [password]`, true) | ||
.version(pkg.version) | ||
.describe( | ||
'Encrypt the <src> HTML file with <password> and save the result in the <dest> HTML file.', | ||
'Encrypt the <src> HTML file with [password] and save the result in the <dest> HTML file.', | ||
) | ||
.example('index.html encrypted.html password') | ||
.action(async (src, dest, password) => { | ||
console.log(`🔐 Encrypting ${src} → ${dest}`) | ||
await encrypt(src, dest, password) | ||
.example('index.html encrypted.html --generate-password 64') | ||
.example('index.html encrypted.html -g 64') | ||
.option( | ||
'-g, --generate-password', | ||
'Generate a random password with given length. Must be a number if used.', | ||
) | ||
.action(async (src, dest, password, options) => { | ||
const length = options['generate-password'] | ||
if (length) { | ||
if (Number.isInteger(length)) { | ||
const pass = generatePassword(length) | ||
console.log(`🔐 Encrypting ${src} → ${dest} with 🔑: ${pass}`) | ||
await encrypt(src, dest, pass) | ||
} else { | ||
console.error( | ||
'❌: The <length> must be an integer when using --generate-password <length>', | ||
) | ||
process.exit(1) | ||
} | ||
} else if (password) { | ||
console.log(`🔐 Encrypting ${src} → ${dest} with 🔑: ${password}`) | ||
await encrypt(src, dest, password) | ||
} else { | ||
console.error( | ||
'❌: Either provide a password or use --generate-password <length>', | ||
) | ||
process.exit(1) | ||
} | ||
}) | ||
.parse(process.argv) |
22
index.js
@@ -1,2 +0,5 @@ | ||
const { subtle, getRandomValues } = require('crypto').webcrypto | ||
const { | ||
randomFillSync, | ||
webcrypto: { subtle, getRandomValues }, | ||
} = require('crypto') | ||
const { mkdir, readFile, writeFile } = require('fs/promises') | ||
@@ -120,3 +123,20 @@ const { resolve, dirname } = require('path') | ||
/** | ||
* Generate a random password of a given length. | ||
* | ||
* @param {number} length The password length. | ||
* @param {string} characters The characters used to generate the password. | ||
* @returns A random password. | ||
*/ | ||
function generatePassword( | ||
length = 80, | ||
characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', | ||
) { | ||
return Array.from(randomFillSync(new Uint32Array(length))) | ||
.map((x) => characters[x % characters.length]) | ||
.join('') | ||
} | ||
exports.encryptHTML = encryptHTML | ||
exports.encrypt = encrypt | ||
exports.generatePassword = generatePassword |
{ | ||
"name": "pagecrypt", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Easily add client-side password-protection to your Single Page Applications and HTML files.", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"cli": "node cli.js", | ||
"test": "npm pack && cd test && npm i ../pagecrypt-*.tgz && npm run test && cd .. && echo 'Verify test output in `test/out-js.html` and `test/out-cli.html`'", | ||
"test": "npm pack && cd test && npm i ../pagecrypt-*.tgz && npm run test && cd .. && echo 'Verify test output in the `test/out-*.html` files.'", | ||
"start": "vite", | ||
@@ -11,0 +11,0 @@ "build": "vite build", |
@@ -16,5 +16,31 @@ # PageCrypt - Password Protected Single Page Applications and HTML files | ||
```sh | ||
npx pagecrypt <src> <dest> <password> | ||
npx pagecrypt <src> <dest> [password] [options] | ||
``` | ||
Encrypt using a generate password with given length: | ||
```sh | ||
npx pagecrypt <src> <dest> -g <length> | ||
``` | ||
#### 1.1. CLI Help | ||
``` | ||
Description | ||
Encrypt the <src> HTML file with [password] and save the result in the <dest> HTML file. | ||
Usage | ||
$ pagecrypt <src> <dest> [password] [options] | ||
Options | ||
-g, --generate-password Generate a random password with given length. Must be a number if used. | ||
-v, --version Displays current version | ||
-h, --help Displays this message | ||
Examples | ||
$ pagecrypt index.html encrypted.html password | ||
$ pagecrypt index.html encrypted.html --generate-password 64 | ||
$ pagecrypt index.html encrypted.html -g 64 | ||
``` | ||
### 2. Automate `pagecrypt` in your build process | ||
@@ -77,2 +103,15 @@ | ||
#### `generatePassword(length: number): string` | ||
```js | ||
import { encrypt, generatePassword } from 'pagecrypt' | ||
// Generate a random password without any external dependencies | ||
const pass = generatePassword(64) | ||
// Works with both JS API:s | ||
await encrypt('index.html', 'encrypted.html', pass) | ||
const encryptedHTML = await encryptHTML('html string', pass) | ||
``` | ||
--- | ||
@@ -79,0 +118,0 @@ |
25914
168
141