An easy and simple way to encrypt and decrypt information for client and server side components
npm install easy-simple-encryption> Easily add simple encryption / decryption to React and Nextjs applications.
- Easy Simple Encryption
- Table of contents
- Installation
- Usage
- Secret
- useEncryption Hook
- encrypt function
- decrypt function
- Versioning
- Authors
To install the library, run:
``sh`
$ npm i easy-simple-encryption
Add a secret key that will be used to generate the encypted text.
This is optional.
> Setting a key is recommended to add additional security
Usage:
`ts`
secret("some key");
Options:
key (Required)
| Type | value | Description |
| ------ | ----- | ------------ |
| string | "key" | A secret key |
Example:
`tsx`
import { secret as SecretKey } from "easy-simple-encryption";
SecretKey(process.env.SECRET);
The hook that can be used from any client side component.
The hook uses async calls.
Usage:
`ts`
const { encrypt, decrypt } = useEncryption();
Usage:
`ts`
encrypt("text");
Options:
text (Required)
| Type | value | Description |
| ------ | ------ | -------------------------- |
| string | "text" | The string to be encrypted |
Example:
> outputs: 4330776p5k6l24246779624o664024255o2631
`tsx
import { useEncryption } from "easy-simple-encryption";
function MyComponent() {
const { encrypt } = useEncryption();
// on async call
const onSubmit = async (e: any) => {
e.preventDefault();
const password = await encrypt("S0mePa$$worD");
}
return (
$3
Usage:
`ts
decrypt("encrypted_key");
`Options:
encrypted_key (Required)| Type | value | Description |
| ------ | --------------- | ----------------------- |
| string | "encrypted_key" | The key to be decrypted |
Example:
> outputs S0mePa$$worD
`tsx
import { useEncryption } from "easy-simple-encryption";function MyComponent() {
const { decrypt } = useEncryption();
// on submit
const onSubmit = async (e: any) => {
e.preventDefault();
const password = await decrypt("4330776p5k6l24246779624o664024255o2631");
}
return (
);
}
export default MyComponent;
`$3
This function can be used on server side components.
Usage:
`ts
encrypt("text");
`Options:
text (Required)| Type | value | Description |
| ------ | ------ | -------------------------- |
| string | "text" | The string to be encrypted |
Example:
`ts
"use server";
import { encrypt, decrypt } from "easy-simple-encryption";export const login = async (
details: Authentication
): Promise => {
const { password } = details;
const account: UserAccount = await checkAccount(await encrypt(password));
if (account.error) {
return {
result: AUTHPAGERESULT.FAIL,
};
}
return {
result: AUTHPAGERESULT.SUCCESS,
};
};
`$3
This function can be used on server side components.
Usage:
`ts
decrypt("text");
`Options:
encrypted_key (Required)| Type | value | Description |
| ------ | --------------- | ----------------------- |
| string | "encrypted_key" | The key to be decrypted |
Example:
`ts
"use server";
import { encrypt, decrypt } from "easy-simple-encryption";export const login = async (
details: Authentication
): Promise => {
const { email, password } = details;
const account: UserAccount = await getAccount(email);
const auth = await decrypt(account.password);
if (auth !== password) {
return {
result: AUTHPAGERESULT.FAIL,
};
}
return {
result: AUTHPAGERESULT.SUCCESS,
};
};
``- 1.0.7 Corrected a bug that was making the encryption/decryption not working correctly.
- 1.0.4 (Added Secret)
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- Wallace Krumrei - WallaceKrumrei