Giter VIP home page Giter VIP logo

pvtsutils's Introduction

License: MIT Coverage Status Test

pvtsutils

pvtsutils is a set of common utility functions used in various Peculiar Ventures TypeScript based projects.

Install

npm install pvtsutils

Using

const utils = require("pvtsutils");

The pvtsutils namespace will always be available globally and also supports AMD loaders.

Types

There is an index.d.ts file which makes easy to use current module as external

Helpers

Convert

Helps to convert string to ArrayBuffer and back

Convert support next encoding types hex, utf8, binary, base64, base64url. utf8 is default.

Examples

Converts string to buffer

const buf = Convert.FromString("some string", "hex");

Converts buffer to string

const str = Convert.ToString(buf, "utf8");

assign

Copies properties from objects to a target object. More info

Example

const obj = assign({}, {name: "Bob"}, {age: 12}); // {name: "Bob", age: 12}

isEqual

Returns true if 2 ArrayBuffers are equal

combine

Combines some ArrayBuffer values

Example

const buf1 = new Uint8Array([1, 2, 3]);
const buf2 = new Uint8Array([4, 5, 6]);
const buf = combine(buf1, buf2); // [1, 2, 3, 4, 5, 6]

Some example capabilities included in pvtsutils include:

  • Converting values to adn from hex,
  • Converting values to and from base64,
  • Working with base64 url,
  • Working with binary data.
  • And more...

pvtsutils's People

Contributors

microshine avatar rmhrisk avatar sander avatar ai avatar jstewmon avatar

Stargazers

Yaron Shani avatar  avatar Dmytro Donskov avatar

Watchers

James Cloos avatar  avatar  avatar Dmytro Donskov avatar  avatar

pvtsutils's Issues

RangeError in webcrypto-core.padStart

Since the version change to 1.3.3 of pvtsutils we have noticed a couple of errors that are currently causing ECDSA certificate generation to fail.

If we pin the version back to 1.3.2 the issue no longer happens.

When running locally babel complains with the following error: The argument can't be less than 0

RangeError: The argument can't be less than 0
    at module.exports (/node_modules/@babel/node/node_modules/core-js/internals/to-positive-integer.js:8:25)
    at module.exports (/node_modules/@babel/node/node_modules/core-js/internals/to-offset.js:7:16)
    at Uint8Array.set (/node_modules/@babel/node/node_modules/core-js/modules/es.typed-array.set.js:36:16)
    at Function.padStart (/node_modules/webcrypto-core/build/webcrypto-core.js:972:13)
    at Function.encodeSignature (/node_modules/webcrypto-core/build/webcrypto-core.js:940:22)
    at Function.sign (/node_modules/@peculiar/webcrypto/build/webcrypto.js:1575:54)
    at EcdsaProvider.onSign (node_modules/@peculiar/webcrypto/build/webcrypto.js:1737:25)
    at EcdsaProvider.sign (/node_modules/webcrypto-core/build/webcrypto-core.js:157:28)
    at SubtleCrypto.sign (/node_modules/webcrypto-core/build/webcrypto-core.js:1405:39)
    at Function.create /node_modules/@peculiar/x509/build/x509.cjs.js:2407:35)

Which is probably some extra checks babel performs. However, without babel we get the following error:

RangeError: offset is out of bounds
    at Uint8Array.set (<anonymous>)
    at Function.padStart (/node_modules/webcrypto-core/build/webcrypto-core.js:972:13)
    at Function.encodeSignature (/node_modules/webcrypto-core/build/webcrypto-core.js:940:22)
    at Function.sign (/node_modules/@peculiar/webcrypto/build/webcrypto.js:1575:54)
    at EcdsaProvider.onSign (/node_modules/@peculiar/webcrypto/build/webcrypto.js:1737:25)
    at EcdsaProvider.sign (/node_modules/webcrypto-core/build/webcrypto-core.js:157:28)
    at SubtleCrypto.sign (/node_modules/webcrypto-core/build/webcrypto-core.js:1405:39)
    at Function.create (/node_modules/@peculiar/x509/build/x509.cjs.js:2407:35)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

The following code snippet can recreate the problem:

const x509 = require('@peculiar/x509');
const {Crypto} = require('@peculiar/webcrypto');
const {randomBytes} = require('crypto');
const moment = require('moment');

