Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Apply updated core structure
Browse files Browse the repository at this point in the history
- add index.d.ts
- add tests for types
- update npm scripts
- fix type errors
- fix text errors
  • Loading branch information
microshine committed Nov 13, 2016
1 parent 16334e8 commit 217512c
Show file tree
Hide file tree
Showing 14 changed files with 276 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@
/node_modules
/.nyc_output/
/coverage/
libeay32.dll
1 change: 1 addition & 0 deletions .npmignore
Expand Up @@ -2,3 +2,4 @@
/node_modules
/build
.gitignore
libeay32.dll
183 changes: 183 additions & 0 deletions index.d.ts
@@ -0,0 +1,183 @@
/// <reference types="node" />
/// <reference types="webcrypto-core" />

declare type NodeBufferSource = BufferSource | Buffer;

declare namespace NodeWebcryptoOpenSSL {

enum EcNamedCurves {
secp112r1 = 704,
secp112r2 = 705,
secp128r1 = 706,
secp128r2 = 707,
secp160k1 = 708,
secp160r1 = 709,
secp160r2 = 710,
secp192r1 = 409,
secp192k1 = 711,
secp224k1 = 712,
secp224r1 = 713,
secp256k1 = 714,
secp256r1 = 415,
secp384r1 = 715,
secp521r1 = 716,
sect113r1 = 717,
sect113r2 = 718,
sect131r1 = 719,
sect131r2 = 720,
sect163k1 = 721,
sect163r1 = 722,
sect163r2 = 723,
sect193r1 = 724,
sect193r2 = 725,
sect233k1 = 726,
sect233r1 = 727,
sect239k1 = 728,
sect283k1 = 729,
sect283r1 = 730,
sect409k1 = 731,
sect409r1 = 732,
sect571k1 = 733,
sect571r1 = 734,
}

enum RsaPublicExponent {
RSA_3 = 0,
RSA_F4 = 1,
}

enum KeyType {
PUBLIC = 0,
PRIVATE = 1,
}

class Key {
type: number;
modulusLength(): number;
publicExponent(): Buffer;
exportJwk(keyType: KeyType, callback: (err: Error, jwk: any) => void): void;
exportJwk(keyType: KeyType): any;
exportSpki(callback: (err: Error, raw: Buffer) => void): void;
exportPkcs8(callback: (err: Error, raw: Buffer) => void): void;
sign(digestName: string, message: Buffer, callback: (err: Error, signature: Buffer) => void): void;
verify(digestName: string, message: Buffer, signature: Buffer, callback: (err: Error, valid: boolean) => void): void;
RsaOaepEncDec(digestName: string, data: Buffer, label: Buffer | null, decrypt: boolean, callback: (err: Error, raw: Buffer) => void): void;
RsaPssSign(digestName: string, saltLength: number, data: Buffer, cb: (err: Error, signature: Buffer) => void): void;
RsaPssVerify(digestName: string, saltLength: number, data: Buffer, signature: Buffer, cb: (err: Error, verified: boolean) => void): void;
EcdhDeriveKey(pubkey: Key, derivedLen: number, callback: (err: Error, raw: Buffer) => void): void;
EcdhDeriveBits(pubkey: Key, lengthBits: number, callback: (err: Error, raw: Buffer) => void): void;
static generateRsa(modulus: number, publicExponent: RsaPublicExponent, callback: (err: Error, key: Key) => void): void;
static generateEc(namedCurve: EcNamedCurves, callback: (err: Error, key: Key) => void): void;
static importJwk(jwk: Object, keyType: KeyType, callback: (err: Error, key: Key) => void): void;
static importJwk(jwk: {
[key: string]: Buffer;
}, keyType: KeyType): any;
static importSpki(raw: Buffer, callback: (err: Error, key: Key) => void): void;
static importPkcs8(raw: Buffer, callback: (err: Error, key: Key) => void): void;
}

class AesKey {
static generate(keySize: number, callback: (err: Error, key: AesKey) => void): void;
encrypt(cipher: string, iv: Buffer, input: Buffer, callback: (err: Error, data: Buffer) => void): void;
encryptGcm(iv: Buffer, input: Buffer, aad: Buffer | undefined, tag: number, callback: (err: Error, data: Buffer) => void): void;
decrypt(cipher: string, iv: Buffer, input: Buffer, callback: (err: Error, data: Buffer) => void): void;
decryptGcm(iv: Buffer, input: Buffer, aad: Buffer | undefined, tag: number, callback: (err: Error, data: Buffer) => void): void;
export(callback: (err: Error, raw: Buffer) => void): void;
static import(raw: Buffer, callback: (err: Error, key: AesKey) => void): void;
}

class Core {
static digest(digestName: string, messgae: Buffer, callback: (err: Error, digest: Buffer) => void): void;
}

interface CryptoKeyPair extends NativeCryptoKeyPair {
privateKey: CryptoKey;
publicKey: CryptoKey;
}

class CryptoKey implements NativeCryptoKey {
type: string;
extractable: boolean;
algorithm: Algorithm;
usages: string[];
private native_: AesKey | Key;
readonly native: AesKey | Key;
constructor(key: AesKey | Key, alg: Algorithm, type: string, extractable: boolean, keyUsages: string[]);
}

interface IKeyStorageItem extends NativeCryptoKey {
name: string;
keyJwk: any;
file?: string;
}

class KeyStorage {
protected directory: string;
protected keys: {
[key: string]: IKeyStorageItem;
};
constructor(directory: string);
protected createDirectory(directory: string, flags?: any): void;
protected readFile(file: string): IKeyStorageItem | null;
protected readDirectory(): void;
protected saveFile(key: IKeyStorageItem): void;
protected removeFile(key: IKeyStorageItem): void;
readonly length: number;
clear(): void;
protected getItemById(id: string): IKeyStorageItem;
getItem(key: string): CryptoKey | null;
key(index: number): string;
removeItem(key: string): void;
setItem(key: string, data: CryptoKey): void;
}

interface WebCryptoOptions {
directory?: string;
}

class SubtleCrypto extends WebcryptoCore.SubtleCrypto {
digest(algorithm: AlgorithmIdentifier, data: NodeBufferSource): PromiseLike<ArrayBuffer>;
generateKey(algorithm: string, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKeyPair | CryptoKey>;
generateKey(algorithm: RsaHashedKeyGenParams | EcKeyGenParams | DhKeyGenParams, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKeyPair>;
generateKey(algorithm: AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKey>;
sign(algorithm: string | RsaPssParams | EcdsaParams | AesCmacParams, key: CryptoKey, data: NodeBufferSource): PromiseLike<ArrayBuffer>;
verify(algorithm: string | RsaPssParams | EcdsaParams | AesCmacParams, key: CryptoKey, signature: NodeBufferSource, data: NodeBufferSource): PromiseLike<boolean>;
encrypt(algorithm: string | RsaOaepParams | AesCtrParams | AesCbcParams | AesCmacParams | AesGcmParams | AesCfbParams, key: CryptoKey, data: NodeBufferSource): PromiseLike<ArrayBuffer>;
decrypt(algorithm: string | RsaOaepParams | AesCtrParams | AesCbcParams | AesCmacParams | AesGcmParams | AesCfbParams, key: CryptoKey, data: NodeBufferSource): PromiseLike<ArrayBuffer>;
wrapKey(format: string, key: CryptoKey, wrappingKey: CryptoKey, wrapAlgorithm: AlgorithmIdentifier): PromiseLike<ArrayBuffer>;
unwrapKey(format: string, wrappedKey: NodeBufferSource, unwrappingKey: CryptoKey, unwrapAlgorithm: AlgorithmIdentifier, unwrappedKeyAlgorithm: AlgorithmIdentifier, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKey>;
deriveKey(algorithm: string | EcdhKeyDeriveParams | DhKeyDeriveParams | ConcatParams | HkdfCtrParams | Pbkdf2Params, baseKey: CryptoKey, derivedKeyType: string | AesDerivedKeyParams | HmacImportParams | ConcatParams | HkdfCtrParams | Pbkdf2Params, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKey>;
deriveBits(algorithm: string | EcdhKeyDeriveParams | DhKeyDeriveParams | ConcatParams | HkdfCtrParams | Pbkdf2Params, baseKey: CryptoKey, length: number): PromiseLike<ArrayBuffer>;
exportKey(format: "jwk", key: CryptoKey): PromiseLike<JsonWebKey>;
exportKey(format: "raw" | "pkcs8" | "spki", key: CryptoKey): PromiseLike<ArrayBuffer>;
exportKey(format: string, key: CryptoKey): PromiseLike<JsonWebKey | ArrayBuffer>;
importKey(format: "jwk", keyData: JsonWebKey, algorithm: string | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | DhImportKeyParams, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKey>;
importKey(format: "raw" | "pkcs8" | "spki", keyData: NodeBufferSource, algorithm: string | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | DhImportKeyParams, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKey>;
importKey(format: string, keyData: JsonWebKey | NodeBufferSource, algorithm: string | RsaHashedImportParams | EcKeyImportParams | HmacImportParams | DhImportKeyParams, extractable: boolean, keyUsages: string[]): PromiseLike<CryptoKey>;
}

/**
* OpenSSL with WebCrypto Interface
*/
class WebCrypto implements NativeCrypto {

keyStorage: KeyStorage;
subtle: SubtleCrypto;

/**
* Generates cryptographically random values
* @param array Initialize array
*/
getRandomValues(array: NodeBufferSource): NodeBufferSource;
getRandomValues(array: ArrayBufferView): ArrayBufferView;

/**
* Constructor
*/
constructor(options?: WebCryptoOptions);
}
}

declare module "node-webcrypto-ossl" {
export = NodeWebcryptoOpenSSL.WebCrypto;
}
2 changes: 1 addition & 1 deletion lib/crypto/aes.ts
Expand Up @@ -82,7 +82,7 @@ export class AesCrypto extends BaseCrypto {
resolve(data.buffer);
});
break;
default: throw new WebCryptoError(`ExportKey: Unknown export frmat '${format}'`);
default: throw new WebCryptoError(`ExportKey: Unknown export format '${format}'`);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/crypto/ec.ts
Expand Up @@ -158,7 +158,7 @@ export class EcCrypto extends BaseCrypto {
});
break;
default:
throw new WebCryptoError(`ExportKey: Unknown export frmat '${format}'`);
throw new WebCryptoError(`ExportKey: Unknown export format '${format}'`);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/crypto/rsa.ts
Expand Up @@ -154,7 +154,7 @@ export abstract class RsaCrypto extends BaseCrypto {
});
break;
default:
throw new WebCryptoError(`ExportKey: Unknown export frmat '${format}'`);
throw new WebCryptoError(`ExportKey: Unknown export format '${format}'`);
}
});
}
Expand Down
9 changes: 7 additions & 2 deletions lib/key.ts
@@ -1,13 +1,18 @@
import * as native from "./native";

