Skip to content

Commit 1e3b6eb

Browse files
authored
Added docs for crypto APIs (#500)
Signed-off-by: ItalyPaleAle <[email protected]>
1 parent 5e42a79 commit 1e3b6eb

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

daprdocs/content/en/js-sdk-docs/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ A client library for building Dapr apps in JavaScript and TypeScript. This clien
1111

1212
## Installation
1313

14-
To get started with the Javascript SDK, install the Dapr JavaScript SDK package from [NPM](https://www.npmjs.com/package/@dapr/dapr):
14+
To get started with the JavaScript SDK, install the Dapr JavaScript SDK package from [NPM](https://www.npmjs.com/package/@dapr/dapr):
1515

1616
```bash
1717
npm install --save @dapr/dapr
1818
```
1919

2020
## Structure
2121

22-
The Dapr Javascript SDK contains two major components:
22+
The Dapr JavaScript SDK contains two major components:
2323

2424
- **DaprServer**: to manage all Dapr sidecar to application communication.
2525
- **DaprClient**: to manage all application to Dapr sidecar communication.

daprdocs/content/en/js-sdk-docs/js-client/_index.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The Dapr Client allows you to communicate with the Dapr Sidecar and get access t
1414

1515
- [Dapr CLI]({{< ref install-dapr-cli.md >}}) installed
1616
- Initialized [Dapr environment]({{< ref install-dapr-selfhost.md >}})
17-
- [Latest LTS version of Node or greater](https://nodejs.org/en/)
17+
- [Latest LTS version of Node.js or greater](https://nodejs.org/en/)
1818

1919
## Installing and importing Dapr's JS SDK
2020

@@ -451,6 +451,96 @@ start().catch((e) => {
451451
});
452452
```
453453

454+
### Cryptography API
455+
456+
> Support for the cryptography API is only available on the gRPC client in the JavaScript SDK.
457+
458+
```typescript
459+
import { createReadStream, createWriteStream } from "node:fs";
460+
import { readFile, writeFile } from "node:fs/promises";
461+
import { pipeline } from "node:stream/promises";
462+
463+
import { DaprClient, CommunicationProtocolEnum } from "@dapr/dapr";
464+
465+
const daprHost = "127.0.0.1";
466+
const daprPort = "50050"; // Dapr Sidecar Port of this example server
467+
468+
async function start() {
469+
const client = new DaprClient({
470+
daprHost,
471+
daprPort,
472+
communicationProtocol: CommunicationProtocolEnum.GRPC,
473+
});
474+
475+
// Encrypt and decrypt a message using streams
476+
await encryptDecryptStream(client);
477+
478+
// Encrypt and decrypt a message from a buffer
479+
await encryptDecryptBuffer(client);
480+
}
481+
482+
async function encryptDecryptStream(client: DaprClient) {
483+
// First, encrypt the message
484+
console.log("== Encrypting message using streams");
485+
console.log("Encrypting plaintext.txt to ciphertext.out");
486+
487+
await pipeline(
488+
createReadStream("plaintext.txt"),
489+
await client.crypto.encrypt({
490+
componentName: "crypto-local",
491+
keyName: "symmetric256",
492+
keyWrapAlgorithm: "A256KW",
493+
}),
494+
createWriteStream("ciphertext.out"),
495+
);
496+
497+
// Decrypt the message
498+
console.log("== Decrypting message using streams");
499+
console.log("Encrypting ciphertext.out to plaintext.out");
500+
await pipeline(
501+
createReadStream("ciphertext.out"),
502+
await client.crypto.decrypt({
503+
componentName: "crypto-local",
504+
}),
505+
createWriteStream("plaintext.out"),
506+
);
507+
}
508+
509+
async function encryptDecryptBuffer(client: DaprClient) {
510+
// Read "plaintext.txt" so we have some content
511+
const plaintext = await readFile("plaintext.txt");
512+
513+
// First, encrypt the message
514+
console.log("== Encrypting message using buffers");
515+
516+
const ciphertext = await client.crypto.encrypt(plaintext, {
517+
componentName: "crypto-local",
518+
keyName: "my-rsa-key",
519+
keyWrapAlgorithm: "RSA",
520+
});
521+
522+
await writeFile("test.out", ciphertext);
523+
524+
// Decrypt the message
525+
console.log("== Decrypting message using buffers");
526+
const decrypted = await client.crypto.decrypt(ciphertext, {
527+
componentName: "crypto-local",
528+
});
529+
530+
// The contents should be equal
531+
if (plaintext.compare(decrypted) !== 0) {
532+
throw new Error("Decrypted message does not match original message");
533+
}
534+
}
535+
536+
start().catch((e) => {
537+
console.error(e);
538+
process.exit(1);
539+
});
540+
```
541+
542+
> For a full guide on cryptography visit [How-To: Cryptography]({{< ref howto-cryptography.md >}}).
543+
454544
### Distributed Lock API
455545

456546
#### Try Lock and Unlock APIs

0 commit comments

Comments
 (0)