@@ -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