Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add encrypt and decrypt functions #679

Merged
merged 1 commit into from
Dec 13, 2024

Conversation

aralroca
Copy link
Collaborator

Fixes #678

It is useful for data-attributes and server actions, but can be used also for API endpoints, etc...

We already had the methods because they were in the encrypted store, but they were not exposed and it does not cost anything, as they are useful.


encrypt

Reference

encrypt(textOrObject: unknown): string

The encrypt function allows you to securely send sensitive data from the client to the server. It is typically used in conjunction with the decrypt function to safely handle data transmission.

Converts a given text or object into an encrypted string for secure client-server communication.

Example usage:

import { encrypt, decrypt } from "brisa/server";

// ...
<button
  onClick={(e: Event) => {
    // Decrypt on the server action:
    console.log(
      decrypt((e.target as HTMLButtonElement).dataset.encrypted!)
    )
  }}
  data-encrypted={encrypt("some sensible data")}
>
  Click to recover sensible data on the server
</button>

In this example, the encrypt function secures the data before it is stored in a data-encrypted attribute. The decrypt function is then used on the server to recover the original value.

decrypt

Reference

decrypt(encryptedString: string): unknown

The decrypt function allows you to securely retrieve sensitive data sent from the client to the server. It works in conjunction with the encrypt function to ensure safe handling of sensitive information.

Converts an encrypted string back into its original text or object, enabling secure client-server communication.

Example usage:

import { encrypt, decrypt } from "brisa/server";

// ...
<button
  onClick={(e: Event) => {
    // Decrypt on the server action:
    console.log(
      decrypt((e.target as HTMLButtonElement).dataset.encrypted!)
    )
  }}
  data-encrypted={encrypt("some sensible data")}
>
  Click to recover sensible data on the server
</button>

In this example, the encrypt function secures the data before it is stored in a data-encrypted attribute. The decrypt function is then used on the server to recover the original value.

@aralroca aralroca merged commit de67cbc into canary Dec 13, 2024
@aralroca aralroca deleted the aralroca/encrypt-and-decrypt branch December 13, 2024 19:02
Copy link

pkg-pr-new bot commented Dec 13, 2024

Open in Stackblitz

brisa-adapter-vercel

npm i https://pkg.pr.new/brisa-build/brisa/brisa-adapter-vercel@679

brisa-tailwindcss

npm i https://pkg.pr.new/brisa-build/brisa/brisa-tailwindcss@679

brisa

npm i https://pkg.pr.new/brisa-build/brisa@679

create-brisa

npm i https://pkg.pr.new/brisa-build/brisa/create-brisa@679

www

npm i https://pkg.pr.new/brisa-build/brisa/www@679

brisa-pandacss

npm i https://pkg.pr.new/brisa-build/brisa/brisa-pandacss@679

commit: 9ea71a8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose encrypt and decrypt server APIs
1 participant