Skip to content

Commit

Permalink
Merge 68bde69 into 970fd2c
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Mar 2, 2019
2 parents 970fd2c + 68bde69 commit e963ddf
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 1,109 deletions.
1,101 changes: 5 additions & 1,096 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Common layer to be used by crypto libraries based on WebCrypto API for input validation.",
"main": "build/webcrypto-core.js",
"module": "build/webcrypto-core.es.js",
"types": "types/index.d.ts",
"types": "build/webcrypto-core.d.ts",
"scripts": {
"prepare": "npm run build",
"build": "npm run build:defs && npm run build:js",
Expand Down Expand Up @@ -44,7 +44,7 @@
"nyc": "^13.3.0",
"reflect-metadata": "^0.1.13",
"rollup": "^1.1.2",
"rollup-plugin-typescript": "^1.0.0",
"rollup-plugin-dts": "^0.13.0",
"ts-node": "^8.0.1",
"typescript": "^3.2.4"
},
Expand Down
28 changes: 18 additions & 10 deletions rollup.config.js
@@ -1,14 +1,14 @@
import typescript from "rollup-plugin-typescript";
import ts from "typescript";

const pkg = require("./package.json");
// @ts-check
import { ts, dts } from "rollup-plugin-dts";
// @ts-ignore
import pkg from "./package.json";

const banner = [
"/**",
" * Copyright (c) 2019, Peculiar Ventures, All rights reserved.",
" */",
"",
];
].join("\n");
const input = "src/index.ts";
const external = Object.keys(pkg.dependencies);

Expand All @@ -17,28 +17,36 @@ export default [
{
input,
plugins: [
typescript({ typescript: ts, target: "esnext", removeComments: true }),
ts({
compilerOptions: {
removeComments: true,
},
}),
],
external,
output: [
{
banner: banner.join("\n"),
banner,
file: pkg.main,
format: "cjs",
},
{
banner,
file: pkg.module,
format: "es",
}
]
},
// lib
{
input,
plugins: [
typescript({ typescript: ts, target: "esnext", removeComments: true }),
dts(),
],
external,
output: [
{
banner: banner.join("\n"),
file: pkg.module,
file: pkg.types,
format: "es",
}
]
Expand Down
40 changes: 40 additions & 0 deletions src/hkdf/hkdf.ts
@@ -0,0 +1,40 @@
import { ProviderCrypto } from "../provider";
import { KeyUsages } from "../types";
import { BufferSourceConverter } from "../utils";

export abstract class HkdfProvider extends ProviderCrypto {

public name = "HKDF";
public hashAlgorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];
public usages: KeyUsages = ["deriveKey", "deriveBits"];

public checkAlgorithmParams(algorithm: HkdfParams) {
// hash
this.checkRequiredProperty(algorithm, "hash");
this.checkHashAlgorithm(algorithm.hash as Algorithm, this.hashAlgorithms);

// salt
this.checkRequiredProperty(algorithm, "salt");
if (!BufferSourceConverter.isBufferSource(algorithm.salt)) {
throw new TypeError("salt: Is not of type '(ArrayBuffer or ArrayBufferView)'");
}

// info
this.checkRequiredProperty(algorithm, "info");
if (!BufferSourceConverter.isBufferSource(algorithm.info)) {
throw new TypeError("salt: Is not of type '(ArrayBuffer or ArrayBufferView)'");
}
}

public checkImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]) {
super.checkImportKey(format, keyData, algorithm, extractable, keyUsages);
if (extractable) {
// If extractable is not false, then throw a SyntaxError
throw new SyntaxError("extractable: Must be False");
}
}

public abstract onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey>;
public abstract onDeriveBits(algorithm: Pbkdf2Params, baseKey: CryptoKey, length: number): Promise<ArrayBuffer>;

}
1 change: 1 addition & 0 deletions src/hkdf/index.ts
@@ -0,0 +1 @@
export * from "./hkdf";
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -5,6 +5,7 @@ export * from "./rsa";
export * from "./ec";
export * from "./hmac";
export * from "./pbkdf";
export * from "./hkdf";
export * from "./crypto";
export * from "./provider";
export * from "./storage";
Expand Down
67 changes: 67 additions & 0 deletions test/hkdf.ts
@@ -0,0 +1,67 @@
import assert from "assert";
import "reflect-metadata";
import { HkdfProvider, OperationError } from "../src";

context("HKDF", () => {

const provider = Reflect.construct(HkdfProvider, []) as HkdfProvider;

context("checkAlgorithmParams", () => {

it("error if `hash` is missing", () => {
assert.throws(() => {
provider.checkAlgorithmParams({ salt: new Uint8Array(4), info: new Uint8Array(4) } as any);
}, Error);
});

it("error if `hash` is wrong", () => {
assert.throws(() => {
provider.checkAlgorithmParams({ hash: { name: "WRONG" }, salt: new Uint8Array(4), info: new Uint8Array(4) } as any);
}, OperationError);
});

it("error if `salt` is missing", () => {
assert.throws(() => {
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, info: new Uint8Array(4) } as any);
}, Error);
});

it("error if `salt` wrong type", () => {
assert.throws(() => {
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, salt: "wrong", info: new Uint8Array(4) } as any);
}, TypeError);
});

it("error if `info` is missing", () => {
assert.throws(() => {
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, salt: new Uint8Array(4) } as any);
}, Error);
});

it("error if `info` wrong type", () => {
assert.throws(() => {
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, info: "wrong", salt: new Uint8Array(4) } as any);
}, TypeError);
});

it("correct value", () => {
provider.checkAlgorithmParams({ hash: { name: "SHA-256" }, salt: new Uint8Array(4), info: new Uint8Array(4) } as any);
});

});

context("checkImportKey", () => {

it("throw error if extractable is true", () => {
assert.throws(() => {
provider.checkImportKey("raw", new ArrayBuffer(0), { name: "HKDF" }, true, ["deriveBits"]);
}, SyntaxError);
});

it("correct extractable value", () => {
provider.checkImportKey("raw", new ArrayBuffer(0), { name: "HKDF" }, false, ["deriveBits"]);
});

});

});
2 changes: 1 addition & 1 deletion test/hmac.ts
@@ -1,5 +1,5 @@
import assert from "assert";
import "reflect-metadata"
import "reflect-metadata";
import { OperationError } from "../src/errors";
import { HmacProvider } from "../src/hmac";

Expand Down

0 comments on commit e963ddf

Please sign in to comment.