Socket
Socket
Sign inDemoInstall

webcrypto-core

Package Overview
Dependencies
Maintainers
2
Versions
78
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

webcrypto-core - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

types/utils/buffer_converter.d.ts

129

index.js

@@ -30,2 +30,23 @@ // Copyright (c) 2017, Peculiar Ventures, All rights reserved.

class BufferSourceConverter {
static toArrayBuffer(data) {
if (data instanceof ArrayBuffer) {
return data;
}
if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
return new Uint8Array(data);
}
if (ArrayBuffer.isView(data)) {
return data.buffer;
}
throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
}
static toUint8Array(data) {
return new Uint8Array(this.toArrayBuffer(data));
}
static isBufferSource(data) {
return ArrayBuffer.isView(data) || data instanceof ArrayBuffer;
}
}
class ProviderCrypto {

@@ -207,12 +228,3 @@ async digest(algorithm, data) {

prepareData(data) {
if (data instanceof ArrayBuffer) {
return data;
}
if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
return new Uint8Array(data);
}
if (ArrayBuffer.isView(data)) {
return data.buffer;
}
throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
return BufferSourceConverter.toArrayBuffer(data);
}

@@ -481,3 +493,3 @@ }

};
this.namedCurves = ["P-256", "P-384", "P-521"];
this.namedCurves = ["P-256", "P-384", "P-521", "K-256"];
}

