diff --git a/apps/basic/README.md b/apps/basic/README.md index daa0bfdc4..48e01fa85 100644 --- a/apps/basic/README.md +++ b/apps/basic/README.md @@ -1,18 +1,71 @@ -# Basic example using @cipherstash/protect +# Basic example of using @cipherstash/protect -This is a basic example of how to use the @cipherstash/protect package. +This basic example demonstrates how to use the `@cipherstash/protect` package to encrypt arbitrary input. -## Usage +## Installing the basic example -1. Create a `.env` file in the root directory of this project with the following contents: +> [!IMPORTANT] +> Make sure you have installed Node.js and [pnpm](https://pnpm.io/installation) before following these steps. +Clone this repo: + +```bash +git clone https://github.com/cipherstash/protectjs +``` + +Install dependencies: + +```bash +# Build Project.js +cd protectjs +pnpm build + +# Install deps for basic example +cd protectjs/apps/basic +pnpm install ``` -CS_CLIENT_ID=your-client-id -CS_CLIENT_KEY=your-client-key + +Lastly, install the CipherStash CLI: + +- On macOS: + + ```bash + brew install cipherstash/tap/stash + ``` + +- On Linux, download the binary for your platform, and put it on your `PATH`: + - [Linux ARM64](https://github.com/cipherstash/cli-releases/releases/latest/download/stash-aarch64-unknown-linux-gnu) + - [Linux x86_64](https://github.com/cipherstash/cli-releases/releases/latest/download/stash-x86_64-unknown-linux-gnu) + + +## Configuring the basic example + +> [!IMPORTANT] +> Make sure you have [installed the CipherStash CLI](#installation) before following these steps. + +Set up all the configuration and credentials required for Protect.js: + +```bash +stash setup ``` -2. Run `pnpm install` to install the dependencies. +If you have not already signed up for a CipherStash account, this will prompt you to do so along the way. + +At the end of `stash setup`, you will have two files in your project: + +- `cipherstash.toml` which contains the configuration for Protect.js +- `cipherstash.secret.toml` which contains the credentials for Protect.js + +> [!WARNING] +> Do not commit `cipherstash.secret.toml` to git, because it contains sensitive credentials. -3. Run `pnpm start` to start the application. -4. The application will log the plaintext to the console which has been encrypted using the CipherStash, decrypted, and logged the original plaintext. \ No newline at end of file +## Using the basic example + +Run the example: + +``` +pnpm start +``` + +The application will log the plaintext to the console that has been encrypted using the CipherStash, decrypted, and logged the original plaintext. diff --git a/apps/basic/index.mjs b/apps/basic/index.mjs index d77da6c6e..2074c6ca6 100644 --- a/apps/basic/index.mjs +++ b/apps/basic/index.mjs @@ -1,17 +1,38 @@ import 'dotenv/config' import { protect } from '@cipherstash/protect' +import readline from 'node:readline'; + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const askQuestion = () => { + return new Promise((resolve) => { + rl.question("\n👋Hello\n\nWhat is your name? ", (answer) => { + resolve(answer); + }); + }); +}; async function main() { const protectClient = await protect() + const input = await askQuestion(); - const ciphertext = await protectClient.encrypt('plaintext', { + const ciphertext = await protectClient.encrypt(input, { column: 'column_name', table: 'users', }) + console.log('Encrypting your name...') + console.log('The ciphertext is:', ciphertext) + const plaintext = await protectClient.decrypt(ciphertext) + console.log('Decrypting the ciphertext...') console.log('The plaintext is:', plaintext) + + rl.close(); } main()