Skip to content
This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
/ secure-id Public archive

Library for creating encrypted ids that is safe to use in public facing API

Notifications You must be signed in to change notification settings

openland/secure-id

Repository files navigation

@openland/secure-id

@openland/secure-id is a library for encrypting id's from database to make them safe to use in API calls.

How to use

yarn install @openland/secure-id

Before using your library you need to create a Shared Secret and distributed it to every application node that is going to use this library. Using of OpenSSH to generate long random string is highly recommended. Shared Secret MUST NOT be stored in database, use configuration files or environment variables instead. Shared Secret MUST be stored in a safe place since it could NOT be changed.

// First you need to create a factory for IDs
// WARNING: Constructor performs heavy computations and can take up to 100ms. Always reuse factory and IDs!
let factory = new SecIDFactory('Shared Secret');

// Then declare all IDs:
let userId = factory.createId('UserEntity');

// Encrypt and decrypt
let encryptedUserId = userId.serialize(1); // == "AVljNZ9dX8I40v3xRMeVUyN9Nv"
let decryptedUserId = userId.parse(encryptedUserId); // == 1

ID style

@openland/secure-id supports three encoding formats: hex, base64 and hashids. Default one is hashids: https://hashids.org.

let hexFactory = new SecIDFactory('Shared Secret', 'hex');
let base64Factory = new SecIDFactory('Shared Secret', 'base64');

License

MIT (c) Data Makes Perfect LLC

About

Library for creating encrypted ids that is safe to use in public facing API

Resources

Stars

Watchers

Forks

Packages

No packages published