@@ -646,3 +658,3 @@ checkAlgorithmParams(algorithm) {

const preparedAlgorithm = this.prepareAlgorithm(algorithm);
const preparedData = this.prepareData(data);
const preparedData = BufferSourceConverter.toArrayBuffer(data);
const provider = this.getProvider(preparedAlgorithm.name);

@@ -663,3 +675,3 @@ const result = await provider.digest(preparedAlgorithm, preparedData);

const preparedAlgorithm = this.prepareAlgorithm(algorithm);
const preparedData = this.prepareData(data);
const preparedData = BufferSourceConverter.toArrayBuffer(data);
const provider = this.getProvider(preparedAlgorithm.name);

@@ -673,4 +685,4 @@ const result = await provider.sign(preparedAlgorithm, key, preparedData);

const preparedAlgorithm = this.prepareAlgorithm(algorithm);
const preparedData = this.prepareData(data);
const preparedSignature = this.prepareData(signature);
const preparedData = BufferSourceConverter.toArrayBuffer(data);
const preparedSignature = BufferSourceConverter.toArrayBuffer(signature);
const provider = this.getProvider(preparedAlgorithm.name);

@@ -684,3 +696,3 @@ const result = await provider.verify(preparedAlgorithm, key, preparedSignature, preparedData);

const preparedAlgorithm = this.prepareAlgorithm(algorithm);
const preparedData = this.prepareData(data);
const preparedData = BufferSourceConverter.toArrayBuffer(data);
const provider = this.getProvider(preparedAlgorithm.name);

@@ -694,3 +706,3 @@ const result = await provider.encrypt(preparedAlgorithm, key, preparedData, { keyUsage: true });

const preparedAlgorithm = this.prepareAlgorithm(algorithm);
const preparedData = this.prepareData(data);
const preparedData = BufferSourceConverter.toArrayBuffer(data);
const provider = this.getProvider(preparedAlgorithm.name);

@@ -731,3 +743,3 @@ const result = await provider.decrypt(preparedAlgorithm, key, preparedData, { keyUsage: true });

if (["pkcs8", "spki", "raw"].indexOf(format) !== -1) {
const preparedData = this.prepareData(keyData);
const preparedData = BufferSourceConverter.toArrayBuffer(keyData);
return provider.importKey(format, preparedData, preparedAlgorithm, extractable, keyUsages);

@@ -749,3 +761,3 @@ }

const preparedAlgorithm = this.prepareAlgorithm(wrapAlgorithm);
const preparedData = this.prepareData(keyData);
const preparedData = BufferSourceConverter.toArrayBuffer(keyData);
const provider = this.getProvider(preparedAlgorithm.name);

@@ -756,3 +768,3 @@ return provider.encrypt(preparedAlgorithm, wrappingKey, preparedData, { keyUsage: false });

const preparedAlgorithm = this.prepareAlgorithm(unwrapAlgorithm);
const preparedData = this.prepareData(wrappedKey);
const preparedData = BufferSourceConverter.toArrayBuffer(wrappedKey);
const provider = this.getProvider(preparedAlgorithm.name);

@@ -790,14 +802,2 @@ let keyData = await provider.decrypt(preparedAlgorithm, unwrappingKey, preparedData, { keyUsage: false });

}
prepareData(data) {
if (data instanceof ArrayBuffer) {
return data;
}
if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
return new Uint8Array(data);
}
if (ArrayBuffer.isView(data)) {
return data.buffer;
}
throw new TypeError("The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
}
getProvider(name) {

@@ -817,2 +817,65 @@ const provider = this.providers.get(name);

class PemConverter {
static toArrayBuffer(pem) {
const base64 = pem
.replace(/-{5}(BEGIN|END) .*-{5}/g, "")
.replace("\r", "")
.replace("\n", "");
return pvtsutils.Convert.FromBase64(base64);
}
static toUint8Array(pem) {
const bytes = this.toArrayBuffer(pem);
return new Uint8Array(bytes);
}
static fromBufferSource(buffer, tag) {
const base64 = pvtsutils.Convert.ToBase64(buffer);
let sliced;
let offset = 0;
const rows = [];
while (true) {
sliced = base64.slice(offset, offset = offset + 64);
if (sliced.length) {
rows.push(sliced);
if (sliced.length < 64) {
break;
}
}
else {
break;
}
}
const upperCaseTag = tag.toUpperCase();
return `-----BEGIN ${upperCaseTag}-----\n${rows.join("\n")}\n-----END ${upperCaseTag}-----`;
}
static isPEM(data) {
return /-----BEGIN .+-----[A-Za-z0-9+\/\+\=\s\n]+-----END .+-----/i.test(data);
}
static getTagName(pem) {
if (!this.isPEM(pem)) {
throw new Error("Bad parameter. Incoming data is not right PEM");
}
const res = /-----BEGIN (.+)-----/.exec(pem);
if (!res) {
throw new Error("Cannot get tag from PEM");
}
return res[1];
}
static hasTagName(pem, tagName) {
const tag = this.getTagName(pem);
return tagName.toLowerCase() === tag.toLowerCase();
}
static isCertificate(pem) {
return this.hasTagName(pem, "certificate");
}
static isCertificateRequest(pem) {
return this.hasTagName(pem, "certificate request");
}
static isCRL(pem) {
return this.hasTagName(pem, "x509 crl");
}
static isPublicKey(pem) {
return this.hasTagName(pem, "public key");
}
}
exports.AlgorithmError = AlgorithmError;

@@ -845,1 +908,3 @@ exports.CryptoError = CryptoError;

exports.CryptoKey = CryptoKey;
exports.PemConverter = PemConverter;
exports.BufferSourceConverter = BufferSourceConverter;
{
"name": "webcrypto-core",
"version": "1.0.1",
"version": "1.0.2",
"description": "Common layer to be used by crypto libraries based on WebCrypto API for input validation.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -13,2 +13,3 @@ export * from "./errors";

export * from "./key";
export * from "./utils";
export * from "./types";

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

import { KeyUsages } from "./types";
export interface CryptoKeyPair {
privateKey: CryptoKey;
publicKey: CryptoKey;
}
import { KeyUsages, NativeCryptoKey } from "./types";
export interface KeyAlgorithm extends Algorithm {
}
export declare class CryptoKey {
static create<T extends CryptoKey>(this: {
new (): T;
}, algorithm: KeyAlgorithm, type: KeyType, extractable: boolean, usages: KeyUsages): T;
export declare class CryptoKey implements NativeCryptoKey {
static create<T extends CryptoKey>(this: new () => T, algorithm: KeyAlgorithm, type: KeyType, extractable: boolean, usages: KeyUsages): T;
static isKeyType(data: any): data is KeyType;

@@ -13,0 +7,0 @@ algorithm: KeyAlgorithm;

@@ -24,5 +24,4 @@ import { CryptoKey } from "./key";

protected prepareAlgorithm(algorithm: AlgorithmIdentifier): Algorithm | HashedAlgorithm;
protected prepareData(data: any): ArrayBuffer;
protected getProvider(name: string): ProviderCrypto;
protected checkCryptoKey(key: CryptoKey): void;
}

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

export declare type NativeCryptoKey = CryptoKey;
export declare type HexString = string;
export declare type KeyUsages = KeyUsage[];

@@ -11,1 +13,72 @@ export declare type ProviderKeyUsage = KeyUsages;

}
export declare type ImportAlgorithms = Algorithm | RsaHashedImportParams | EcKeyImportParams;
/**
* Base generic class for crypto storages
*/
export interface CryptoStorage<T> {
/**
* Returns list of indexes from storage
*/
keys(): Promise<string[]>;
/**
* Returns index of item in storage
* @param item Crypto item
* @returns Index of item in storage otherwise null
*/
indexOf(item: T): Promise<string | null>;
/**
* Add crypto item to storage and returns it's index
*/
setItem(item: T): Promise<string>;
/**
* Returns crypto item from storage by index
* @param index index of crypto item
* @returns Crypto item
* @throws Throws Error when cannot find crypto item in storage
*/
getItem(index: string): Promise<T>;
/**
* Returns `true` if item is in storage otherwise `false`
* @param item Crypto item
*/
hasItem(item: T): Promise<boolean>;
/**
* Removes all items from storage
*/
clear(): Promise<void>;
/**
* Removes crypto item from storage by index
* @param index Index of crypto storage
*/
removeItem(index: string): Promise<void>;
}
export interface CryptoKeyStorage extends CryptoStorage<CryptoKey> {
getItem(index: string): Promise<CryptoKey>;
getItem(index: string, algorithm: ImportAlgorithms, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
}
export declare type CryptoCertificateFormat = "raw" | "pem";
export declare type CryptoCertificateType = "x509" | "request";
export interface CryptoCertificate {
type: CryptoCertificateType;
publicKey: CryptoKey;
}
export interface CryptoX509Certificate extends CryptoCertificate {
notBefore: Date;
notAfter: Date;
serialNumber: HexString;
issuerName: string;
subjectName: string;
}
export interface CryptoX509CertificateRequest extends CryptoCertificate {
subjectName: string;
}
export interface CryptoCertificateStorage extends CryptoStorage<CryptoCertificate> {
getItem(index: string): Promise<CryptoCertificate>;
getItem(index: string, algorithm: ImportAlgorithms, keyUsages: KeyUsage[]): Promise<CryptoCertificate>;
exportCert(format: CryptoCertificateFormat, item: CryptoCertificate): Promise<ArrayBuffer | string>;
exportCert(format: "raw", item: CryptoCertificate): Promise<ArrayBuffer>;
exportCert(format: "pem", item: CryptoCertificate): Promise<string>;
importCert(format: CryptoCertificateFormat, data: BufferSource | string, algorithm: ImportAlgorithms, keyUsages: KeyUsage[]): Promise<CryptoCertificate>;
importCert(format: "raw", data: BufferSource, algorithm: ImportAlgorithms, keyUsages: KeyUsage[]): Promise<CryptoCertificate>;
importCert(format: "pem", data: string, algorithm: ImportAlgorithms, keyUsages: KeyUsage[]): Promise<CryptoCertificate>;
}
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