refactor: use TS override option

This commit is contained in:
microshine 2022-03-02 21:35:31 +03:00
parent b98aecb553
commit 7a3520e723
46 changed files with 157 additions and 172 deletions

1
index.d.ts vendored
View File

@ -1,6 +1,7 @@
export declare class Crypto implements globalThis.Crypto { export declare class Crypto implements globalThis.Crypto {
public subtle: SubtleCrypto; public subtle: SubtleCrypto;
public getRandomValues<T extends ArrayBufferView | null>(array: T): T; public getRandomValues<T extends ArrayBufferView | null>(array: T): T;
public randomUUID(): string;
} }
export declare class CryptoKey implements globalThis.CryptoKey { export declare class CryptoKey implements globalThis.CryptoKey {

View File

@ -2,38 +2,44 @@ import typescript from "rollup-plugin-typescript2";
import pkg from "./package.json"; import pkg from "./package.json";
const banner = [ const banner = [
"/**", "/*!",
" * Copyright (c) 2020 Peculiar Ventures, LLC", " Copyright (c) Peculiar Ventures, LLC",
" */", "*/",
"", "",
].join("\n"); ].join("\n");
const input = "src/index.ts"; const input = "src/index.ts";
const external = Object.keys(pkg.dependencies); const external = [
...["crypto", "process"],
...Object.keys(pkg.dependencies || {}),
];
export default { export default [
input, {
plugins: [ input,
typescript({ plugins: [
check: true, typescript({
clean: true, check: true,
tsconfigOverride: { clean: true,
compilerOptions: { tsconfigOverride: {
module: "ES2015", compilerOptions: {
module: "ES2015",
removeComments: true,
}
} }
} }),
}), ],
], external: [...external],
external: ["crypto", "process", ...external], output: [
output: [ {
{ banner,
banner, file: pkg.main,
file: pkg.main, format: "cjs",
format: "cjs", },
}, {
{ banner,
banner, file: pkg.module,
file: pkg.module, format: "es",
format: "es", },
}, ],
], },
}; ];

View File

@ -2,7 +2,7 @@ import { CryptoKey } from "./key";
export abstract class AsymmetricKey extends CryptoKey { export abstract class AsymmetricKey extends CryptoKey {
public abstract type: "public" | "private"; public abstract override type: "public" | "private";
public pem?: string; public pem?: string;
} }

View File

@ -4,15 +4,15 @@ import * as core from "webcrypto-core";
export class CryptoKey extends core.CryptoKey { export class CryptoKey extends core.CryptoKey {
public data: Buffer = Buffer.alloc(0); public data: Buffer = Buffer.alloc(0);
public algorithm: KeyAlgorithm = { name: "" }; public override algorithm: KeyAlgorithm = { name: "" };
@JsonProp({ name: "ext", type: JsonPropTypes.Boolean, optional: true }) @JsonProp({ name: "ext", type: JsonPropTypes.Boolean, optional: true })
public extractable = false; public override extractable = false;
public type: KeyType = "secret"; public override type: KeyType = "secret";
@JsonProp({ name: "key_ops", type: JsonPropTypes.String, repeated: true, optional: true }) @JsonProp({ name: "key_ops", type: JsonPropTypes.String, repeated: true, optional: true })
public usages: KeyUsage[] = []; public override usages: KeyUsage[] = [];
@JsonProp({ type: JsonPropTypes.String }) @JsonProp({ type: JsonPropTypes.String })
protected kty = "oct"; protected kty = "oct";

View File

@ -2,7 +2,7 @@ import { CryptoKey } from "./key";
export class SymmetricKey extends CryptoKey { export class SymmetricKey extends CryptoKey {
public readonly kty = "oct"; public override readonly kty = "oct";
public readonly type: "secret" = "secret"; public override readonly type: "secret" = "secret";
} }

View File

@ -34,7 +34,7 @@ export class AesCbcProvider extends core.AesCbcProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof AesCryptoKey)) { if (!(getCryptoKey(key) instanceof AesCryptoKey)) {
throw new TypeError("key: Is not a AesCryptoKey"); throw new TypeError("key: Is not a AesCryptoKey");

View File

@ -144,7 +144,7 @@ export class AesCmacProvider extends core.AesCmacProvider {
return setCryptoKey(res); return setCryptoKey(res);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof AesCryptoKey)) { if (!(getCryptoKey(key) instanceof AesCryptoKey)) {
throw new TypeError("key: Is not a AesCryptoKey"); throw new TypeError("key: Is not a AesCryptoKey");

View File

@ -34,7 +34,7 @@ export class AesCtrProvider extends core.AesCtrProvider {
return setCryptoKey(res); return setCryptoKey(res);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof AesCryptoKey)) { if (!(getCryptoKey(key) instanceof AesCryptoKey)) {
throw new TypeError("key: Is not a AesCryptoKey"); throw new TypeError("key: Is not a AesCryptoKey");

View File

@ -34,7 +34,7 @@ export class AesEcbProvider extends core.AesEcbProvider {
return setCryptoKey(res); return setCryptoKey(res);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof AesCryptoKey)) { if (!(getCryptoKey(key) instanceof AesCryptoKey)) {
throw new TypeError("key: Is not a AesCryptoKey"); throw new TypeError("key: Is not a AesCryptoKey");

View File

@ -34,7 +34,7 @@ export class AesGcmProvider extends core.AesGcmProvider {
return setCryptoKey(res); return setCryptoKey(res);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof AesCryptoKey)) { if (!(getCryptoKey(key) instanceof AesCryptoKey)) {
throw new TypeError("key: Is not a AesCryptoKey"); throw new TypeError("key: Is not a AesCryptoKey");

View File

@ -26,15 +26,15 @@ export class AesKwProvider extends core.AesKwProvider {
return setCryptoKey(res); return setCryptoKey(res);
} }
public async onEncrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onEncrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
return AesCrypto.encrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); return AesCrypto.encrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data));
} }
public async onDecrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDecrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data));
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof AesCryptoKey)) { if (!(getCryptoKey(key) instanceof AesCryptoKey)) {
throw new TypeError("key: Is not a AesCryptoKey"); throw new TypeError("key: Is not a AesCryptoKey");

View File

@ -5,10 +5,10 @@ import { SymmetricKey } from "../../keys";
export class AesCryptoKey extends SymmetricKey { export class AesCryptoKey extends SymmetricKey {
public algorithm!: AesKeyAlgorithm; public override algorithm!: AesKeyAlgorithm;
@JsonProp({name: "k", converter: JsonBase64UrlConverter}) @JsonProp({name: "k", converter: JsonBase64UrlConverter})
public data!: Buffer; public override data!: Buffer;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
@ -31,7 +31,7 @@ export class AesCryptoKey extends SymmetricKey {
} }
} }
public set alg(value: string) { public override set alg(value: string) {
// nothing, cause set is needed for json-schema, but is not used by module // nothing, cause set is needed for json-schema, but is not used by module
} }

View File

@ -44,7 +44,7 @@ export class DesCbcProvider extends core.DesProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof DesCryptoKey)) { if (!(getCryptoKey(key) instanceof DesCryptoKey)) {
throw new TypeError("key: Is not a DesCryptoKey"); throw new TypeError("key: Is not a DesCryptoKey");

View File

@ -44,7 +44,7 @@ export class DesEde3CbcProvider extends core.DesProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof DesCryptoKey)) { if (!(getCryptoKey(key) instanceof DesCryptoKey)) {
throw new TypeError("key: Is not a DesCryptoKey"); throw new TypeError("key: Is not a DesCryptoKey");

View File

@ -5,10 +5,10 @@ import { SymmetricKey } from "../../keys";
export class DesCryptoKey extends SymmetricKey { export class DesCryptoKey extends SymmetricKey {
public algorithm!: core.DesKeyAlgorithm; public override algorithm!: core.DesKeyAlgorithm;
@JsonProp({name: "k", converter: JsonBase64UrlConverter}) @JsonProp({name: "k", converter: JsonBase64UrlConverter})
public data!: Buffer; public override data!: Buffer;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
@ -23,7 +23,7 @@ export class DesCryptoKey extends SymmetricKey {
} }
} }
public set alg(value: string) { public override set alg(value: string) {
// nothing, cause set is needed for json-schema, but is not used by module // nothing, cause set is needed for json-schema, but is not used by module
} }

View File

@ -13,7 +13,7 @@ export class EcCrypto {
public static publicKeyUsages = ["verify"]; public static publicKeyUsages = ["verify"];
public static privateKeyUsages = ["sign", "deriveKey", "deriveBits"]; public static privateKeyUsages = ["sign", "deriveKey", "deriveBits"];
public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const privateKey = new EcPrivateKey(); const privateKey = new EcPrivateKey();
privateKey.algorithm = algorithm; privateKey.algorithm = algorithm;
privateKey.extractable = extractable; privateKey.extractable = extractable;

View File

@ -7,9 +7,9 @@ import { EcPublicKey } from "./public_key";
export class EcdhProvider extends core.EcdhProvider { export class EcdhProvider extends core.EcdhProvider {
public namedCurves = core.EcCurves.names; public override namedCurves = core.EcCurves.names;
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const keys = await EcCrypto.generateKey( const keys = await EcCrypto.generateKey(
{ {
...algorithm, ...algorithm,
@ -33,7 +33,7 @@ export class EcdhProvider extends core.EcdhProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
const internalKey = getCryptoKey(key); const internalKey = getCryptoKey(key);
if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) { if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) {

View File

@ -6,9 +6,9 @@ import { EcPublicKey } from "./public_key";
export class EcdsaProvider extends core.EcdsaProvider { export class EcdsaProvider extends core.EcdsaProvider {
public namedCurves = core.EcCurves.names; public override namedCurves = core.EcCurves.names;
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const keys = await EcCrypto.generateKey( const keys = await EcCrypto.generateKey(
{ {
...algorithm, ...algorithm,
@ -40,7 +40,7 @@ export class EcdsaProvider extends core.EcdsaProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
const internalKey = getCryptoKey(key); const internalKey = getCryptoKey(key);
if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) { if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) {

View File

@ -6,7 +6,7 @@ import { getOidByNamedCurve } from "./helper";
export class EcPrivateKey extends AsymmetricKey implements IJsonConvertible { export class EcPrivateKey extends AsymmetricKey implements IJsonConvertible {
public readonly type: "private" = "private"; public readonly type: "private" = "private";
public algorithm!: EcKeyAlgorithm; public override algorithm!: EcKeyAlgorithm;
public getKey() { public getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PrivateKeyInfo); const keyInfo = AsnParser.parse(this.data, core.asn1.PrivateKeyInfo);

View File

@ -7,7 +7,7 @@ import { getOidByNamedCurve } from "./helper";
export class EcPublicKey extends AsymmetricKey implements IJsonConvertible { export class EcPublicKey extends AsymmetricKey implements IJsonConvertible {
public readonly type: "public" = "public"; public readonly type: "public" = "public";
public algorithm!: EcKeyAlgorithm; public override algorithm!: EcKeyAlgorithm;
public getKey() { public getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PublicKeyInfo); const keyInfo = AsnParser.parse(this.data, core.asn1.PublicKeyInfo);

View File

@ -12,7 +12,7 @@ export class EdCrypto {
public static publicKeyUsages = ["verify"]; public static publicKeyUsages = ["verify"];
public static privateKeyUsages = ["sign", "deriveKey", "deriveBits"]; public static privateKeyUsages = ["sign", "deriveKey", "deriveBits"];
public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public static async generateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const privateKey = new EdPrivateKey(); const privateKey = new EdPrivateKey();
privateKey.algorithm = algorithm; privateKey.algorithm = algorithm;
privateKey.extractable = extractable; privateKey.extractable = extractable;

View File

@ -5,7 +5,7 @@ import { EdCrypto } from "./crypto";
export class EcdhEsProvider extends core.EcdhEsProvider { export class EcdhEsProvider extends core.EcdhEsProvider {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const keys = await EdCrypto.generateKey( const keys = await EdCrypto.generateKey(
{ {
name: this.name, name: this.name,

View File

@ -7,7 +7,7 @@ import { EdPublicKey } from "./public_key";
export class EdDsaProvider extends core.EdDsaProvider { export class EdDsaProvider extends core.EdDsaProvider {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const keys = await EdCrypto.generateKey( const keys = await EdCrypto.generateKey(
{ {
name: this.name, name: this.name,

View File

@ -6,7 +6,7 @@ import { getOidByNamedCurve } from "./helper";
export class EdPrivateKey extends AsymmetricKey implements IJsonConvertible { export class EdPrivateKey extends AsymmetricKey implements IJsonConvertible {
public readonly type: "private" = "private"; public readonly type: "private" = "private";
public algorithm!: EcKeyAlgorithm; public override algorithm!: EcKeyAlgorithm;
public getKey() { public getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PrivateKeyInfo); const keyInfo = AsnParser.parse(this.data, core.asn1.PrivateKeyInfo);

View File

@ -8,7 +8,7 @@ import { getOidByNamedCurve } from "./helper";
export class EdPublicKey extends AsymmetricKey implements IJsonConvertible { export class EdPublicKey extends AsymmetricKey implements IJsonConvertible {
public readonly type: "public" = "public"; public readonly type: "public" = "public";
public algorithm!: EcKeyAlgorithm; public override algorithm!: EcKeyAlgorithm;
public getKey() { public getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PublicKeyInfo); const keyInfo = AsnParser.parse(this.data, core.asn1.PublicKeyInfo);

View File

@ -43,7 +43,7 @@ export class HkdfProvider extends core.HkdfProvider {
return Buffer.concat(blocks).slice(0, byteLength); return Buffer.concat(blocks).slice(0, byteLength);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof HkdfCryptoKey)) { if (!(getCryptoKey(key) instanceof HkdfCryptoKey)) {
throw new TypeError("key: Is not HKDF CryptoKey"); throw new TypeError("key: Is not HKDF CryptoKey");

View File

@ -2,7 +2,7 @@ import { CryptoKey } from "../../keys";
export class HkdfCryptoKey extends CryptoKey { export class HkdfCryptoKey extends CryptoKey {
public data!: Buffer; public override data!: Buffer;
public algorithm!: KeyAlgorithm; public override algorithm!: KeyAlgorithm;
} }

View File

@ -21,7 +21,7 @@ export class HmacProvider extends core.HmacProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public async onSign(algorithm: Algorithm, key: HmacCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onSign(algorithm: Algorithm, key: HmacCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
const hash = key.algorithm.hash.name.replace("-", ""); const hash = key.algorithm.hash.name.replace("-", "");
const hmac = crypto.createHmac(hash, getCryptoKey(key).data) const hmac = crypto.createHmac(hash, getCryptoKey(key).data)
.update(Buffer.from(data)).digest(); .update(Buffer.from(data)).digest();
@ -29,7 +29,7 @@ export class HmacProvider extends core.HmacProvider {
return new Uint8Array(hmac).buffer; return new Uint8Array(hmac).buffer;
} }
public async onVerify(algorithm: Algorithm, key: HmacCryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> { public override async onVerify(algorithm: Algorithm, key: HmacCryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> {
const hash = key.algorithm.hash.name.replace("-", ""); const hash = key.algorithm.hash.name.replace("-", "");
const hmac = crypto.createHmac(hash, getCryptoKey(key).data) const hmac = crypto.createHmac(hash, getCryptoKey(key).data)
.update(Buffer.from(data)).digest(); .update(Buffer.from(data)).digest();
@ -74,7 +74,7 @@ export class HmacProvider extends core.HmacProvider {
} }
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof HmacCryptoKey)) { if (!(getCryptoKey(key) instanceof HmacCryptoKey)) {
throw new TypeError("key: Is not HMAC CryptoKey"); throw new TypeError("key: Is not HMAC CryptoKey");

View File

@ -5,9 +5,9 @@ import { CryptoKey } from "../../keys";
export class HmacCryptoKey extends CryptoKey { export class HmacCryptoKey extends CryptoKey {
@JsonProp({ name: "k", converter: JsonBase64UrlConverter }) @JsonProp({ name: "k", converter: JsonBase64UrlConverter })
public data!: Buffer; public override data!: Buffer;
public algorithm!: HmacKeyAlgorithm; public override algorithm!: HmacKeyAlgorithm;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore // @ts-ignore
@ -16,7 +16,7 @@ export class HmacCryptoKey extends CryptoKey {
return `HS${hash.replace("SHA-", "")}`; return `HS${hash.replace("SHA-", "")}`;
} }
protected set alg(value: string) { protected override set alg(value: string) {
// nothing, cause set is needed for json-schema, but is not used by module // nothing, cause set is needed for json-schema, but is not used by module
} }

View File

@ -31,7 +31,7 @@ export class Pbkdf2Provider extends core.Pbkdf2Provider {
throw new core.OperationError("format: Must be 'raw'"); throw new core.OperationError("format: Must be 'raw'");
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
if (!(getCryptoKey(key) instanceof PbkdfCryptoKey)) { if (!(getCryptoKey(key) instanceof PbkdfCryptoKey)) {
throw new TypeError("key: Is not PBKDF CryptoKey"); throw new TypeError("key: Is not PBKDF CryptoKey");

View File

@ -18,7 +18,7 @@ export class RsaCrypto {
public static publicKeyUsages = ["verify", "encrypt", "wrapKey"]; public static publicKeyUsages = ["verify", "encrypt", "wrapKey"];
public static privateKeyUsages = ["sign", "decrypt", "unwrapKey"]; public static privateKeyUsages = ["sign", "decrypt", "unwrapKey"];
public static async generateKey(algorithm: RsaHashedKeyGenParams | RsaKeyGenParams, extractable: boolean, keyUsages: string[]): Promise<core.CryptoKeyPair> { public static async generateKey(algorithm: RsaHashedKeyGenParams | RsaKeyGenParams, extractable: boolean, keyUsages: string[]): Promise<CryptoKeyPair> {
const privateKey = new RsaPrivateKey(); const privateKey = new RsaPrivateKey();
privateKey.algorithm = algorithm as RsaHashedKeyAlgorithm; privateKey.algorithm = algorithm as RsaHashedKeyAlgorithm;
privateKey.extractable = extractable; privateKey.extractable = extractable;

View File

@ -6,7 +6,7 @@ import { getJwkAlgorithm } from "./helper";
export class RsaPrivateKey extends AsymmetricKey { export class RsaPrivateKey extends AsymmetricKey {
public readonly type: "private" = "private"; public readonly type: "private" = "private";
public algorithm!: RsaHashedKeyAlgorithm; public override algorithm!: RsaHashedKeyAlgorithm;
public getKey() { public getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PrivateKeyInfo); const keyInfo = AsnParser.parse(this.data, core.asn1.PrivateKeyInfo);

View File

@ -6,7 +6,7 @@ import { getJwkAlgorithm } from "./helper";
export class RsaPublicKey extends AsymmetricKey { export class RsaPublicKey extends AsymmetricKey {
public readonly type: "public" = "public"; public readonly type: "public" = "public";
public algorithm!: RsaHashedKeyAlgorithm; public override algorithm!: RsaHashedKeyAlgorithm;
public getKey() { public getKey() {
const keyInfo = AsnParser.parse(this.data, core.asn1.PublicKeyInfo); const keyInfo = AsnParser.parse(this.data, core.asn1.PublicKeyInfo);

View File

@ -14,7 +14,7 @@ export class RsaEsProvider extends core.ProviderCrypto {
privateKey: ["decrypt", "unwrapKey"] as core.KeyUsages, privateKey: ["decrypt", "unwrapKey"] as core.KeyUsages,
}; };
public async onGenerateKey(algorithm: RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public override async onGenerateKey(algorithm: RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const keys = await RsaCrypto.generateKey( const keys = await RsaCrypto.generateKey(
{ {
...algorithm, ...algorithm,
@ -29,7 +29,7 @@ export class RsaEsProvider extends core.ProviderCrypto {
}; };
} }
public checkGenerateKeyParams(algorithm: RsaKeyGenParams) { public override checkGenerateKeyParams(algorithm: RsaKeyGenParams) {
// public exponent // public exponent
this.checkRequiredProperty(algorithm, "publicExponent"); this.checkRequiredProperty(algorithm, "publicExponent");
if (!(algorithm.publicExponent && algorithm.publicExponent instanceof Uint8Array)) { if (!(algorithm.publicExponent && algorithm.publicExponent instanceof Uint8Array)) {
@ -52,28 +52,28 @@ export class RsaEsProvider extends core.ProviderCrypto {
} }
} }
public async onEncrypt(algorithm: Algorithm, key: RsaPublicKey, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onEncrypt(algorithm: Algorithm, key: RsaPublicKey, data: ArrayBuffer): Promise<ArrayBuffer> {
const options = this.toCryptoOptions(key); const options = this.toCryptoOptions(key);
const enc = crypto.publicEncrypt(options, new Uint8Array(data)); const enc = crypto.publicEncrypt(options, new Uint8Array(data));
return new Uint8Array(enc).buffer; return new Uint8Array(enc).buffer;
} }
public async onDecrypt(algorithm: Algorithm, key: RsaPrivateKey, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDecrypt(algorithm: Algorithm, key: RsaPrivateKey, data: ArrayBuffer): Promise<ArrayBuffer> {
const options = this.toCryptoOptions(key); const options = this.toCryptoOptions(key);
const dec = crypto.privateDecrypt(options, new Uint8Array(data)); const dec = crypto.privateDecrypt(options, new Uint8Array(data));
return new Uint8Array(dec).buffer; return new Uint8Array(dec).buffer;
} }
public async onExportKey(format: KeyFormat, key: CryptoKey): Promise<JsonWebKey | ArrayBuffer> { public override async onExportKey(format: KeyFormat, key: CryptoKey): Promise<JsonWebKey | ArrayBuffer> {
return RsaCrypto.exportKey(format, getCryptoKey(key)); return RsaCrypto.exportKey(format, getCryptoKey(key));
} }
public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey> { public override async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKey> {
const key = await RsaCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); const key = await RsaCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages);
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
const internalKey = getCryptoKey(key); const internalKey = getCryptoKey(key);
if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) { if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) {

View File

@ -15,7 +15,7 @@ import { RsaPublicKey } from "./public_key";
export class RsaOaepProvider extends core.RsaOaepProvider { export class RsaOaepProvider extends core.RsaOaepProvider {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const keys = await RsaCrypto.generateKey( const keys = await RsaCrypto.generateKey(
{ {
...algorithm, ...algorithm,
@ -151,7 +151,7 @@ export class RsaOaepProvider extends core.RsaOaepProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
const internalKey = getCryptoKey(key); const internalKey = getCryptoKey(key);
if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) { if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) {

View File

@ -6,7 +6,7 @@ import { RsaPublicKey } from "./public_key";
export class RsaPssProvider extends core.RsaPssProvider { export class RsaPssProvider extends core.RsaPssProvider {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
const keys = await RsaCrypto.generateKey( const keys = await RsaCrypto.generateKey(
{ {
...algorithm, ...algorithm,
@ -38,7 +38,7 @@ export class RsaPssProvider extends core.RsaPssProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
const internalKey = getCryptoKey(key); const internalKey = getCryptoKey(key);
if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) { if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) {

View File

@ -6,7 +6,7 @@ import { RsaPublicKey } from "./public_key";
export class RsaSsaProvider extends core.RsaSsaProvider { export class RsaSsaProvider extends core.RsaSsaProvider {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> { public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<globalThis.CryptoKeyPair> {
const keys = await RsaCrypto.generateKey( const keys = await RsaCrypto.generateKey(
{ {
...algorithm, ...algorithm,
@ -38,7 +38,7 @@ export class RsaSsaProvider extends core.RsaSsaProvider {
return setCryptoKey(key); return setCryptoKey(key);
} }
public checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
super.checkCryptoKey(key, keyUsage); super.checkCryptoKey(key, keyUsage);
const internalKey = getCryptoKey(key); const internalKey = getCryptoKey(key);
if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) { if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) {

View File

@ -5,7 +5,7 @@ export class Sha1Provider extends core.ProviderCrypto {
public name = "SHA-1"; public name = "SHA-1";
public usages = []; public usages = [];
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> {
return ShaCrypto.digest(algorithm, data); return ShaCrypto.digest(algorithm, data);
} }

View File

@ -5,7 +5,7 @@ export class Sha256Provider extends core.ProviderCrypto {
public name = "SHA-256"; public name = "SHA-256";
public usages = []; public usages = [];
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> {
return ShaCrypto.digest(algorithm, data); return ShaCrypto.digest(algorithm, data);
} }

View File

@ -5,7 +5,7 @@ export class Sha384Provider extends core.ProviderCrypto {
public name = "SHA-384"; public name = "SHA-384";
public usages = []; public usages = [];
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> {
return ShaCrypto.digest(algorithm, data); return ShaCrypto.digest(algorithm, data);
} }

View File

@ -5,7 +5,7 @@ export class Sha512Provider extends core.ProviderCrypto {
public name = "SHA-512"; public name = "SHA-512";
public usages = []; public usages = [];
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> {
return ShaCrypto.digest(algorithm, data); return ShaCrypto.digest(algorithm, data);
} }

View File

@ -1,10 +1,12 @@
import crypto from "crypto"; import crypto from "crypto";
import * as core from "webcrypto-core";
export class ShakeCrypto { export class ShakeCrypto {
public static digest(algorithm: Algorithm, data: ArrayBuffer) { public static digest(algorithm: Required<core.ShakeParams>, data: ArrayBuffer) {
const hash = crypto.createHash(algorithm.name.toLowerCase()) const hash = crypto.createHash(algorithm.name.toLowerCase(), {outputLength: algorithm.length})
.update(Buffer.from(data)).digest(); .update(Buffer.from(data)).digest();
return new Uint8Array(hash).buffer; return new Uint8Array(hash).buffer;
} }

View File

@ -1,11 +1,9 @@
import * as core from "webcrypto-core"; import * as core from "webcrypto-core";
import { ShakeCrypto } from "./crypto"; import { ShakeCrypto } from "./crypto";
export class Shake128Provider extends core.ProviderCrypto { export class Shake128Provider extends core.Shake128Provider {
public name = "shake128";
public usages = [];
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDigest(algorithm: Required<core.ShakeParams>, data: ArrayBuffer): Promise<ArrayBuffer> {
return ShakeCrypto.digest(algorithm, data); return ShakeCrypto.digest(algorithm, data);
} }

View File

@ -1,11 +1,9 @@
import * as core from "webcrypto-core"; import * as core from "webcrypto-core";
import { ShakeCrypto } from "./crypto"; import { ShakeCrypto } from "./crypto";
export class Shake256Provider extends core.ProviderCrypto { export class Shake256Provider extends core.Shake256Provider {
public name = "shake256";
public usages = [];
public async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise<ArrayBuffer> { public override async onDigest(algorithm: Required<core.ShakeParams>, data: ArrayBuffer): Promise<ArrayBuffer> {
return ShakeCrypto.digest(algorithm, data); return ShakeCrypto.digest(algorithm, data);
} }

View File

@ -60,7 +60,7 @@ context("Crypto", () => {
hash: "SHA-256", hash: "SHA-256",
info: new Uint8Array([1, 2, 3, 4, 5]), info: new Uint8Array([1, 2, 3, 4, 5]),
salt: new Uint8Array([1, 2, 3, 4, 5]), salt: new Uint8Array([1, 2, 3, 4, 5]),
} as globalThis.HkdfParams, } as HkdfParams,
hkdf, hkdf,
{ {
name: "HMAC", name: "HMAC",
@ -79,7 +79,7 @@ context("Crypto", () => {
const alg: globalThis.RsaHashedKeyGenParams = { const alg: globalThis.RsaHashedKeyGenParams = {
name: "RSASSA-PKCS1-v1_5", name: "RSASSA-PKCS1-v1_5",
hash: "SHA-256", hash: "SHA-256",
publicExponent: new Uint8Array([1,0,1]), publicExponent: new Uint8Array([1, 0, 1]),
modulusLength: 3072, modulusLength: 3072,
}; };
const keys = await crypto.subtle.generateKey(alg, false, ["sign", "verify"]); const keys = await crypto.subtle.generateKey(alg, false, ["sign", "verify"]);
@ -230,16 +230,36 @@ context("Crypto", () => {
const data = Buffer.from("test data"); const data = Buffer.from("test data");
it("shake128", async () => { context("shake128", () => {
const hash = await crypto.subtle.digest("shake128", data);
it("default", async () => {
const hash = await crypto.subtle.digest("shake128", data);
assert.strictEqual(Buffer.from(hash).toString("hex"), "ae3bdcf04986a8e7ddd99ac948254693");
});
it("128 byte length", async () => {
const hash = await crypto.subtle.digest({ name: "shake128", length: 128 } as core.ShakeParams, data);
assert.strictEqual(Buffer.from(hash).toString("hex"), "ae3bdcf04986a8e7ddd99ac948254693fc32ca6ce3ed278c0c54127f072ba21e977d76aa76cab8f85f61c3e1fb7dab42c6b96d39f96fbd8cdcba7121e28cc97bb51f277a00398f99a9e6f11d027473cbffb3ac4ce444e2e8284caeca4e62f725d340fa3519eec7ca3eb4188607c26b0ecdf3750beba8882d6f2b734960cca914");
});
assert.strictEqual(Buffer.from(hash).toString("hex"), "ae3bdcf04986a8e7ddd99ac948254693");
}); });
it("shake256", async () => { context("shake128", () => {
const hash = await crypto.subtle.digest("shake256", data);
it("default", async () => {
assert.strictEqual(Buffer.from(hash).toString("hex"), "be15253026b9a85e01ae54b1939284e8e514fbdad2a3bd5c1c0f437e60548e26"); const hash = await crypto.subtle.digest("shake256", data);
assert.strictEqual(Buffer.from(hash).toString("hex"), "be15253026b9a85e01ae54b1939284e8e514fbdad2a3bd5c1c0f437e60548e26");
});
it("256 byte length", async () => {
const hash = await crypto.subtle.digest({ name: "shake256", length: 256 } as core.ShakeParams, data);
assert.strictEqual(Buffer.from(hash).toString("hex"), "be15253026b9a85e01ae54b1939284e8e514fbdad2a3bd5c1c0f437e60548e262dd68c2a2f932847f9610eeb51f8ba1a180ca878c788e900d899538d45c9c4a6f1bf10d8502a7ccbd9fd540bd856591000700e10130673ef970ffb788afe08426648a216d032733b71e85f128f1ed9e4c8bd910b5000e8c381afb45735680eaf7cb5bf1ae4265ee0822dfe6a9426ff21e309398df57cbf5861f5947f3d261e2d4517ff0d1be988e7014a09c4312d37010cf0e47468c1cf832e6a61e9d9fe3b67e6ab265cb6d95ad7a1f863d71e0e6ed5cd17d568b86e99d84bdb970a580f551017b501ae6761d2d6de76a64385dc10f27d18c2564a6bfbfb1e3f335010bebdf8");
});
}); });
}); });

View File

@ -1,59 +1,19 @@
{ {
"compilerOptions": { "compilerOptions": {
/* Basic Options */ "target": "es2019",
"target": "es2018", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ "module": "commonjs",
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ "lib": [
// "lib": [], /* Specify library files to be included in the compilation. */ "dom"
// "allowJs": true, /* Allow javascript files to be compiled. */ ],
// "checkJs": true, /* Report errors in .js files. */ "removeComments": true,
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ "importHelpers": true,
// "declaration": true, /* Generates corresponding '.d.ts' file. */ "strict": true,
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ "esModuleInterop": true,
// "sourceMap": true, /* Generates corresponding '.map' file. */ "experimentalDecorators": true,
// "outFile": "./", /* Concatenate and emit output to single file. */ "noImplicitOverride": true
// "outDir": "./", /* Redirect output structure to the directory. */ },
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ "exclude": [
// "composite": true, /* Enable project compilation */ "build/**/*",
"removeComments": true, /* Do not emit comments to output. */ "index.d.ts"
// "noEmit": true, /* Do not emit outputs. */ ]
"importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
}
} }