export interface CryptoKeyPair extends NativeCryptoKeyPair {
privateKey: CryptoKey;
publicKey: CryptoKey;
}

export class CryptoKey implements NativeCryptoKey {
type: string;
extractable: boolean;
algorithm: Algorithm;
usages: string[] = [];

private native_: any;
get native(): any {
private native_: native.AesKey | native.Key;
get native(): native.AesKey | native.Key {
return this.native_;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/key_storage.ts
Expand Up @@ -2,7 +2,7 @@
import * as core from "webcrypto-core";

import * as native from "./native";
import { CryptoKey as _CryptoKey } from "./key";
import { CryptoKey } from "./key";
import * as fs from "fs";
import * as path from "path";
import * as mkdirp from "mkdirp";
Expand All @@ -11,7 +11,7 @@ const JSON_FILE_EXT = ".json";

class KeyStorageError extends core.WebCryptoError { }

export interface IKeyStorageItem extends CryptoKey {
export interface IKeyStorageItem extends NativeCryptoKey {
name: string;
keyJwk: any;
file?: string;
Expand Down Expand Up @@ -212,7 +212,7 @@ export class KeyStorage {
default:
throw new Error(`Unknown type '${item.type}'`);
}
res = new _CryptoKey(nativeKey, item.algorithm as any, item.type, item.extractable, item.usages);
res = new CryptoKey(nativeKey, item.algorithm as any, item.type, item.extractable, item.usages);
return res;
}

Expand All @@ -234,7 +234,7 @@ export class KeyStorage {
}

setItem(key: string, data: CryptoKey): void {
let nativeKey = (data as _CryptoKey).native;
let nativeKey = (data as CryptoKey).native;
let jwk: any = null;
switch (data.type.toLowerCase()) {
case "public":
Expand Down
2 changes: 1 addition & 1 deletion lib/subtle.ts
Expand Up @@ -7,7 +7,7 @@ const AlgorithmNames = webcrypto.AlgorithmNames;

// Local
import * as native from "./native";
import { CryptoKey } from "./key";
import { CryptoKey, CryptoKeyPair } from "./key";
// import * as alg from "./alg";
import * as rsa from "./crypto/rsa";
import * as aes from "./crypto/aes";
Expand Down
4 changes: 0 additions & 4 deletions lib/typings/index.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions lib/webcrypto.ts
Expand Up @@ -15,7 +15,7 @@ _global.atob = (data: string) => new Buffer(data, "base64").toString("binary");
const ERR_RANDOM_VALUE_LENGTH = "Failed to execute 'getRandomValues' on 'Crypto': The ArrayBufferView's byte length (%1) exceeds the number of bytes of entropy available via this API (65536).";

export interface WebCryptoOptions {
directory: string;
directory?: string;
}

/**
Expand All @@ -25,7 +25,7 @@ class WebCrypto implements NativeCrypto {

keyStorage: KeyStorage;

subtle: NativeSubtleCrypto;
subtle: SubtleCrypto;

/**
* Generates cryptographically random values
Expand Down
9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -7,13 +7,16 @@
},
"description": "A WebCrypto Polyfill for Node in TypeScript built on OpenSSL",
"main": "buildjs/webcrypto.js",
"types": "index.d.ts",
"scripts": {
"clean": "rm -rf build/ buildjs/ coverage/ .nyc_output/ npm-debug.log npm-debug.log.*",
"prepublish": "npm run build",
"postinstall": "npm run build",
"pretest": "npm run build",
"pretest": "tsc --sourceMap",
"test": "mocha",
"build": "tsc --sourceMap",
"build": "npm run build:es5",
"build:es5": "tsc --module commonjs --target es5",
"build:es2015": "tsc --module es2015 --target es2015",
"pub": "npm version patch && npm publish && git push",
"sync": "git ac && git pull --rebase && git push",
"coverage": "nyc npm test",
Expand All @@ -40,7 +43,7 @@
"dependencies": {
"mkdirp": "^0.5.1",
"nan": "^2.4.0",
"webcrypto-core": "latest"
"webcrypto-core": "git+https://github.com/PeculiarVentures/webcrypto-core#types"
},
"devDependencies": {
"@types/mkdirp": "^0.3.29",
Expand Down

0 comments on commit 217512c

Please sign in to comment.