Socket
Socket
Sign inDemoInstall

js-encoding-utils

Package Overview
Dependencies
58
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.3 to 0.2.4

47

dist/encoder.js

@@ -1,1 +0,46 @@

"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");var env=_interopRequireWildcard(require("./env.js"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.encodeBase64=encodeBase64,exports.decodeBase64=decodeBase64,exports.encodeBase64Url=encodeBase64Url,exports.decodeBase64Url=decodeBase64Url,exports.arrayBufferToHexString=arrayBufferToHexString,exports.hexStringToArrayBuffer=hexStringToArrayBuffer,exports.arrayBufferToString=arrayBufferToString,exports.stringToArrayBuffer=stringToArrayBuffer;function encodeBase64(a){var b="";b="string"==typeof a?a:arrayBufferToString(a);var c=env.getEnvBtoa();return c(b)}function decodeBase64(a){var b=env.getEnvAtob(),c=b(a),d=stringToArrayBuffer(c);return getAsciiIfAscii(d)}function sanitizeTypedArrayAndArrayBuffer(a){if(a instanceof Uint8Array)return a;if(ArrayBuffer.isView(a)&&"undefined"!=typeof a.buffer)return new Uint8Array(a.buffer);if(a instanceof ArrayBuffer)return new Uint8Array(a);throw new Error("Input must be an ArrayBuffer or a TypedArray")}function getAsciiIfAscii(a){if(a instanceof Uint8Array){for(var b=!0,c=0;c<a.length;c++)if(126<a[c]||32>a[c]&&13!==a[c]&&10!==a[c]){b=!1;break}var d=null;if(b){d="";for(var e=0;e<a.length;e++)d+=String.fromCharCode(a[e])}else d=a;return d}throw new Error("Input data must be an Uint8Array")}function encodeBase64Url(a){return encodeBase64(a).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}function decodeBase64Url(a){if("string"!=typeof a)throw new Error("Input must be string");return a=a.replace(/-/g,"+").replace(/_/g,"/"),decodeBase64(a)}function arrayBufferToHexString(a){for(var b,c=sanitizeTypedArrayAndArrayBuffer(a),d="",e=0;e<c.length;e++)b=(255&c[e]).toString(16),b=1===b.length?"0".concat(b):b,d+=b;return d}function hexStringToArrayBuffer(a){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");for(var b=[],c=a.length,d=0;d<c;d+=2)b.push(parseInt(a.substr(d,2),16));return new Uint8Array(b)}function arrayBufferToString(a){var b=sanitizeTypedArrayAndArrayBuffer(a);return String.fromCharCode.apply(null,b)}function stringToArrayBuffer(a){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");for(var b=new Uint8Array(a.length),c=0;c<a.length;c++)b[c]=a.charCodeAt(c);return b}
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");var env=_interopRequireWildcard(require("./env.js"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.encodeBase64=encodeBase64,exports.decodeBase64=decodeBase64,exports.encodeBase64Url=encodeBase64Url,exports.decodeBase64Url=decodeBase64Url,exports.arrayBufferToHexString=arrayBufferToHexString,exports.hexStringToArrayBuffer=hexStringToArrayBuffer,exports.arrayBufferToString=arrayBufferToString,exports.stringToArrayBuffer=stringToArrayBuffer;/**
* encoder.js
*/ /**
* Encode ArrayBuffer or TypedArray To Base64
* @param data
* @return {*}
*/function encodeBase64(a){var b="";b="string"==typeof a?a:arrayBufferToString(a);var c=env.getEnvBtoa();return c(b)}/**
* Decode Base64 to Uint8Array
* @param str
* @return {Uint8Array|string|*}
*/function decodeBase64(a){var b=env.getEnvAtob(),c=b(a),d=stringToArrayBuffer(c);return getAsciiIfAscii(d)}/**
* if input data is an ArrayBuffer or TypedArray, it would be returned as Uint8Array
* @param data
* @return {Uint8Array}
*/function sanitizeTypedArrayAndArrayBuffer(a){if(a instanceof Uint8Array)return a;if(ArrayBuffer.isView(a)&&"undefined"!=typeof a.buffer)// TypedArray except Uint8Array
return new Uint8Array(a.buffer);if(a instanceof ArrayBuffer)// ArrayBuffer
return new Uint8Array(a);throw new Error("Input must be an ArrayBuffer or a TypedArray")}/**
* Check if the given Uint8Array can be expressed in Ascii Text
* @param data
* @return {Uint8Array|string|*}
*/function getAsciiIfAscii(a){if(a instanceof Uint8Array){for(var b=!0,c=0;c<a.length;c++)if(126<a[c]||32>a[c]&&13!==a[c]&&10!==a[c]){b=!1;break}var d=null;if(b){d="";for(var e=0;e<a.length;e++)d+=String.fromCharCode(a[e])}else d=a;return d}throw new Error("Input data must be an Uint8Array")}/**
* Encode ArrayBuffer or TypedArray to base64url string
* @param data
* @return {string}
*/function encodeBase64Url(a){return encodeBase64(a).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}/**
* Decode Base64Url string to Uint8Array
* @param str
* @return {Uint8Array}
*/function decodeBase64Url(a){if("string"!=typeof a)throw new Error("Input must be string");// str = str + "=".repeat(str.length % 4); // this sometimes causes error...
return a=a.replace(/-/g,"+").replace(/_/g,"/"),decodeBase64(a)}/**
* Encode ArrayBuffer or TypedArray to hex string
* @param data
* @return {string}
*/function arrayBufferToHexString(a){for(var b,c=sanitizeTypedArrayAndArrayBuffer(a),d="",e=0;e<c.length;e++)b=(255&c[e]).toString(16),b=1===b.length?"0".concat(b):b,d+=b;return d}/**
* Decode hex string to Uint8Array
* @param str
* @return {Uint8Array}
*/function hexStringToArrayBuffer(a){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");for(var b=[],c=a.length,d=0;d<c;d+=2)b.push(parseInt(a.substr(d,2),16));return new Uint8Array(b)}/**
* Encode ArrayBuffer or TypedArray to string with code (like output of legacy atob)
* @param data
* @return {string}
*/function arrayBufferToString(a){var b=sanitizeTypedArrayAndArrayBuffer(a);return String.fromCharCode.apply(null,b)}/**
* Decode string with code (like output of legacy atob) to Uint8Array
* @param str
* @return {Uint8Array}
*/function stringToArrayBuffer(a){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");for(var b=new Uint8Array(a.length),c=0;c<a.length;c++)b[c]=a.charCodeAt(c);return b}

@@ -1,1 +0,6 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getEnvBtoa=getEnvBtoa,exports.getEnvAtob=getEnvAtob;function getEnvBtoa(){return"undefined"==typeof window?nodeBtoa:window.btoa}function getEnvAtob(){return"undefined"==typeof window?nodeAtob:window.atob}var nodeBtoa=function(a){var b,c=require("buffer").Buffer,d=Object.prototype.toString.call(a).slice(8,-1);return b=c.isBuffer(a)?a:0<=["ArrayBuffer","TypedArray","Uint8Array","Int8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array"].indexOf(d)?c.from(a):new c.from(a.toString(),"binary"),b.toString("base64")},nodeAtob=function(a){var b=require("buffer").Buffer;return new b.from(a,"base64").toString("binary")};
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getEnvBtoa=getEnvBtoa,exports.getEnvAtob=getEnvAtob;/**
* this module handles the difference between window (browser) and node js for specific functions and libraries.
* env.js
*/function getEnvBtoa(){return"undefined"==typeof window?nodeBtoa:window.btoa;// node
}function getEnvAtob(){return"undefined"==typeof window?nodeAtob:window.atob;// node
}var nodeBtoa=function(a){var b,c=require("buffer").Buffer,d=Object.prototype.toString.call(a).slice(8,-1);return b=c.isBuffer(a)?a:0<=["ArrayBuffer","TypedArray","Uint8Array","Int8Array","Uint8ClampedArray","Int16Array","Uint16Array","Int32Array","Uint32Array","Float32Array","Float64Array"].indexOf(d)?c.from(a):new c.from(a.toString(),"binary"),b.toString("base64")},nodeAtob=function(a){var b=require("buffer").Buffer;return new b.from(a,"base64").toString("binary")};

@@ -1,1 +0,22 @@

"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");Object.defineProperty(exports,"__esModule",{value:!0}),exports.pemToBin=pemToBin,exports.binToPem=binToPem;var encoder=_interopRequireWildcard(require("./encoder.js")),supportedPEMTypes={public:"PUBLIC KEY",private:"PRIVATE KEY",certificate:"CERTIFICATE",certRequest:"CERTIFICATE REQUEST"};function pemToBin(a){var b=dearmorPem(a);return encoder.decodeBase64(b)}function binToPem(a,b){var c=encoder.encodeBase64(a);return formatAsPem(c,b)}function formatAsPem(a,b){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");if(!b||"string"!=typeof b)throw new Error("Input arg must be a non-null string");if(0>Object.keys(supportedPEMTypes).indexOf(b))throw new Error("Unsupported type");for(var c=supportedPEMTypes[b],d="-----BEGIN ".concat(c,"-----\n");0<a.length;)d+="".concat(a.substring(0,64),"\n"),a=a.substring(64);return d="".concat(d,"-----END ").concat(c,"-----"),d}function dearmorPem(a){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");var b=/^-----[s]*BEGIN[^-]*-----$/gm,c=/^-----[s]*END[^-]*-----$/gm;try{var d=a.split(b)[1].split(c)[0];return d=d.replace(/\r?\n/g,""),d}catch(a){throw new Error("Invalid format as PEM")}}
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");Object.defineProperty(exports,"__esModule",{value:!0}),exports.pemToBin=pemToBin,exports.binToPem=binToPem;var encoder=_interopRequireWildcard(require("./encoder.js")),supportedPEMTypes={public:"PUBLIC KEY",private:"PRIVATE KEY",certificate:"CERTIFICATE",certRequest:"CERTIFICATE REQUEST"};/**
* Convert PEM armored string to Uint8Array
* @param keydataB64Pem
* @return {Uint8Array}
*/function pemToBin(a){var b=dearmorPem(a);return encoder.decodeBase64(b)}/**
* Convert ArrayBuffer or TypedArray to PEM armored string with a specified type
* @param keydata
* @param type
* @return {string}
*/function binToPem(a,b){var c=encoder.encodeBase64(a);return formatAsPem(c,b)}/**
* Armor the given Base64 string and return PEM formatted string
* @param str
* @param type
* @return {string}
*/function formatAsPem(a,b){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");if(!b||"string"!=typeof b)throw new Error("Input arg must be a non-null string");if(0>Object.keys(supportedPEMTypes).indexOf(b))throw new Error("Unsupported type");for(var c=supportedPEMTypes[b],d="-----BEGIN ".concat(c,"-----\n");0<a.length;)d+="".concat(a.substring(0,64),"\n"),a=a.substring(64);return d="".concat(d,"-----END ").concat(c,"-----"),d}/**
* Dearmor the given PEM string and return Base64 string
* @param str
* @return {string}
*/function dearmorPem(a){if(!a||"string"!=typeof a)throw new Error("Input arg must be a non-null string");// const beginRegExp = RegExp('^-----[\s]*BEGIN[^-]*KEY-----$', 'gm');
// const endRegExp = RegExp('^-----[\s]*END[^-]*KEY-----$', 'gm');
var b=/^-----[s]*BEGIN[^-]*-----$/gm,c=/^-----[s]*END[^-]*-----$/gm;// check if the object starts from 'begin'
try{var d=a.split(b)[1].split(c)[0];return d=d.replace(/\r?\n/g,""),d}catch(a){throw new Error("Invalid format as PEM")}}

4

dist/index.js

@@ -1,1 +0,3 @@

"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");var encoder=_interopRequireWildcard(require("./encoder.js"));var formatter=_interopRequireWildcard(require("./formatter.js"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.formatter=exports.encoder=exports.default=void 0;exports.encoder=encoder;exports.formatter=formatter;var _default={encoder:encoder,formatter:formatter};exports.default=_default;
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard");var encoder=_interopRequireWildcard(require("./encoder.js"));var formatter=_interopRequireWildcard(require("./formatter.js"));Object.defineProperty(exports,"__esModule",{value:!0}),exports.formatter=exports.encoder=exports.default=void 0;exports.encoder=encoder;exports.formatter=formatter;/**
* index.js
**/var _default={encoder:encoder,formatter:formatter};exports.default=_default;
{
"name": "js-encoding-utils",
"version": "0.2.3",
"version": "0.2.4",
"description": "Miscellaneous Encoding Utilities for Crypto-related Objects in JavaScript",

@@ -23,2 +23,3 @@ "main": "dist/index.js",

"pem",
"der",
"base64",

@@ -35,8 +36,8 @@ "base64url",

"@babel/register": "7.0.0",
"babel-eslint": "9.0.0",
"babel-loader": "8.0.2",
"babel-eslint": "10.0.1",
"babel-loader": "8.0.4",
"babel-plugin-istanbul": "5.1.0",
"babel-preset-minify": "^0.4.3",
"chai": "^4.1.2",
"eslint": "5.6.0",
"babel-preset-minify": "^0.5.0",
"chai": "^4.2.0",
"eslint": "5.6.1",
"karma": "3.0.0",

@@ -51,4 +52,4 @@ "karma-chrome-launcher": "^2.2.0",

"mocha-sinon": "^2.1.0",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.0"
"webpack": "^4.20.2",
"webpack-cli": "^3.1.1"
},

@@ -55,0 +56,0 @@ "dependencies": {

Miscellaneous Encoding Utilities for Crypto-related Objects in JavaScript
--
[![CircleCI](https://circleci.com/gh/junkurihara/jsbu.svg?style=svg)](https://circleci.com/gh/junkurihara/jsbu)
[![CircleCI](https://circleci.com/gh/junkurihara/jseu.svg?style=svg)](https://circleci.com/gh/junkurihara/jseu)

@@ -29,3 +29,3 @@ > **WARNING**: At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application.

import jseu from 'js-encoding-utils'; // for npm
import jseu from 'jseu/dist/index.js'; // for git have
import jseu from 'jseu/dist/index.js'; // for github
```

@@ -32,0 +32,0 @@

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc