Socket
Socket
Sign inDemoInstall

object-encode

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-encode - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

LICENSE.md

99

index.js

@@ -1,44 +0,63 @@

const juri = require("juri")();
const codec = require('string-codec');
var ss = require('seededshuffle');
const codec_algorithms = [ "hex", "base64", "ascii85","base91","rot5", "rot13", "rot18", "rot47", "rev", "url" ,"punycode" ];
const codec_algorithms = [
'hex',
'base64',
'ascii85',
'base91',
'rot5',
'rot13',
'rot18',
'rot47',
'rev',
'url',
'punycode',
];
function set_algorithm(algorithm){
algorithm = codec_algorithms.indexOf(algorithm)>-1 ? algorithm : 'base64';
function set_algorithm(algorithm) {
algorithm = codec_algorithms.indexOf(algorithm) > -1 ? algorithm : 'base64';
return algorithm;
}
function encode(str, algorithm, salt, remove_padding){
function encode(str, algorithm, salt, remove_padding) {
//reject if string not entered
if (typeof str !== 'string') {
throw new Error('String Value required.');
}
//ensure remove_padding values set
remove_padding = remove_padding===undefined ? true : remove_padding;
remove_padding = remove_padding === undefined ? true : remove_padding;
//set right algorithm
algorithm = set_algorithm(algorithm);
//ensure salt is string
salt = String(salt) || 'changeme';
//reject if string not entered
if(typeof str !== 'string'){ throw new Error("String Value required."); }
salt = String(salt) || 'changeme';
//encode string with given algorithm
str = codec.encoder( str, algorithm );
str = codec.encoder(str, algorithm);
//remove padding for base64 encoding
if(remove_padding && algorithm=='base64'){ str = str.replace(/=+$/,''); }
if (remove_padding && algorithm == 'base64') {
str = str.replace(/=+$/, '');
}
//shuffle string chars using salt
str = ss.shuffle(str.split(''),salt,true).join('');
str = ss.shuffle(str.split(''), salt, true).join('');
return str;
return str;
}
function decode(str, algorithm, salt){
function decode(str, algorithm, salt) {
//reject if string not entered
if (typeof str !== 'string') {
throw new Error('String Value required.');
}
//set right algorithm
algorithm = set_algorithm(algorithm);
//ensure salt is string
salt = String(salt) || 'changeme';
//reject if string not entered
if(typeof str !== 'string'){ throw new Error("String Value required."); }
salt = String(salt) || 'changeme';
//unshuffle string using salt
str = ss.unshuffle(str.split(''),salt,true).join('');
str = ss.unshuffle(str.split(''), salt, true).join('');
//decode string by given algorithm
str = codec.decoder( str, algorithm );
str = codec.decoder(str, algorithm);

@@ -48,7 +67,8 @@ return str;

function encode_object(object, algorithm, salt, remove_padding){
function encode_object(object, algorithm, salt, remove_padding) {
if (typeof object !== 'object') {
throw new Error('You can only encode objects using this method.');
}
if(typeof object !== 'object'){ throw new Error("You can only encode objects using this method"); }
var str = juri.encode(object);
var str = JSON.stringify(object);
// console.log(str, str.length)

@@ -60,18 +80,18 @@ str = encode(str, algorithm, salt, remove_padding);

function decode_object(string, algorithm, salt, remove_padding){
function decode_object(string, algorithm, salt, remove_padding) {
if (typeof string !== 'string') {
throw new Error('String Value required.');
}
if(typeof string !== 'string'){ throw new Error("String Value required."); }
var object = {};
try{
try {
string = decode(string, algorithm, salt, remove_padding);
object = juri.decode(string);
}
catch(e){
object = JSON.parse(string);
} catch (e) {
object = {
':ERROR:' : {
code : 403,
msg : "Error decoding String tinto object!"
}
':ERROR:': {
code: 403,
msg: 'Error decoding String into object!',
},
};

@@ -81,10 +101,9 @@ }

return object;
}
module.exports = {
encode : encode,
decode : decode,
encode_object : encode_object,
decode_object : decode_object
}
encode: encode,
decode: decode,
encode_object: encode_object,
decode_object: decode_object,
};
{
"name": "object-encode",
"version": "1.0.2",
"version": "1.0.3",
"description": "Encode your objects into simple string hashes that you can then pass around. Then decode them back with ease.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"jest": {
"verbose": true
},
"keywords": [

@@ -18,3 +21,2 @@ "encode",

"dependencies": {
"juri": "^1.0.3",
"seededshuffle": "^0.1.1",

@@ -24,4 +26,3 @@ "string-codec": "^0.1.0"

"devDependencies": {
"assert": "^1.4.1",
"log-symbols": "^1.0.2"
"jest": "^29.7.0"
},

@@ -28,0 +29,0 @@ "repository": {

@@ -1,2 +0,11 @@

# Object Encode
# Object Encode
This module has been used in production a few times already and I haven't had any issues raised so far. The latest update was only to write better tests using [jest](https://www.npmjs.com/package/jest).
I would love to hear about what you build using **Object Encode**.
# Intro
Sometimes you need to safely encode an object into a string and then decode it back into an object.

@@ -57,2 +66,4 @@

**NOTE:**
* `encode_object()` uses `JSON.stringify` methods. As such, only pass objects that can be stringified safely. Things like circular references and functions will throw an error.
* Default algorithm is ***base64***.

@@ -83,5 +94,7 @@

# A Note on Security
# A Note On Security
The default *salt* value is **'changeme'** so please use your own. Like passwords, choose a strong salt value.
Please do not encode sensitive data like passwords within your objects. This library is not built with security in mind. The ultimate goal was to simply encode objects into strings so be wise & keep your sensitive data safe!
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc