webcrypto-core
Advanced tools
Comparing version 1.0.14 to 1.0.15
@@ -9,2 +9,3 @@ /** | ||
var tslib = require('tslib'); | ||
var pvtsutils = require('pvtsutils'); | ||
@@ -33,3 +34,10 @@ | ||
/** | ||
* PEM converter | ||
*/ | ||
class PemConverter { | ||
/** | ||
* Converts PEM to Array buffer | ||
* @param pem PEM string | ||
*/ | ||
static toArrayBuffer(pem) { | ||
@@ -42,2 +50,6 @@ const base64 = pem | ||
} | ||
/** | ||
* Converts PEM to Uint8Array | ||
* @param pem PEM string | ||
*/ | ||
static toUint8Array(pem) { | ||
@@ -47,2 +59,7 @@ const bytes = this.toArrayBuffer(pem); | ||
} | ||
/** | ||
* Converts buffer source to PEM | ||
* @param buffer Buffer source | ||
* @param tag PEM tag name | ||
*/ | ||
static fromBufferSource(buffer, tag) { | ||
@@ -68,5 +85,14 @@ const base64 = pvtsutils.Convert.ToBase64(buffer); | ||
} | ||
/** | ||
* Returns `true` if incoming data is PEM string, otherwise `false` | ||
* @param data Data | ||
*/ | ||
static isPEM(data) { | ||
// tslint:disable-next-line:max-line-length | ||
return /-----BEGIN .+-----[A-Za-z0-9+\/\+\=\s\n]+-----END .+-----/i.test(data); | ||
} | ||
/** | ||
* Returns tag name from PEM string | ||
* @param pem PEM string | ||
*/ | ||
static getTagName(pem) { | ||
@@ -82,2 +108,7 @@ if (!this.isPEM(pem)) { | ||
} | ||
/** | ||
* Returns `true` if tag name from PEM matches to tagName parameter | ||
* @param pem PEM string | ||
* @param tagName Tag name for comparison | ||
*/ | ||
static hasTagName(pem, tagName) { | ||
@@ -114,3 +145,3 @@ const tag = this.getTagName(pem); | ||
if (ArrayBuffer.isView(data)) { | ||
return Buffer.from(data.buffer, data.byteOffset, data.byteLength); | ||
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength); | ||
} | ||
@@ -132,5 +163,8 @@ if (data instanceof ArrayBuffer) { | ||
class ProviderCrypto { | ||
async digest(algorithm, data) { | ||
this.checkDigest.apply(this, arguments); | ||
return this.onDigest.apply(this, arguments); | ||
//#region Digest | ||
digest(algorithm, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkDigest.apply(this, arguments); | ||
return this.onDigest.apply(this, arguments); | ||
}); | ||
} | ||
@@ -140,8 +174,14 @@ checkDigest(algorithm, data) { | ||
} | ||
async onDigest(algorithm, data) { | ||
throw new UnsupportedOperationError("digest"); | ||
onDigest(algorithm, data) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("digest"); | ||
}); | ||
} | ||
async generateKey(algorithm, extractable, keyUsages) { | ||
this.checkGenerateKey.apply(this, arguments); | ||
return this.onGenerateKey.apply(this, arguments); | ||
//#endregion | ||
//#region Generate key | ||
generateKey(algorithm, extractable, keyUsages) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkGenerateKey.apply(this, arguments); | ||
return this.onGenerateKey.apply(this, arguments); | ||
}); | ||
} | ||
@@ -164,9 +204,16 @@ checkGenerateKey(algorithm, extractable, keyUsages) { | ||
checkGenerateKeyParams(algorithm) { | ||
// nothing | ||
} | ||
async onGenerateKey(algorithm, extractable, keyUsages) { | ||
throw new UnsupportedOperationError("generateKey"); | ||
onGenerateKey(algorithm, extractable, keyUsages) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("generateKey"); | ||
}); | ||
} | ||
async sign(algorithm, key, data) { | ||
this.checkSign.apply(this, arguments); | ||
return this.onSign.apply(this, arguments); | ||
//#endregion | ||
//#region Sign | ||
sign(algorithm, key, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkSign.apply(this, arguments); | ||
return this.onSign.apply(this, arguments); | ||
}); | ||
} | ||
@@ -178,8 +225,14 @@ checkSign(algorithm, key, data) { | ||
} | ||
async onSign(algorithm, key, data) { | ||
throw new UnsupportedOperationError("sign"); | ||
onSign(algorithm, key, data) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("sign"); | ||
}); | ||
} | ||
async verify(algorithm, key, signature, data) { | ||
this.checkVerify.apply(this, arguments); | ||
return this.onVerify.apply(this, arguments); | ||
//#endregion | ||
//#region Verify | ||
verify(algorithm, key, signature, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkVerify.apply(this, arguments); | ||
return this.onVerify.apply(this, arguments); | ||
}); | ||
} | ||
@@ -191,8 +244,14 @@ checkVerify(algorithm, key, signature, data) { | ||
} | ||
async onVerify(algorithm, key, signature, data) { | ||
throw new UnsupportedOperationError("verify"); | ||
onVerify(algorithm, key, signature, data) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("verify"); | ||
}); | ||
} | ||
async encrypt(algorithm, key, data, options) { | ||
this.checkEncrypt.apply(this, arguments); | ||
return this.onEncrypt.apply(this, arguments); | ||
//#endregion | ||
//#region Encrypt | ||
encrypt(algorithm, key, data, options) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkEncrypt.apply(this, arguments); | ||
return this.onEncrypt.apply(this, arguments); | ||
}); | ||
} | ||
@@ -204,8 +263,14 @@ checkEncrypt(algorithm, key, data, options = {}) { | ||
} | ||
async onEncrypt(algorithm, key, data) { | ||
throw new UnsupportedOperationError("encrypt"); | ||
onEncrypt(algorithm, key, data) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("encrypt"); | ||
}); | ||
} | ||
async decrypt(algorithm, key, data, options) { | ||
this.checkDecrypt.apply(this, arguments); | ||
return this.onDecrypt.apply(this, arguments); | ||
//#endregion | ||
//#region | ||
decrypt(algorithm, key, data, options) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkDecrypt.apply(this, arguments); | ||
return this.onDecrypt.apply(this, arguments); | ||
}); | ||
} | ||
@@ -217,8 +282,14 @@ checkDecrypt(algorithm, key, data, options = {}) { | ||
} | ||
async onDecrypt(algorithm, key, data) { | ||
throw new UnsupportedOperationError("decrypt"); | ||
onDecrypt(algorithm, key, data) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("decrypt"); | ||
}); | ||
} | ||
async deriveBits(algorithm, baseKey, length, options) { | ||
this.checkDeriveBits.apply(this, arguments); | ||
return this.onDeriveBits.apply(this, arguments); | ||
//#endregion | ||
//#region Derive bits | ||
deriveBits(algorithm, baseKey, length, options) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkDeriveBits.apply(this, arguments); | ||
return this.onDeriveBits.apply(this, arguments); | ||
}); | ||
} | ||
@@ -233,8 +304,14 @@ checkDeriveBits(algorithm, baseKey, length, options = {}) { | ||
} | ||
async onDeriveBits(algorithm, baseKey, length) { | ||
throw new UnsupportedOperationError("deriveBits"); | ||
onDeriveBits(algorithm, baseKey, length) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("deriveBits"); | ||
}); | ||
} | ||
async exportKey(format, key) { | ||
this.checkExportKey.apply(this, arguments); | ||
return this.onExportKey.apply(this, arguments); | ||
//#endregion | ||
//#region Export key | ||
exportKey(format, key) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkExportKey.apply(this, arguments); | ||
return this.onExportKey.apply(this, arguments); | ||
}); | ||
} | ||
@@ -248,8 +325,14 @@ checkExportKey(format, key) { | ||
} | ||
async onExportKey(format, key) { | ||
throw new UnsupportedOperationError("exportKey"); | ||
onExportKey(format, key) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("exportKey"); | ||
}); | ||
} | ||
async importKey(format, keyData, algorithm, extractable, keyUsages) { | ||
this.checkImportKey.apply(this, arguments); | ||
return this.onImportKey.apply(this, arguments); | ||
//#endregion | ||
//#region Import key | ||
importKey(format, keyData, algorithm, extractable, keyUsages) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkImportKey.apply(this, arguments); | ||
return this.onImportKey.apply(this, arguments); | ||
}); | ||
} | ||
@@ -261,9 +344,14 @@ checkImportKey(format, keyData, algorithm, extractable, keyUsages) { | ||
this.checkImportParams(algorithm); | ||
// check key usages | ||
if (Array.isArray(this.usages)) { | ||
// symmetric provider | ||
this.checkKeyUsages(keyUsages, this.usages); | ||
} | ||
} | ||
async onImportKey(format, keyData, algorithm, extractable, keyUsages) { | ||
throw new UnsupportedOperationError("importKey"); | ||
onImportKey(format, keyData, algorithm, extractable, keyUsages) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
throw new UnsupportedOperationError("importKey"); | ||
}); | ||
} | ||
//#endregion | ||
checkAlgorithmName(algorithm) { | ||
@@ -275,4 +363,6 @@ if (algorithm.name.toLowerCase() !== this.name.toLowerCase()) { | ||
checkAlgorithmParams(algorithm) { | ||
// nothing | ||
} | ||
checkDerivedKeyParams(algorithm) { | ||
// nothing | ||
} | ||
@@ -306,2 +396,3 @@ checkKeyUsages(usages, allowed) { | ||
checkImportParams(algorithm) { | ||
// nothing | ||
} | ||
@@ -339,2 +430,3 @@ checkKeyFormat(format) { | ||
checkGenerateKeyParams(algorithm) { | ||
// length | ||
this.checkRequiredProperty(algorithm, "length"); | ||
@@ -399,2 +491,3 @@ if (typeof algorithm.length !== "number") { | ||
checkAlgorithmParams(algorithm) { | ||
// counter | ||
this.checkRequiredProperty(algorithm, "counter"); | ||
@@ -407,2 +500,3 @@ if (!(algorithm.counter instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.counter))) { | ||
} | ||
// length | ||
this.checkRequiredProperty(algorithm, "length"); | ||
@@ -433,2 +527,3 @@ if (typeof algorithm.length !== "number") { | ||
checkAlgorithmParams(algorithm) { | ||
// iv | ||
this.checkRequiredProperty(algorithm, "iv"); | ||
@@ -441,2 +536,3 @@ if (!(algorithm.iv instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.iv))) { | ||
} | ||
// tagLength | ||
if (!("tagLength" in algorithm)) { | ||
@@ -485,2 +581,3 @@ algorithm.tagLength = 128; | ||
checkGenerateKeyParams(algorithm) { | ||
// length | ||
this.checkRequiredProperty(algorithm, "length"); | ||
@@ -505,4 +602,6 @@ if (typeof algorithm.length !== "number") { | ||
checkGenerateKeyParams(algorithm) { | ||
// hash | ||
this.checkRequiredProperty(algorithm, "hash"); | ||
this.checkHashAlgorithm(algorithm.hash, this.hashAlgorithms); | ||
// public exponent | ||
this.checkRequiredProperty(algorithm, "publicExponent"); | ||
@@ -516,2 +615,3 @@ if (!(algorithm.publicExponent && algorithm.publicExponent instanceof Uint8Array)) { | ||
} | ||
// modulus length | ||
this.checkRequiredProperty(algorithm, "modulusLength"); | ||
@@ -574,2 +674,3 @@ switch (algorithm.modulusLength) { | ||
checkAlgorithmParams(algorithm) { | ||
// label | ||
if (algorithm.label | ||
@@ -584,2 +685,3 @@ && !(algorithm.label instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.label))) { | ||
checkGenerateKeyParams(algorithm) { | ||
// named curve | ||
this.checkRequiredProperty(algorithm, "namedCurve"); | ||
@@ -638,5 +740,6 @@ this.checkNamedCurve(algorithm.namedCurve); | ||
}; | ||
this.namedCurves = ["P-256", "P-384", "P-521"]; | ||
this.namedCurves = ["P-256", "P-384", "P-521", "K-256"]; | ||
} | ||
checkAlgorithmParams(algorithm) { | ||
// public | ||
this.checkRequiredProperty(algorithm, "public"); | ||
@@ -662,2 +765,6 @@ if (!(algorithm.public instanceof CryptoKey)) { | ||
} | ||
/** | ||
* Returns default size in bits by hash algorithm name | ||
* @param algName Name of the hash algorithm | ||
*/ | ||
getDefaultLength(algName) { | ||
@@ -678,4 +785,6 @@ switch (algName.toUpperCase()) { | ||
checkGenerateKeyParams(algorithm) { | ||
// hash | ||
this.checkRequiredProperty(algorithm, "hash"); | ||
this.checkHashAlgorithm(algorithm.hash, this.hashAlgorithms); | ||
// length | ||
if ("length" in algorithm) { | ||
@@ -691,2 +800,3 @@ if (typeof algorithm.length !== "number") { | ||
checkImportParams(algorithm) { | ||
// hash | ||
this.checkRequiredProperty(algorithm, "hash"); | ||
@@ -705,4 +815,6 @@ this.checkHashAlgorithm(algorithm.hash, this.hashAlgorithms); | ||
checkAlgorithmParams(algorithm) { | ||
// hash | ||
this.checkRequiredProperty(algorithm, "hash"); | ||
this.checkHashAlgorithm(algorithm.hash, this.hashAlgorithms); | ||
// salt | ||
this.checkRequiredProperty(algorithm, "salt"); | ||
@@ -712,2 +824,3 @@ if (!(algorithm.salt instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.salt))) { | ||
} | ||
// iterations | ||
this.checkRequiredProperty(algorithm, "iterations"); | ||
@@ -724,2 +837,3 @@ if (typeof algorithm.iterations !== "number") { | ||
if (extractable) { | ||
// If extractable is not false, then throw a SyntaxError | ||
throw new SyntaxError("extractable: Must be False"); | ||
@@ -738,4 +852,6 @@ } | ||
checkAlgorithmParams(algorithm) { | ||
// hash | ||
this.checkRequiredProperty(algorithm, "hash"); | ||
this.checkHashAlgorithm(algorithm.hash, this.hashAlgorithms); | ||
// salt | ||
this.checkRequiredProperty(algorithm, "salt"); | ||
@@ -745,2 +861,3 @@ if (!BufferSourceConverter.isBufferSource(algorithm.salt)) { | ||
} | ||
// info | ||
this.checkRequiredProperty(algorithm, "info"); | ||
@@ -754,2 +871,3 @@ if (!BufferSourceConverter.isBufferSource(algorithm.info)) { | ||
if (extractable) { | ||
// If extractable is not false, then throw a SyntaxError | ||
throw new SyntaxError("extractable: Must be False"); | ||
@@ -805,122 +923,152 @@ } | ||
} | ||
async digest(algorithm, data) { | ||
this.checkRequiredArguments(arguments, 2, "digest"); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = await provider.digest(preparedAlgorithm, preparedData); | ||
return result; | ||
digest(algorithm, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 2, "digest"); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = yield provider.digest(preparedAlgorithm, preparedData); | ||
return result; | ||
}); | ||
} | ||
async generateKey(algorithm, extractable, keyUsages) { | ||
this.checkRequiredArguments(arguments, 3, "generateKey"); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = await provider.generateKey({ ...preparedAlgorithm, name: provider.name }, extractable, keyUsages); | ||
return result; | ||
generateKey(algorithm, extractable, keyUsages) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 3, "generateKey"); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = yield provider.generateKey(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), extractable, keyUsages); | ||
return result; | ||
}); | ||
} | ||
async sign(algorithm, key, data) { | ||
this.checkRequiredArguments(arguments, 3, "sign"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = await provider.sign({ ...preparedAlgorithm, name: provider.name }, key, preparedData); | ||
return result; | ||
sign(algorithm, key, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 3, "sign"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = yield provider.sign(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), key, preparedData); | ||
return result; | ||
}); | ||
} | ||
async verify(algorithm, key, signature, data) { | ||
this.checkRequiredArguments(arguments, 4, "verify"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const preparedSignature = BufferSourceConverter.toArrayBuffer(signature); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = await provider.verify({ ...preparedAlgorithm, name: provider.name }, key, preparedSignature, preparedData); | ||
return result; | ||
verify(algorithm, key, signature, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 4, "verify"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const preparedSignature = BufferSourceConverter.toArrayBuffer(signature); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = yield provider.verify(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), key, preparedSignature, preparedData); | ||
return result; | ||
}); | ||
} | ||
async encrypt(algorithm, key, data) { | ||
this.checkRequiredArguments(arguments, 3, "encrypt"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = await provider.encrypt({ ...preparedAlgorithm, name: provider.name }, key, preparedData, { keyUsage: true }); | ||
return result; | ||
encrypt(algorithm, key, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 3, "encrypt"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = yield provider.encrypt(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), key, preparedData, { keyUsage: true }); | ||
return result; | ||
}); | ||
} | ||
async decrypt(algorithm, key, data) { | ||
this.checkRequiredArguments(arguments, 3, "decrypt"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = await provider.decrypt({ ...preparedAlgorithm, name: provider.name }, key, preparedData, { keyUsage: true }); | ||
return result; | ||
decrypt(algorithm, key, data) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 3, "decrypt"); | ||
this.checkCryptoKey(key); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(data); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = yield provider.decrypt(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), key, preparedData, { keyUsage: true }); | ||
return result; | ||
}); | ||
} | ||
async deriveBits(algorithm, baseKey, length) { | ||
this.checkRequiredArguments(arguments, 3, "deriveBits"); | ||
this.checkCryptoKey(baseKey); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = await provider.deriveBits({ ...preparedAlgorithm, name: provider.name }, baseKey, length, { keyUsage: true }); | ||
return result; | ||
deriveBits(algorithm, baseKey, length) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 3, "deriveBits"); | ||
this.checkCryptoKey(baseKey); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
const result = yield provider.deriveBits(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), baseKey, length, { keyUsage: true }); | ||
return result; | ||
}); | ||
} | ||
async deriveKey(algorithm, baseKey, derivedKeyType, extractable, keyUsages) { | ||
this.checkRequiredArguments(arguments, 5, "deriveKey"); | ||
const preparedDerivedKeyType = this.prepareAlgorithm(derivedKeyType); | ||
const importProvider = this.getProvider(preparedDerivedKeyType.name); | ||
importProvider.checkDerivedKeyParams(preparedDerivedKeyType); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
provider.checkCryptoKey(baseKey, "deriveKey"); | ||
const derivedBits = await provider.deriveBits({ ...preparedAlgorithm, name: provider.name }, baseKey, derivedKeyType.length, { keyUsage: false }); | ||
return this.importKey("raw", derivedBits, derivedKeyType, extractable, keyUsages); | ||
deriveKey(algorithm, baseKey, derivedKeyType, extractable, keyUsages) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 5, "deriveKey"); | ||
// check derivedKeyType | ||
const preparedDerivedKeyType = this.prepareAlgorithm(derivedKeyType); | ||
const importProvider = this.getProvider(preparedDerivedKeyType.name); | ||
importProvider.checkDerivedKeyParams(preparedDerivedKeyType); | ||
// derive bits | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
provider.checkCryptoKey(baseKey, "deriveKey"); | ||
const derivedBits = yield provider.deriveBits(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), baseKey, derivedKeyType.length, { keyUsage: false }); | ||
// import derived key | ||
return this.importKey("raw", derivedBits, derivedKeyType, extractable, keyUsages); | ||
}); | ||
} | ||
async exportKey(format, key) { | ||
this.checkRequiredArguments(arguments, 2, "exportKey"); | ||
this.checkCryptoKey(key); | ||
const provider = this.getProvider(key.algorithm.name); | ||
const result = await provider.exportKey(format, key); | ||
return result; | ||
exportKey(format, key) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 2, "exportKey"); | ||
this.checkCryptoKey(key); | ||
const provider = this.getProvider(key.algorithm.name); | ||
const result = yield provider.exportKey(format, key); | ||
return result; | ||
}); | ||
} | ||
async importKey(format, keyData, algorithm, extractable, keyUsages) { | ||
this.checkRequiredArguments(arguments, 5, "importKey"); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
if (["pkcs8", "spki", "raw"].indexOf(format) !== -1) { | ||
const preparedData = BufferSourceConverter.toArrayBuffer(keyData); | ||
return provider.importKey(format, preparedData, { ...preparedAlgorithm, name: provider.name }, extractable, keyUsages); | ||
} | ||
else { | ||
if (!keyData.kty) { | ||
throw new TypeError("keyData: Is not JSON"); | ||
importKey(format, keyData, algorithm, extractable, keyUsages) { | ||
return tslib.__awaiter(this, arguments, void 0, function* () { | ||
this.checkRequiredArguments(arguments, 5, "importKey"); | ||
const preparedAlgorithm = this.prepareAlgorithm(algorithm); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
if (["pkcs8", "spki", "raw"].indexOf(format) !== -1) { | ||
const preparedData = BufferSourceConverter.toArrayBuffer(keyData); | ||
return provider.importKey(format, preparedData, Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), extractable, keyUsages); | ||
} | ||
} | ||
return provider.importKey(format, keyData, { ...preparedAlgorithm, name: provider.name }, extractable, keyUsages); | ||
else { | ||
if (!keyData.kty) { | ||
throw new TypeError("keyData: Is not JSON"); | ||
} | ||
} | ||
return provider.importKey(format, keyData, Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), extractable, keyUsages); | ||
}); | ||
} | ||
async wrapKey(format, key, wrappingKey, wrapAlgorithm) { | ||
let keyData = await this.exportKey(format, key); | ||
if (format === "jwk") { | ||
const json = JSON.stringify(keyData); | ||
keyData = pvtsutils.Convert.FromUtf8String(json); | ||
} | ||
const preparedAlgorithm = this.prepareAlgorithm(wrapAlgorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(keyData); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
return provider.encrypt({ ...preparedAlgorithm, name: provider.name }, wrappingKey, preparedData, { keyUsage: false }); | ||
wrapKey(format, key, wrappingKey, wrapAlgorithm) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
let keyData = yield this.exportKey(format, key); | ||
if (format === "jwk") { | ||
const json = JSON.stringify(keyData); | ||
keyData = pvtsutils.Convert.FromUtf8String(json); | ||
} | ||
// encrypt key data | ||
const preparedAlgorithm = this.prepareAlgorithm(wrapAlgorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(keyData); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
return provider.encrypt(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), wrappingKey, preparedData, { keyUsage: false }); | ||
}); | ||
} | ||
async unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages) { | ||
const preparedAlgorithm = this.prepareAlgorithm(unwrapAlgorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(wrappedKey); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
let keyData = await provider.decrypt({ ...preparedAlgorithm, name: provider.name }, unwrappingKey, preparedData, { keyUsage: false }); | ||
if (format === "jwk") { | ||
try { | ||
keyData = JSON.parse(pvtsutils.Convert.ToUtf8String(keyData)); | ||
unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages) { | ||
return tslib.__awaiter(this, void 0, void 0, function* () { | ||
// decrypt wrapped key | ||
const preparedAlgorithm = this.prepareAlgorithm(unwrapAlgorithm); | ||
const preparedData = BufferSourceConverter.toArrayBuffer(wrappedKey); | ||
const provider = this.getProvider(preparedAlgorithm.name); | ||
let keyData = yield provider.decrypt(Object.assign(Object.assign({}, preparedAlgorithm), { name: provider.name }), unwrappingKey, preparedData, { keyUsage: false }); | ||
if (format === "jwk") { | ||
try { | ||
keyData = JSON.parse(pvtsutils.Convert.ToUtf8String(keyData)); | ||
} | ||
catch (e) { | ||
const error = new TypeError("wrappedKey: Is not a JSON"); | ||
error.internal = e; | ||
throw error; | ||
} | ||
} | ||
catch (e) { | ||
const error = new TypeError("wrappedKey: Is not a JSON"); | ||
error.internal = e; | ||
throw error; | ||
} | ||
} | ||
return this.importKey(format, keyData, unwrappedKeyAlgorithm, extractable, keyUsages); | ||
// import key | ||
return this.importKey(format, keyData, unwrappedKeyAlgorithm, extractable, keyUsages); | ||
}); | ||
} | ||
@@ -939,7 +1087,7 @@ checkRequiredArguments(args, size, methodName) { | ||
if (SubtleCrypto.isHashedAlgorithm(algorithm)) { | ||
const preparedAlgorithm = { ...algorithm }; | ||
const preparedAlgorithm = Object.assign({}, algorithm); | ||
preparedAlgorithm.hash = this.prepareAlgorithm(algorithm.hash); | ||
return preparedAlgorithm; | ||
} | ||
return { ...algorithm }; | ||
return Object.assign({}, algorithm); | ||
} | ||
@@ -946,0 +1094,0 @@ getProvider(name) { |
{ | ||
"name": "webcrypto-core", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"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": "build/webcrypto-core.d.ts", | ||
"module": "build/webcrypto-core.mjs", | ||
"types": "build/types/index.d.ts", | ||
"scripts": { | ||
"prepare": "npm run build", | ||
"build": "rollup -c", | ||
"build": "npm run build:module && npm run build:types", | ||
"clear": "rimraf build/*", | ||
"rebuild": "npm run clear && npm run build", | ||
"build:module": "rollup -c", | ||
"build:types": "tsc -p tsconfig.types.json", | ||
"test": "mocha", | ||
"prepub": "npm run build", | ||
"pub": "npm version patch && npm publish && git push", | ||
"prepub": "npm run rebuild", | ||
"pub": "npm version patch && npm publish", | ||
"postpub": "git push && git push --tags origin master", | ||
"coverage": "nyc npm test", | ||
@@ -34,3 +39,3 @@ "precoveragehtml": "npm run coverage", | ||
"dependencies": { | ||
"pvtsutils": "^1.0.4", | ||
"pvtsutils": "^1.0.6", | ||
"tslib": "^1.10.0" | ||
@@ -40,11 +45,12 @@ }, | ||
"@types/mocha": "^5.2.7", | ||
"@types/node": "^10.14.17", | ||
"coveralls": "^3.0.6", | ||
"mocha": "^6.2.0", | ||
"@types/node": "^10.17.6", | ||
"coveralls": "^3.0.9", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.1.1", | ||
"reflect-metadata": "^0.1.13", | ||
"rollup": "^1.20.3", | ||
"rollup-plugin-dts": "^0.14.0", | ||
"ts-node": "^8.3.0", | ||
"typescript": "^3.6.2" | ||
"rimraf": "^3.0.0", | ||
"rollup": "^1.27.8", | ||
"rollup-plugin-typescript2": "^0.25.3", | ||
"ts-node": "^8.5.4", | ||
"typescript": "^3.7.3" | ||
}, | ||
@@ -51,0 +57,0 @@ "author": "PeculiarVentures", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110193
45
2596
11
1
Updatedpvtsutils@^1.0.6