const defaultSerialBytes = 19;
const getRandomSerialNumber = () => {
  const sn = randomBytes(defaultSerialBytes).toString('hex');
  return `01${sn}`;
};

(async () => {
  const alg = {
    name: 'ECDSA',
    namedCurve: 'P-256',
    hash: 'SHA-256',
    usages: ['sign', 'verify'],
  };

  const crypto = new Crypto();
  x509.cryptoProvider.set(crypto);
  const keys = await crypto.subtle.generateKey(alg, true, alg.usages);

  const cert = await x509.X509CertificateGenerator.createSelfSigned({
    serialNumber: getRandomSerialNumber(),
    name: `CN=test.com`,
    notBefore: moment().toDate(), 
    notAfter: moment().add(10, 'days').toDate(),
    signingAlgorithm: alg,
    keys,
    extensions: [
      new x509.BasicConstraintsExtension(
        false,
        0,
        true
      ),
      await x509.SubjectKeyIdentifierExtension.create(keys.publicKey),
    ],
  });

  const privateKey = await crypto.subtle.exportKey('pkcs8', keys.privateKey);
  const publicKey = await crypto.subtle.exportKey('spki', keys.publicKey);

  return { certificate: cert.toString('pem'), privateKey, publicKey };

})().catch(console.error);

TypeError: Buffer.isBuffer is not a function

Hi. thanks also for the great set of libraries :)

I ran into an issue when using @peculiar/x509:

error:  TypeError: Buffer.isBuffer is not a function
    at Function.toView (index.js:26:54)
    at Function.toUint8Array (index.js:23:1)
    at Function.ToBinary (index.js:237:1)
    at Function.toArrayBuffer (x509.es.js:248:31)
    at new PemData (x509.es.js:266:1)
    at new PublicKey (x509.es.js:291:1)
    at Function.create (x509.es.js:827:1)

I am getting this error when using the function

x509.SubjectKeyIdentifierExtension.create(keys.publicKey)

also using following function:

x509.PemConverter.encode( pkcs8, "private key")

both uses the x509.PemConverter class which uses pvtsutils under the hood...

When debbuging the error I can see the the Buffer object looks following:
{INSPECT_MAX_BYTES: 50, kMaxLength: 2147483647, Buffer: ฦ’, SlowBuffer: ฦ’}
and the attribute Buffer.Buffer has the the method isBuffer()

Buffer.Buffer.isBuffer()

if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {

I am running the x509 library version 1.6.1 from yarn which has a dependency to pvtsutils 1.2.1 which are both the latest. Can someone help me here?

TypeError: The provided value is not of type '(ArrayBuffer or ArrayBufferView)

Hi. First off, thanks for a great set of libraries.

Ran into an issue when using @peculiar/x509:

TypeError: The provided value is not of type '(ArrayBuffer or ArrayBufferView)'
  at Function.toView (../node_modules/pvtsutils/build/index.js:35:17)
  at Function.toUint8Array (../node_modules/pvtsutils/build/index.js:23:23)
  at Function.ToHex (../node_modules/pvtsutils/build/index.js:166:45)
  at AuthorityKeyIdentifierExtension.onInit (../node_modules/@peculiar/x509/build/x509.cjs.js:671:44)
  at new AsnData (../node_modules/@peculiar/x509/build/x509.cjs.js:51:18)
  at new Extension (../node_modules/@peculiar/x509/build/x509.cjs.js:75:9)
  at new AuthorityKeyIdentifierExtension (../node_modules/@peculiar/x509/build/x509.cjs.js:645:13)

Tracked it down to these LoCs in @peculiar/x509: https://github.com/PeculiarVentures/x509/blob/master/src/extensions/authority_key_identifier.ts#L104-L107

And to this function in pvtsutils: https://github.com/PeculiarVentures/pvtsutils/blob/master/src/buffer_source_converter.ts#L35-L46

Should the toView method also accept an object-like input like { buffer: ArrayBuffer }?

Found a similar piece of code that accepts it this way: https://github.com/PeculiarVentures/asn1-schema/blob/988241d5c3c8fa4ef6f581b55b7354330ead0156/packages/schema/src/parser.ts#L27

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.