1.0.4 • Published 3 months ago

easy-simple-encryption v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

React - Easy Simple Encryption

Easily add simple encryption / decryption to React and Nextjs applications.

Table of contents

Installation

To install the library, run:

$ npm i easy-simple-encryption

Usage

Secret

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:

secret("some key");

Options:

key (Required)

TypevalueDescription
string"key"A secret key

Example:

import { secret as SecretKey } from "easy-simple-encryption";
SecretKey(process.env.SECRET);

useEncryption

The hook that can be used from any client side component. The hook uses async calls.

Usage:

const { encrypt, decrypt } = useEncryption();

encrypt

Usage:

encrypt("text");

Options:

text (Required)

TypevalueDescription
string"text"The string to be encrypted

Example:

outputs: 4330776p5k6l24246779624o664024255o2631

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 (
    <div>
      <button onClick={onSubmit}>
    </div>
  );
}
export default MyComponent;

decrypt

Usage:

decrypt("encrypted_key");

Options:

encrypted_key (Required)

TypevalueDescription
string"encrypted_key"The key to be decrypted

Example:

outputs S0mePa$$worD

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 (
    <div>
      <button onClick={onSubmit}>
    </div>
  );
}
export default MyComponent;

encrypt function

This function can be used on server side components.

Usage:

encrypt("text");

Options:

text (Required)

TypevalueDescription
string"text"The string to be encrypted

Example:

"use server";
import { encrypt, decrypt } from "easy-simple-encryption";

export const login = async (
  details: Authentication
): Promise<AuthenticationResult> => {
  const { password } = details;

  const account: UserAccount = await checkAccount(await encrypt(password));
  if (account.error) {
    return {
      result: AUTHPAGERESULT.FAIL,
    };
  }

  return {
    result: AUTHPAGERESULT.SUCCESS,
  };
};

decrypt function

This function can be used on server side components.

Usage:

decrypt("text");

Options:

encrypted_key (Required)

TypevalueDescription
string"encrypted_key"The key to be decrypted

Example:

"use server";
import { encrypt, decrypt } from "easy-simple-encryption";

export const login = async (
  details: Authentication
): Promise<AuthenticationResult> => {
  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,
  };
};

Versioning

  • 1.0.4 (Added Secret)
  • 1.0.3
  • 1.0.2
  • 1.0.1
  • 1.0.0

Authors