Socket
Socket
Sign inDemoInstall

js-encoding-utils

Package Overview
Dependencies
0
Maintainers
1
Versions
32
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.6.2 to 0.6.6

30

dist/encoder.js

@@ -32,3 +32,3 @@ "use strict";

*/
exports.encodeBase64 = function (data) {
var encodeBase64 = function (data) {
var str = '';

@@ -38,6 +38,7 @@ if (typeof data === 'string')

else
str = exports.arrayBufferToString(data);
str = (0, exports.arrayBufferToString)(data);
var btoa = env.getEnvBtoa();
return btoa(str);
};
exports.encodeBase64 = encodeBase64;
/**

@@ -48,8 +49,9 @@ * Decode Base64 to Uint8Array

*/
exports.decodeBase64 = function (str) {
var decodeBase64 = function (str) {
var atob = env.getEnvAtob();
var binary = atob(str);
var data = exports.stringToArrayBuffer(binary);
var data = (0, exports.stringToArrayBuffer)(binary);
return getAsciiIfAscii(data);
};
exports.decodeBase64 = decodeBase64;
/**

@@ -97,3 +99,4 @@ * if input data is an ArrayBuffer or TypedArray, it would be returned as Uint8Array

*/
exports.encodeBase64Url = function (data) { return exports.encodeBase64(data).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); };
var encodeBase64Url = function (data) { return (0, exports.encodeBase64)(data).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); };
exports.encodeBase64Url = encodeBase64Url;
/**

@@ -104,7 +107,8 @@ * Decode Base64Url string to Uint8Array

*/
exports.decodeBase64Url = function (str) {
var decodeBase64Url = function (str) {
str = str.replace(/-/g, '+').replace(/_/g, '/');
// str = str + "=".repeat(str.length % 4); // this sometimes causes error...
return exports.decodeBase64(str);
return (0, exports.decodeBase64)(str);
};
exports.decodeBase64Url = decodeBase64Url;
/**

@@ -115,3 +119,3 @@ * Encode ArrayBuffer or TypedArray to hex string

*/
exports.arrayBufferToHexString = function (data) {
var arrayBufferToHexString = function (data) {
var arr = sanitizeTypedArrayAndArrayBuffer(data);

@@ -126,2 +130,3 @@ var hexStr = '';

};
exports.arrayBufferToHexString = arrayBufferToHexString;
/**

@@ -132,3 +137,3 @@ * Decode hex string to Uint8Array

*/
exports.hexStringToArrayBuffer = function (str) {
var hexStringToArrayBuffer = function (str) {
var arr = [];

@@ -140,2 +145,3 @@ var len = str.length;

};
exports.hexStringToArrayBuffer = hexStringToArrayBuffer;
/**

@@ -146,3 +152,3 @@ * Encode ArrayBuffer or TypedArray to string with code (like output of legacy atob)

*/
exports.arrayBufferToString = function (data) {
var arrayBufferToString = function (data) {
var bytes = sanitizeTypedArrayAndArrayBuffer(data);

@@ -153,2 +159,3 @@ var arr = new Array(bytes.length);

};
exports.arrayBufferToString = arrayBufferToString;
/**

@@ -159,6 +166,7 @@ * Decode string with code (like output of legacy atob) to Uint8Array

*/
exports.stringToArrayBuffer = function (str) {
var stringToArrayBuffer = function (str) {
var bytes = new Uint8Array(str.length);
return bytes.map(function (_x, i) { return str.charCodeAt(i); });
};
exports.stringToArrayBuffer = stringToArrayBuffer;
//# sourceMappingURL=encoder.js.map

@@ -8,3 +8,3 @@ "use strict";

exports.getEnvAtob = exports.getEnvBtoa = void 0;
exports.getEnvBtoa = function () {
var getEnvBtoa = function () {
if (typeof window !== 'undefined')

@@ -15,3 +15,4 @@ return window.btoa; // browser

};
exports.getEnvAtob = function () {
exports.getEnvBtoa = getEnvBtoa;
var getEnvAtob = function () {
if (typeof window !== 'undefined')

@@ -22,2 +23,3 @@ return window.atob; // browser

};
exports.getEnvAtob = getEnvAtob;
var nodeBtoa = function (str) {

@@ -24,0 +26,0 @@ if (typeof Buffer === 'undefined')

@@ -39,6 +39,7 @@ "use strict";

*/
exports.pemToBin = function (keydataB64Pem) {
var pemToBin = function (keydataB64Pem) {
var keydataB64 = dearmorPem(keydataB64Pem);
return encoder.decodeBase64(keydataB64);
};
exports.pemToBin = pemToBin;
/**

@@ -50,6 +51,7 @@ * Convert ArrayBuffer or TypedArray to PEM armored string with a specified type

*/
exports.binToPem = function (keydata, type) {
var binToPem = function (keydata, type) {
var keydataB64 = encoder.encodeBase64(keydata);
return formatAsPem(keydataB64, type);
};
exports.binToPem = binToPem;
/**

@@ -56,0 +58,0 @@ * Armor the given Base64 string and return PEM formatted string

@@ -0,13 +1,14 @@

/// <reference types="node" />
export declare namespace encoder {
const encodeBase64: (data: string | ArrayBuffer | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array) => string;
const encodeBase64: (data: string | ArrayBuffer | NodeJS.TypedArray) => string;
const decodeBase64: (str: string) => string | Uint8Array;
const encodeBase64Url: (data: ArrayBuffer | Uint8Array) => string;
const decodeBase64Url: (str: string) => string | Uint8Array;
const arrayBufferToHexString: (data: ArrayBuffer | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array) => string;
const arrayBufferToHexString: (data: ArrayBuffer | NodeJS.TypedArray) => string;
const hexStringToArrayBuffer: (str: string) => Uint8Array;
const stringToArrayBuffer: (str: string) => Uint8Array;
const arrayBufferToString: (data: ArrayBuffer | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array) => string;
const arrayBufferToString: (data: ArrayBuffer | NodeJS.TypedArray) => string;
}
export declare namespace formatter {
const binToPem: (keydata: ArrayBuffer | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array, type: "public" | "private" | "encryptedPrivate" | "certificate" | "certRequest") => string;
const binToPem: (keydata: ArrayBuffer | NodeJS.TypedArray, type: "public" | "private" | "encryptedPrivate" | "certificate" | "certRequest") => string;
const pemToBin: (keydataB64Pem: string) => string | Uint8Array;

@@ -14,0 +15,0 @@ }

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

!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.jseu=e():r.jseu=e()}(this,(function(){return(()=>{"use strict";var r={279:(r,e,n)=>{n.r(e),n.d(e,{default:()=>B,encoder:()=>t,formatter:()=>o});var t,o,f=function(r){if("undefined"==typeof Buffer)throw new Error("UnsupportedEnvironment");return Buffer.from(r.toString(),"binary").toString("base64")},i=function(r){if("undefined"==typeof Buffer)throw new Error("UnsupportedEnvironment");return Buffer.from(r,"base64").toString("binary")},u=function(r){var e;return e="string"==typeof r?r:y(r),("undefined"!=typeof window?window.btoa:f)(e)},a=function(r){var e=("undefined"!=typeof window?window.atob:i)(r),n=E(e);return p(n)},c=function(r){return r instanceof Uint8Array?r:ArrayBuffer.isView(r)&&void 0!==r.buffer?new Uint8Array(r.buffer):new Uint8Array(r)},p=function(r){for(var e=!0,n=0;n<r.length;n++)if(r[n]>126||r[n]<32&&13!==r[n]&&10!==r[n]){e=!1;break}var t=null;if(e)for(t="",n=0;n<r.length;n++)t+=String.fromCharCode(r[n]);else t=r;return t},d=function(r){return u(r).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")},s=function(r){return r=r.replace(/-/g,"+").replace(/_/g,"/"),a(r)},l=function(r){for(var e=c(r),n="",t=0;t<e.length;t++){var o=(255&e[t]).toString(16);n+=o=1===o.length?"0"+o:o}return n},g=function(r){for(var e=[],n=r.length,t=0;t<n;t+=2)e.push(parseInt(r.substr(t,2),16));return new Uint8Array(e)},y=function(r){var e=c(r),n=new Array(e.length);return e.forEach((function(r,e){n[e]=r})),String.fromCharCode.apply(null,n)},E=function(r){return new Uint8Array(r.length).map((function(e,n){return r.charCodeAt(n)}))},b={public:"PUBLIC KEY",private:"PRIVATE KEY",encryptedPrivate:"ENCRYPTED PRIVATE KEY",certificate:"CERTIFICATE",certRequest:"CERTIFICATE REQUEST"},v=function(r){var e=h(r);return a(e)},m=function(r,e){var n=u(r);return w(n,e)},w=function(r,e){for(var n=b[e],t="-----BEGIN "+n+"-----\n";r.length>0;)t+=r.substring(0,64)+"\n",r=r.substring(64);return t+"-----END "+n+"-----"},h=function(r){var e=RegExp("^-----[s]*BEGIN[^-]*-----$","gm"),n=RegExp("^-----[s]*END[^-]*-----$","gm");try{var t=r.split(e)[1].split(n)[0];return t.replace(/\r?\n/g,"")}catch(r){throw new Error("Invalid format as PEM")}};!function(r){r.encodeBase64=u,r.decodeBase64=a,r.encodeBase64Url=d,r.decodeBase64Url=s,r.arrayBufferToHexString=l,r.hexStringToArrayBuffer=g,r.stringToArrayBuffer=E,r.arrayBufferToString=y}(t||(t={})),function(r){r.binToPem=m,r.pemToBin=v}(o||(o={}));const B={encoder:t,formatter:o}}},e={};function n(t){if(e[t])return e[t].exports;var o=e[t]={exports:{}};return r[t](o,o.exports,n),o.exports}return n.d=(r,e)=>{for(var t in e)n.o(e,t)&&!n.o(r,t)&&Object.defineProperty(r,t,{enumerable:!0,get:e[t]})},n.o=(r,e)=>Object.prototype.hasOwnProperty.call(r,e),n.r=r=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})},n(279)})()}));
!function(r,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.jseu=e():r.jseu=e()}(this,(function(){return(()=>{"use strict";var r={d:(e,n)=>{for(var t in n)r.o(n,t)&&!r.o(e,t)&&Object.defineProperty(e,t,{enumerable:!0,get:n[t]})},o:(r,e)=>Object.prototype.hasOwnProperty.call(r,e),r:r=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})}},e={};r.r(e),r.d(e,{default:()=>h,encoder:()=>n,formatter:()=>t});var n,t,o=function(r){if("undefined"==typeof Buffer)throw new Error("UnsupportedEnvironment");return Buffer.from(r.toString(),"binary").toString("base64")},f=function(r){if("undefined"==typeof Buffer)throw new Error("UnsupportedEnvironment");return Buffer.from(r,"base64").toString("binary")},i=function(r){var e;return e="string"==typeof r?r:g(r),("undefined"!=typeof window?window.btoa:o)(e)},u=function(r){var e=("undefined"!=typeof window?window.atob:f)(r),n=y(e);return c(n)},a=function(r){return r instanceof Uint8Array?r:ArrayBuffer.isView(r)&&void 0!==r.buffer?new Uint8Array(r.buffer):new Uint8Array(r)},c=function(r){for(var e=!0,n=0;n<r.length;n++)if(r[n]>126||r[n]<32&&13!==r[n]&&10!==r[n]){e=!1;break}var t=null;if(e)for(t="",n=0;n<r.length;n++)t+=String.fromCharCode(r[n]);else t=r;return t},d=function(r){return i(r).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")},p=function(r){return r=r.replace(/-/g,"+").replace(/_/g,"/"),u(r)},l=function(r){for(var e=a(r),n="",t=0;t<e.length;t++){var o=(255&e[t]).toString(16);n+=o=1===o.length?"0"+o:o}return n},s=function(r){for(var e=[],n=r.length,t=0;t<n;t+=2)e.push(parseInt(r.substr(t,2),16));return new Uint8Array(e)},g=function(r){var e=a(r),n=new Array(e.length);return e.forEach((function(r,e){n[e]=r})),String.fromCharCode.apply(null,n)},y=function(r){return new Uint8Array(r.length).map((function(e,n){return r.charCodeAt(n)}))},E={public:"PUBLIC KEY",private:"PRIVATE KEY",encryptedPrivate:"ENCRYPTED PRIVATE KEY",certificate:"CERTIFICATE",certRequest:"CERTIFICATE REQUEST"},b=function(r){var e=w(r);return u(e)},v=function(r,e){var n=i(r);return m(n,e)},m=function(r,e){for(var n=E[e],t="-----BEGIN "+n+"-----\n";r.length>0;)t+=r.substring(0,64)+"\n",r=r.substring(64);return t+"-----END "+n+"-----"},w=function(r){var e=RegExp("^-----[s]*BEGIN[^-]*-----$","gm"),n=RegExp("^-----[s]*END[^-]*-----$","gm");try{var t=r.split(e)[1].split(n)[0];return t.replace(/\r?\n/g,"")}catch(r){throw new Error("Invalid format as PEM")}};!function(r){r.encodeBase64=i,r.decodeBase64=u,r.encodeBase64Url=d,r.decodeBase64Url=p,r.arrayBufferToHexString=l,r.hexStringToArrayBuffer=s,r.stringToArrayBuffer=y,r.arrayBufferToString=g}(n||(n={})),function(r){r.binToPem=v,r.pemToBin=b}(t||(t={}));const h={encoder:n,formatter:t};return e})()}));
{
"name": "js-encoding-utils",
"version": "0.6.2",
"version": "0.6.6",
"description": "Miscellaneous Encoding Utilities for Crypto-related Objects in JavaScript",

@@ -40,22 +40,22 @@ "main": "dist/index.js",

"devDependencies": {
"@types/jest": "26.0.14",
"@types/node": "14.11.8",
"@typescript-eslint/eslint-plugin": "4.4.1",
"@typescript-eslint/parser": "4.4.1",
"can-npm-publish": "1.3.2",
"cross-env": "7.0.2",
"eslint": "7.11.0",
"jasmine-core": "3.6.0",
"jest": "26.5.3",
"karma": "5.2.3",
"@types/jest": "27.0.3",
"@types/node": "16.11.9",
"@typescript-eslint/eslint-plugin": "5.4.0",
"@typescript-eslint/parser": "5.4.0",
"can-npm-publish": "1.3.6",
"cross-env": "7.0.3",
"eslint": "8.3.0",
"jasmine-core": "3.10.1",
"jest": "27.3.1",
"karma": "6.3.9",
"karma-chrome-launcher": "3.1.0",
"karma-jasmine": "4.0.1",
"karma-typescript": "5.2.0",
"ts-jest": "26.4.1",
"ts-loader": "8.0.5",
"typescript": "4.0.3",
"webpack": "5.1.2",
"webpack-cli": "4.0.0"
"karma-typescript": "5.5.2",
"ts-jest": "27.0.7",
"ts-loader": "9.2.6",
"typescript": "4.4.4",
"webpack": "5.64.2",
"webpack-cli": "4.9.1"
},
"dependencies": {}
}

@@ -5,3 +5,2 @@ Miscellaneous Encoding Utilities for Crypto-related Objects in JavaScript

[![CircleCI](https://circleci.com/gh/junkurihara/jseu.svg?style=svg)](https://circleci.com/gh/junkurihara/jseu)
[![Dependencies](https://david-dm.org/junkurihara/jseu.svg)](https://david-dm.org/junkurihara/jseu)
[![codecov](https://codecov.io/gh/junkurihara/jseu/branch/develop/graph/badge.svg)](https://codecov.io/gh/junkurihara/jseu)

@@ -22,3 +21,3 @@ [![Maintainability](https://api.codeclimate.com/v1/badges/771abd93ae5d986f1e0a/maintainability)](https://codeclimate.com/github/junkurihara/jseu/maintainability)

- From npm/yarn:
```shell

@@ -28,3 +27,3 @@ $ npm install --save js-encoding-utils // npm

```
- From GitHub:

@@ -34,3 +33,3 @@ ```shell

```
Then you should import the package as follows.

@@ -41,7 +40,7 @@ ```javascript

```
# Usage
## Base64 <-> Binary
```javascript
// msg is an ArrayBuffer or Typed Array
// msg is an ArrayBuffer or Typed Array
const encoded = jseu.encoder.encodeBase64(msg);

@@ -51,3 +50,3 @@ // now you get Base64 string

const decoded = jseu.encoder.decodeBase64(encoded);
// now you get the original message in Uint8Array
// now you get the original message in Uint8Array
```

@@ -57,3 +56,3 @@

```javascript
// msg is an ArrayBuffer or Typed Array
// msg is an ArrayBuffer or Typed Array
const encoded = jseu.encoder.encodeBase64Url(msg);

@@ -63,3 +62,3 @@ // now you get Base64Url string

const decoded = jseu.encoder.decodeBase64Url(encoded);
// now you get the original message in Uint8Array
// now you get the original message in Uint8Array
```

@@ -69,3 +68,3 @@

```javascript
// msg is an ArrayBuffer or Typed Array
// msg is an ArrayBuffer or Typed Array
const encoded = jseu.encoder.arrayBufferToHexString(msg);

@@ -72,0 +71,0 @@ // now you get the hex-stringified message string

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc