Skip to content

Commit

Permalink
Merge branch 'v0.18.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun Kurihara committed Dec 8, 2019
2 parents 9bced78 3ee664f commit 267b0f6
Show file tree
Hide file tree
Showing 25 changed files with 1,131 additions and 1,163 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@
"name": "jscu",
"private": true,
"license": "MIT",
"version": "0.18.1",
"version": "0.18.2",
"root_package": "packages/js-crypto-utils",
"workspaces": [
"packages/*"
Expand All @@ -24,36 24,36 @@
"html": "lerna run html"
},
"devDependencies": {
"@types/chai": "4.2.5",
"@types/chai": "4.2.6",
"@types/mocha": "5.2.7",
"@types/node": "10.17.5",
"@typescript-eslint/eslint-plugin": "2.8.0",
"@typescript-eslint/parser": "2.8.0",
"@types/node": "12.12.14",
"@typescript-eslint/eslint-plugin": "2.10.0",
"@typescript-eslint/parser": "2.10.0",
"can-npm-publish": "1.3.2",
"chai": "4.2.0",
"cross-env": "6.0.3",
"esdoc": "1.1.0",
"esdoc-standard-plugin": "1.0.0",
"eslint": "6.6.0",
"eslint": "6.7.2",
"istanbul-instrumenter-loader": "3.0.1",
"jsdom": "15.2.1",
"karma": "4.4.1",
"karma-chrome-launcher": "3.1.0",
"karma-cli": "2.0.0",
"karma-coverage": "2.0.1",
"karma-coverage-istanbul-reporter": "2.1.0",
"karma-coverage-istanbul-reporter": "2.1.1",
"karma-mocha": "1.3.0",
"karma-mocha-reporter": "2.2.5",
"karma-sourcemap-loader": "0.3.7",
"karma-webpack": "4.0.2",
"lerna": "3.18.4",
"lerna": "3.19.0",
"mocha": "6.2.2",
"mocha-sinon": "2.1.0",
"nyc": "14.1.1",
"ts-loader": "6.2.1",
"ts-node": "8.5.2",
"ts-node": "8.5.4",
"tsc": "1.20150623.0",
"typescript": "3.7.2",
"typescript": "3.7.3",
"webpack": "4.41.2",
"webpack-cli": "3.3.10",
"webpack-common-shake": "2.1.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/js-crypto-aes/package.json
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
{
"name": "js-crypto-aes",
"version": "0.7.1",
"version": "0.7.2",
"description": "Universal Module for AES Encryption and Decryption in JavaScript",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -37,10 37,10 @@
"webcrypto"
],
"devDependencies": {
"js-crypto-random": "^0.4.1",
"js-crypto-random": "^0.4.2",
"js-encoding-utils": "0.5.6"
},
"dependencies": {
"js-crypto-env": "^0.3.0"
"js-crypto-env": "^0.3.1"
}
}
48 changes: 24 additions & 24 deletions packages/js-crypto-aes/src/aes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 52,14 @@ export const encrypt = async (
assertAlgorithms({name, iv, tagLength});
if(params.ciphers[name].tagLength && !tagLength) tagLength = params.ciphers[name].tagLength;

const webCrypto = await util.getWebCryptoAll(); // web crypto api
const nodeCrypto = await util.getNodeCrypto(); // node crypto
const env = util.getCrypto();

if (typeof webCrypto !== 'undefined' && typeof webCrypto.importKey === 'function' && typeof webCrypto.encrypt === 'function') {// for web API including IE...
return webapi.encrypt(msg, key, {name, iv, additionalData, tagLength}, webCrypto);
if (env.name === 'webCrypto' || env.name === 'msCrypto') {// for web API including IE...
if (typeof env.crypto.importKey !== 'function' || typeof env.crypto.encrypt !== 'function') throw new Error('UnsupportedWebCrypto');
return webapi.encrypt(msg, key, {name, iv, additionalData, tagLength}, env.crypto);
}
else if (typeof nodeCrypto !== 'undefined' ) { // for node
return nodeapi.encrypt(msg, key, {name, iv, additionalData, tagLength}, nodeCrypto);
else if (env.name === 'nodeCrypto') { // for node
return nodeapi.encrypt(msg, key, {name, iv, additionalData, tagLength}, env.crypto);

} else throw new Error('UnsupportedEnvironment'); // TODO:fallback to native implementation
};
Expand All @@ -85,14 85,14 @@ export const decrypt = async (
assertAlgorithms({name, iv, tagLength});
if(params.ciphers[name].tagLength && !tagLength) tagLength = params.ciphers[name].tagLength;

const webCrypto = await util.getWebCryptoAll(); // web crypto api
const nodeCrypto = await util.getNodeCrypto(); // node crypto
const env = util.getCrypto();

if (typeof webCrypto !== 'undefined' && typeof webCrypto.importKey === 'function' && typeof webCrypto.encrypt === 'function') {
return webapi.decrypt(data, key, {name, iv, additionalData, tagLength}, webCrypto);
if (env.name === 'webCrypto' || env.name === 'msCrypto') {// for web API including IE...
if (typeof env.crypto.importKey !== 'function' || typeof env.crypto.decrypt !== 'function') throw new Error('UnsupportedWebCrypto');
return webapi.decrypt(data, key, {name, iv, additionalData, tagLength}, env.crypto);
}
else if (typeof nodeCrypto !== 'undefined'){
return nodeapi.decrypt(data, key, {name, iv, additionalData, tagLength}, nodeCrypto);
else if (env.name === 'nodeCrypto') { // for node
return nodeapi.decrypt(data, key, {name, iv, additionalData, tagLength}, env.crypto);
} else throw new Error('UnsupportedEnvironment');
};

Expand All @@ -112,16 112,16 @@ export const wrapKey = async (
// assertion
if(keyToBeWrapped.length % 8 > 0) throw new Error('WrappedKeyMustBeMultipleOf8');

const webCrypto = await util.getWebCryptoAll(); // web crypto api
const nodeCrypto = await util.getNodeCrypto(); // node crypto
const env = util.getCrypto();

const iv = <Uint8Array>(params.wrapKeys['AES-KW'].defaultIv);

if (typeof webCrypto !== 'undefined' && typeof webCrypto.importKey === 'function' && typeof webCrypto.wrapKey === 'function') {// for web API including IE...
return webapi.wrapKey(keyToBeWrapped, wrappingKey, {name, iv}, webCrypto);
if (env.name === 'webCrypto' || env.name === 'msCrypto') {// for web API including IE...
if (typeof env.crypto.importKey !== 'function' || typeof env.crypto.wrapKey !== 'function') throw new Error('UnsupportedWebCrypto');
return webapi.wrapKey(keyToBeWrapped, wrappingKey, {name, iv}, env.crypto);
}
else if (typeof nodeCrypto !== 'undefined' ) { // for node
return nodeapi.wrapKey(keyToBeWrapped, wrappingKey, {name, iv}, nodeCrypto);
else if (env.name === 'nodeCrypto') { // for node
return nodeapi.wrapKey(keyToBeWrapped, wrappingKey, {name, iv}, env.crypto);
} else {
throw new Error('UnsupportedEnvironment'); // TODO:fallback to native implementation
}
Expand All @@ -139,16 139,16 @@ export const unwrapKey = async (
wrappingKey: Uint8Array,
{name}: {name: 'AES-KW'}
) => {
const webCrypto = await util.getWebCryptoAll(); // web crypto api
const nodeCrypto = await util.getNodeCrypto(); // node crypto
const env = util.getCrypto();

const iv = <Uint8Array>(params.wrapKeys['AES-KW'].defaultIv);

if (typeof webCrypto !== 'undefined' && typeof webCrypto.importKey === 'function' && typeof webCrypto.wrapKey === 'function') {// for web API including IE...
return webapi.unwrapKey(wrappedKey, wrappingKey, {name, iv}, webCrypto);
if (env.name === 'webCrypto' || env.name === 'msCrypto') {// for web API including IE...
if (typeof env.crypto.importKey !== 'function' || typeof env.crypto.unwrapKey !== 'function') throw new Error('UnsupportedWebCrypto');
return webapi.unwrapKey(wrappedKey, wrappingKey, {name, iv}, env.crypto);
}
else if (typeof nodeCrypto !== 'undefined' ) { // for node
return nodeapi.unwrapKey(wrappedKey, wrappingKey, {name, iv}, nodeCrypto);
else if (env.name === 'nodeCrypto') { // for node
return nodeapi.unwrapKey(wrappedKey, wrappingKey, {name, iv}, env.crypto);
} else {
throw new Error('UnsupportedEnvironment'); // TODO:fallback to native implementation
}
Expand Down
10 changes: 5 additions & 5 deletions packages/js-crypto-ec/package.json
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
{
"name": "js-crypto-ec",
"version": "0.6.1",
"version": "0.6.2",
"description": "Universal Module for Elliptic Curve Cryptography (ECDSA and ECDH) in JavaScript",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -42,10 42,10 @@
"asn1.js": "~5.2.0",
"buffer": "~5.4.0",
"elliptic": "~6.5.0",
"js-crypto-env": "^0.3.0",
"js-crypto-hash": "^0.6.1",
"js-crypto-key-utils": "^0.7.1",
"js-crypto-random": "^0.4.1",
"js-crypto-env": "^0.3.1",
"js-crypto-hash": "^0.6.2",
"js-crypto-key-utils": "^0.7.2",
"js-crypto-random": "^0.4.2",
"js-encoding-utils": "0.5.6"
},
"devDependencies": {
Expand Down
45 changes: 20 additions & 25 deletions packages/js-crypto-ec/src/ec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 15,15 @@ import {JsonWebKeyPair, CurveTypes, HashTypes, SignatureFormat} from './typedef'
* @throws {Error} - Throws if UnsupportedEnvironment, i.e., neither WebCrypto, NodeCrypto, nor PureJS codes works.
*/
export const generateKey = async (namedCurve: CurveTypes ='P-256'): Promise<JsonWebKeyPair> => {
const webCrypto = util.getWebCrypto(); // web crypto api
const nodeCrypto = util.getNodeCrypto(); // implementation on node.js
const env = util.getCrypto();

let pure: boolean = false;
let keyPair: JsonWebKeyPair;
try {
if (typeof webCrypto !== 'undefined' && typeof webCrypto.generateKey === 'function' && typeof webCrypto.exportKey === 'function') { // for web API
keyPair = await webapi.generateKey(namedCurve, webCrypto);
} else if (typeof nodeCrypto !== 'undefined') { // for node
keyPair = await nodeapi.generateKey(namedCurve, nodeCrypto);
if (env.name === 'webCrypto' && typeof env.crypto.generateKey === 'function' && typeof env.crypto.exportKey === 'function') { // for web API
keyPair = await webapi.generateKey(namedCurve, env.crypto);
} else if (env.name === 'nodeCrypto') { // for node
keyPair = await nodeapi.generateKey(namedCurve, env.crypto);
} else {
pure = true;
keyPair = await purejs.generateKey(namedCurve);
Expand Down Expand Up @@ -59,16 58,15 @@ export const sign = async (
hash: HashTypes = 'SHA-256',
signatureFormat: SignatureFormat ='raw'
): Promise<Uint8Array> => {
const webCrypto = util.getWebCrypto(); // web crypto api
const nodeCrypto = util.getNodeCrypto(); // implementation on node.js
const env = util.getCrypto();

let pure: boolean = false;
let signature: Uint8Array;
try {
if (typeof webCrypto !== 'undefined' && typeof webCrypto.importKey === 'function' && typeof webCrypto.sign === 'function') { // for web API
signature = await webapi.sign(msg, privateJwk, hash, signatureFormat, webCrypto);
} else if (typeof nodeCrypto !== 'undefined') { // for node
signature = await nodeapi.sign(msg, privateJwk, hash, signatureFormat, nodeCrypto);
if (env.name === 'webCrypto' && typeof env.crypto.importKey === 'function' && typeof env.crypto.sign === 'function') { // for web API
signature = await webapi.sign(msg, privateJwk, hash, signatureFormat, env.crypto);
} else if (env.name === 'nodeCrypto') { // for node
signature = await nodeapi.sign(msg, privateJwk, hash, signatureFormat, env.crypto);
} else {
pure = true;
signature = await purejs.sign(msg, privateJwk, hash, signatureFormat);
Expand Down Expand Up @@ -105,16 103,15 @@ export const verify = async (
hash: HashTypes = 'SHA-256',
signatureFormat: SignatureFormat='raw'
): Promise<boolean> => {
const webCrypto = util.getWebCrypto(); // web crypto api
const nodeCrypto = util.getNodeCrypto(); // implementation on node.js
const env = util.getCrypto();

let pure: boolean = false;
let valid: boolean;
try {
if (typeof webCrypto !== 'undefined' && typeof webCrypto.importKey === 'function' && typeof webCrypto.sign === 'function') { // for web API
valid = await webapi.verify(msg, signature, publicJwk, hash, signatureFormat, webCrypto);
} else if (typeof nodeCrypto !== 'undefined') { // for node
valid = await nodeapi.verify(msg, signature, publicJwk, hash, signatureFormat, nodeCrypto);
if (env.name === 'webCrypto' && typeof env.crypto.importKey === 'function' && typeof env.crypto.sign === 'function') { // for web API
valid = await webapi.verify(msg, signature, publicJwk, hash, signatureFormat, env.crypto);
} else if (env.name === 'nodeCrypto') { // for node
valid = await nodeapi.verify(msg, signature, publicJwk, hash, signatureFormat, env.crypto);
} else {
pure = true;
valid = await purejs.verify(msg, signature, publicJwk, hash, signatureFormat);
Expand Down Expand Up @@ -145,17 142,15 @@ export const verify = async (
export const deriveSecret = async (publicJwk: JsonWebKey, privateJwk: JsonWebKey): Promise<Uint8Array> => {
// assertion
if(publicJwk.crv !== privateJwk.crv) throw new Error('UnmatchedCurveName');

const webCrypto = util.getWebCrypto(); // web crypto api
const nodeCrypto = util.getNodeCrypto(); // implementation on node.js
const env = util.getCrypto();

let pure: boolean = false;
let secret: Uint8Array;
try {
if (typeof webCrypto !== 'undefined' && typeof webCrypto.importKey === 'function' && typeof webCrypto.sign === 'function') { // for web API
secret = await webapi.deriveSecret(publicJwk, privateJwk, webCrypto);
} else if (typeof nodeCrypto !== 'undefined') { // for node
secret = nodeapi.deriveSecret(publicJwk, privateJwk, nodeCrypto);
if (env.name === 'webCrypto' && typeof env.crypto.importKey === 'function' && typeof env.crypto.deriveBits === 'function') { // for web API
secret = await webapi.deriveSecret(publicJwk, privateJwk, env.crypto);
} else if (env.name === 'nodeCrypto') { // for node
secret = nodeapi.deriveSecret(publicJwk, privateJwk, env.crypto);
} else {
pure = true;
secret = await purejs.deriveSecret(publicJwk, privateJwk);
Expand Down
12 changes: 6 additions & 6 deletions packages/js-crypto-env/package.json
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
{
"name": "js-crypto-env",
"version": "0.3.0",
"version": "0.3.1",
"description": "Shared environment modules for jscu",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -28,14 28,14 @@
"jscu"
],
"devDependencies": {
"@types/chai": "4.2.5",
"@types/chai": "4.2.6",
"@types/mocha": "5.2.7",
"@types/node": "10.17.5",
"@types/node": "10.17.6",
"istanbul-instrumenter-loader": "3.0.1",
"karma-coverage-istanbul-reporter": "2.1.0",
"karma-coverage-istanbul-reporter": "2.1.1",
"ts-loader": "6.2.1",
"ts-node": "8.5.2",
"ts-node": "8.5.4",
"tsc": "1.20150623.0",
"typescript": "3.7.2"
"typescript": "3.7.3"
}
}
19 changes: 17 additions & 2 deletions packages/js-crypto-env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 55,20 @@ const getMsCrypto = () : undefined|any => {
return undefined;
};

export default {getNodeCrypto, getWebCrypto, getMsCrypto, getWebCryptoAll, getRootWebCryptoAll};
export {getNodeCrypto, getWebCrypto, getMsCrypto, getWebCryptoAll, getRootWebCryptoAll};
/**
* Get native crypto lib name.
* @return {name: 'msCrypto'|'webCrypto'|'nodeCrypto'|undefined, crypto?: any}
*/
const getCrypto = (): {name: 'msCrypto'|'webCrypto'|'nodeCrypto'|undefined, crypto?: any} => {
const webCrypto = getWebCrypto();
const nodeCrypto = getNodeCrypto();
const msCrypto = getMsCrypto();

if (typeof nodeCrypto !== 'undefined') return {name: 'nodeCrypto', crypto: nodeCrypto};
else if(typeof webCrypto !== 'undefined' && typeof msCrypto === 'undefined') return {name: 'webCrypto', crypto: webCrypto};
else if (typeof msCrypto !== 'undefined') return {name: 'msCrypto', crypto: msCrypto};
else return {name: undefined};
};

export default {getNodeCrypto, getWebCrypto, getMsCrypto, getWebCryptoAll, getRootWebCryptoAll, getCrypto};
export {getNodeCrypto, getWebCrypto, getMsCrypto, getWebCryptoAll, getRootWebCryptoAll, getCrypto};
2 changes: 2 additions & 0 deletions packages/js-crypto-env/test/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 13,13 @@ describe('Common env test', () => {
console.log(typeof env.getMsCrypto());
console.log(typeof env.getWebCryptoAll());
console.log(typeof env.getRootWebCryptoAll());
console.log(typeof env.getCrypto());
expect(typeof env.getNodeCrypto() === 'undefined' || typeof env.getNodeCrypto() === 'object').to.be.true;
expect(typeof env.getWebCrypto() === 'undefined' || typeof env.getWebCrypto() === 'object').to.be.true;
expect(typeof env.getMsCrypto() === 'undefined' || typeof env.getMsCrypto() === 'object').to.be.true;
expect(typeof env.getWebCryptoAll() === 'undefined' || typeof env.getWebCryptoAll() === 'object').to.be.true;
expect(typeof env.getRootWebCryptoAll() === 'undefined' || typeof env.getRootWebCryptoAll() === 'object').to.be.true;
expect(typeof env.getCrypto() === 'object').to.be.true;
});

});
Expand Down
6 changes: 3 additions & 3 deletions packages/js-crypto-hash/package.json
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
{
"name": "js-crypto-hash",
"version": "0.6.1",
"version": "0.6.2",
"description": "Universal Module for Hash Function in JavaScript",
"main": "dist/index.js",
"scripts": {
Expand Down Expand Up @@ -39,9 39,9 @@
"dependencies": {
"buffer": "~5.4.3",
"hash.js": "~1.1.7",
"js-crypto-env": "^0.3.0",
"js-crypto-env": "^0.3.1",
"md5": "~2.2.1",
"sha3": "~2.0.4"
"sha3": "~2.1.0"
},
"devDependencies": {
"js-encoding-utils": "0.5.6"
Expand Down
Loading

0 comments on commit 267b0f6

Please sign in to comment.