1.1.0 • Published 8 months ago
gt-secure-token-library v1.1.0
GT Token Library
A minimalistic library for encrypting and decrypting data in the form of token string like JWT Tokens.
This is lightweight library i made for to add 1 security layer & enhance protection of data for unwanted leaks. I have given support for both 'mjs' & 'cjs' for better development experience. Also have support for Vanilla JavaScript & added jQuery support to use or test this library functionality.
Warning - I am not recommends to use this library with plain html & with plain vanilla JavaScript & jQuery because the secret you are setting with token have high chance to compromise. But you can able to test the library functionality in frontend by using simply browser package.
you can locate & download the browser package here.
Quick Demo
See browser demo here.
You can find more live examples
below.
Install package via NPM.
npm install gt-token-library
Usage with 'cjs' - CommonJS Module.
const GTToken = require("gt-token-library").default;
Usage with 'mjs' - ESM import.
import GTToken from "gt-token-library";
Available Methods & options
Name | Type | Default | Method Description |
---|---|---|---|
encrypt(data, options) | async function | null | Method to encrypt the data along with options as per your requirements. |
decrypt(token) | async function | null | Method to decrypt the token data. |
verify(token) | async function | null | Method to verify token validity & expiry. Return "true" or "false" depends on validity. |
expiresIn | number | 0 | Option to set expiry time in seconds only. |
Supported Data Types for Encryption.
- strings
- number
- boolean
- arrays
- objects
Example with CommonJS Usage - NodeJS
const GTToken = require("gt-token-library").default;
require('dotenv').config();
const tokenSecret = process.env.TOKEN_SECRET;
const dataToBeEcrypted = [1, true, 0.50, "Hello World"];
// Create an instance with a secret key.
const token = new GTToken(tokenSecret);
const init = async () => {
try {
// With expiration
const arrayToken = await token.encrypt(dataToBeEcrypted, { expiresIn: 300 });
console.log(arrayToken);
// Let's decrypt token.
const decrypt = await token.decrypt(arrayToken);
console.log(decrypt);
// Let's verify generated token by using method called --> await verify(token);
// const verify = await token.verify("generated_token_to_verify_will_be_goes_here");
// console.log(verify);
} catch (error) {
if (error) {
console.error('Invalid secret key used for decryption');
} else {
console.error('Error:', error);
}
}
}
init();
Example with ESM Usage - NodeJS
import GTToken from "gt-token-library";
import 'dotenv/config';
const tokenSecret = process.env.TOKEN_SECRET;
const dataToBeEcrypted = { obj: 'Hello World' };
// Create an instance with a secret key.
const token = new GTToken(tokenSecret);
const init = async () => {
try {
// With expiration
const objectToken = await token.encrypt(dataToBeEcrypted, { expiresIn: 300 });
console.log(objectToken);
// Let's decrypt token.
const decrypt = await token.decrypt(objectToken);
console.log(decrypt);
// Let's verify generated token by using method called --> await verify(token);
const verify = await token.verify("generated_token_to_verify_will_be_goes_here");
console.log(verify);
} catch (error) {
if (error) {
console.error('Invalid secret key used for decryption');
} else {
console.error('Error:', error);
}
}
}
init();
Compatibility
- Supported with 'cjs' - CommonJS Module.
- Supported with 'mjs' - ESM.
- TypeScript Support.
- Compatible with Node JS backend like "MERN" Stack.
- Compatible with Next JS client & server side both.
- Compatible with Vite - React JS setup.
- Browser bundle available to support Plain HTML Vanilla JavaScript.
- Compatible with jQuery.