Skip to content

Commit

Permalink
fix: type annotations and add missing return types
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Jan 22, 2024
1 parent e7e9e2c commit c83d87e
Show file tree
Hide file tree
Showing 28 changed files with 94 additions and 99 deletions.
4 changes: 2 additions & 2 deletions src/aes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CryptoKey } from "../crypto_key";

export abstract class AesProvider extends ProviderCrypto {

public checkGenerateKeyParams(algorithm: AesKeyGenParams) {
public checkGenerateKeyParams(algorithm: AesKeyGenParams): void {
// length
this.checkRequiredProperty(algorithm, "length");
if (typeof algorithm.length !== "number") {
Expand All @@ -19,7 +19,7 @@ export abstract class AesProvider extends ProviderCrypto {
}
}

public checkDerivedKeyParams(algorithm: AesKeyGenParams) {
public checkDerivedKeyParams(algorithm: AesKeyGenParams): void {
this.checkGenerateKeyParams(algorithm);
}

Expand Down
2 changes: 1 addition & 1 deletion src/aes/cbc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export abstract class AesCbcProvider extends AesProvider {

public usages: KeyUsages = ["encrypt", "decrypt", "wrapKey", "unwrapKey"];

public checkAlgorithmParams(algorithm: AesCbcParams) {
public checkAlgorithmParams(algorithm: AesCbcParams): void {
this.checkRequiredProperty(algorithm, "iv");
if (!(algorithm.iv instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.iv))) {
throw new TypeError("iv: Is not of type '(ArrayBuffer or ArrayBufferView)'");
Expand Down
2 changes: 1 addition & 1 deletion src/aes/cmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class AesCmacProvider extends AesProvider {

public usages: KeyUsages = ["sign", "verify"];

public checkAlgorithmParams(algorithm: AesCmacParams) {
public checkAlgorithmParams(algorithm: AesCmacParams): void {
this.checkRequiredProperty(algorithm, "length");
if (typeof algorithm.length !== "number") {
throw new TypeError("length: Is not a Number");
Expand Down
2 changes: 1 addition & 1 deletion src/aes/ctr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export abstract class AesCtrProvider extends AesProvider {

public usages: KeyUsages = ["encrypt", "decrypt", "wrapKey", "unwrapKey"];

public checkAlgorithmParams(algorithm: AesCtrParams) {
public checkAlgorithmParams(algorithm: AesCtrParams): void {
// counter
this.checkRequiredProperty(algorithm, "counter");
if (!(algorithm.counter instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.counter))) {
Expand Down
4 changes: 2 additions & 2 deletions src/aes/gsm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export abstract class AesGcmProvider extends AesProvider {

public usages: KeyUsages = ["encrypt", "decrypt", "wrapKey", "unwrapKey"];

public checkAlgorithmParams(algorithm: AesGcmParams) {
public checkAlgorithmParams(algorithm: AesGcmParams): void {
// iv
this.checkRequiredProperty(algorithm, "iv");
if (!(algorithm.iv instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.iv))) {
Expand All @@ -29,7 +29,7 @@ export abstract class AesGcmProvider extends AesProvider {
case 112:
case 120:
case 128:
break;
break;
default:
throw new OperationError("tagLength: Must be one of 32, 64, 96, 104, 112, 120 or 128");
}
Expand Down
3 changes: 1 addition & 2 deletions src/asn1/ec_private_key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as asn1js from "asn1js";
import { AsnIntegerConverter, AsnProp, AsnPropTypes, AsnSerializer } from "@peculiar/asn1-schema";
import { IJsonConvertible } from "@peculiar/json-schema";
import { Convert } from "pvtsutils";
Expand Down Expand Up @@ -46,7 +45,7 @@ export class EcPrivateKey implements IJsonConvertible {

return this;
}
public toJSON() {
public toJSON(): JsonWebKey {
const jwk: JsonWebKey = {};
jwk.d = Convert.ToBase64Url(this.privateKey);
if (this.publicKey) {
Expand Down
2 changes: 1 addition & 1 deletion src/asn1/ec_public_key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class EcPublicKey implements IJsonConvertible {
}
}

public toJSON() {
public toJSON(): JsonWebKey {
let bytes = new Uint8Array(this.value);

if (bytes[0] !== 0x04) {
Expand Down
4 changes: 2 additions & 2 deletions src/asn1/ed_private_key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsnIntegerConverter, AsnProp, AsnPropTypes, AsnSerializer, AsnType, AsnTypeTypes } from "@peculiar/asn1-schema";
import { AsnProp, AsnPropTypes, AsnType, AsnTypeTypes } from "@peculiar/asn1-schema";
import { IJsonConvertible } from "@peculiar/json-schema";
import { Convert } from "pvtsutils";

Expand All @@ -16,7 +16,7 @@ export class EdPrivateKey implements IJsonConvertible {

return this;
}
public toJSON() {
public toJSON(): JsonWebKey {
const jwk: JsonWebKey = {
d: Convert.ToBase64Url(this.value),
};
Expand Down
5 changes: 2 additions & 3 deletions src/asn1/ed_public_key.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AsnProp, AsnPropTypes, AsnType, AsnTypeTypes } from "@peculiar/asn1-schema";
import { IJsonConvertible } from "@peculiar/json-schema";
import { Convert } from "pvtsutils";
import { CryptoError } from "../errors";

// RFC 8410
// https://datatracker.ietf.org/doc/html/rfc8410
Expand All @@ -20,8 +19,8 @@ export class EdPublicKey implements IJsonConvertible {
}
}

public toJSON() {
const json = {
public toJSON(): JsonWebKey {
const json: JsonWebKey = {
x: Convert.ToBase64Url(this.value),
};

Expand Down
2 changes: 1 addition & 1 deletion src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export abstract class Crypto implements globalThis.Crypto {
public abstract readonly subtle: SubtleCrypto;

// @internal
public get [Symbol.toStringTag]() {
public get [Symbol.toStringTag](): string {
return "Crypto";
}

Expand Down
6 changes: 3 additions & 3 deletions src/crypto_key.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeyUsages, NativeCryptoKey } from "./types";
import { KeyUsages } from "./types";

// tslint:disable-next-line:no-empty-interface
export interface KeyAlgorithm extends Algorithm {
Expand All @@ -8,7 +8,7 @@ const KEY_TYPES = ["secret", "private", "public"];

export class CryptoKey implements globalThis.CryptoKey {

public static create<T extends CryptoKey>(this: new() => T, algorithm: KeyAlgorithm, type: KeyType, extractable: boolean, usages: KeyUsages): T {
public static create<T extends CryptoKey>(this: new () => T, algorithm: KeyAlgorithm, type: KeyType, extractable: boolean, usages: KeyUsages): T {
const key = new this();
key.algorithm = algorithm;
key.type = type;
Expand All @@ -28,7 +28,7 @@ export class CryptoKey implements globalThis.CryptoKey {
public extractable!: boolean;

// @internal
public get[Symbol.toStringTag]() {
public get [Symbol.toStringTag](): string {
return "CryptoKey";
}
}
6 changes: 3 additions & 3 deletions src/des/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export abstract class DesProvider extends ProviderCrypto {
public abstract keySizeBits: number;
public abstract ivSize: number;

public checkAlgorithmParams(algorithm: AesCbcParams) {
public checkAlgorithmParams(algorithm: AesCbcParams): void {
if (this.ivSize) {
this.checkRequiredProperty(algorithm, "iv");
if (!(algorithm.iv instanceof ArrayBuffer || ArrayBuffer.isView(algorithm.iv))) {
Expand All @@ -40,7 +40,7 @@ export abstract class DesProvider extends ProviderCrypto {
}
}

public checkGenerateKeyParams(algorithm: DesKeyGenParams) {
public checkGenerateKeyParams(algorithm: DesKeyGenParams): void {
// length
this.checkRequiredProperty(algorithm, "length");
if (typeof algorithm.length !== "number") {
Expand All @@ -51,7 +51,7 @@ export abstract class DesProvider extends ProviderCrypto {
}
}

public checkDerivedKeyParams(algorithm: DesDerivedKeyParams) {
public checkDerivedKeyParams(algorithm: DesDerivedKeyParams): void {
this.checkGenerateKeyParams(algorithm);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ec/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ export abstract class EllipticProvider extends ProviderCrypto {

public abstract namedCurves: string[];

public checkGenerateKeyParams(algorithm: EcKeyGenParams) {
public checkGenerateKeyParams(algorithm: EcKeyGenParams): void {
// named curve
this.checkRequiredProperty(algorithm, "namedCurve");
this.checkNamedCurve(algorithm.namedCurve);
}

public checkNamedCurve(namedCurve: string) {
public checkNamedCurve(namedCurve: string): void {
for (const item of this.namedCurves) {
if (item.toLowerCase() === namedCurve.toLowerCase()) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/ec/curves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class EcCurves {

private constructor() { }

public static register(item: EcCurveParams) {
public static register(item: EcCurveParams): void {
const oid = new asn1.ObjectIdentifier();
oid.value = item.id;
const raw = AsnConvert.serialize(oid);
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export abstract class EcdhProvider extends EllipticProvider {

public namedCurves = ["P-256", "P-384", "P-521", "K-256"];

public checkAlgorithmParams(algorithm: EcdhKeyDeriveParams) {
public checkAlgorithmParams(algorithm: EcdhKeyDeriveParams): void {
// public
this.checkRequiredProperty(algorithm, "public");
if (!(algorithm.public instanceof CryptoKey)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ec/ecdsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export abstract class EcdsaProvider extends EllipticProvider {

public namedCurves = ["P-256", "P-384", "P-521", "K-256"];

public checkAlgorithmParams(algorithm: EcdsaParams) {
public checkAlgorithmParams(algorithm: EcdsaParams): void {
this.checkRequiredProperty(algorithm, "hash");
this.checkHashAlgorithm(algorithm.hash as Algorithm, this.hashAlgorithms);
}
Expand Down
6 changes: 3 additions & 3 deletions src/hkdf/hkdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export abstract class HkdfProvider extends ProviderCrypto {
public hashAlgorithms = ["SHA-1", "SHA-256", "SHA-384", "SHA-512"];
public usages: KeyUsages = ["deriveKey", "deriveBits"];

public checkAlgorithmParams(algorithm: HkdfParams) {
public checkAlgorithmParams(algorithm: HkdfParams): void {
// hash
this.checkRequiredProperty(algorithm, "hash");
this.checkHashAlgorithm(algorithm.hash as Algorithm, this.hashAlgorithms);
Expand All @@ -27,8 +27,8 @@ export abstract class HkdfProvider extends ProviderCrypto {
}
}

public checkImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[], ...args: any[]) {
super.checkImportKey(format, keyData, algorithm, extractable, keyUsages);
public checkImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[], ...args: any[]): void {
super.checkImportKey(format, keyData, algorithm, extractable, keyUsages, ...args);
if (extractable) {
// If extractable is not false, then throw a SyntaxError
throw new SyntaxError("extractable: Must be 'false'");
Expand Down
6 changes: 3 additions & 3 deletions src/hmac/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export abstract class HmacProvider extends ProviderCrypto {
* Returns default size in bits by hash algorithm name
* @param algName Name of the hash algorithm
*/
public getDefaultLength(algName: string) {
public getDefaultLength(algName: string): number {
switch (algName.toUpperCase()) {
// Chrome, Safari and Firefox returns 512
case "SHA-1":
Expand All @@ -27,7 +27,7 @@ export abstract class HmacProvider extends ProviderCrypto {
}
}

public checkGenerateKeyParams(algorithm: HmacKeyGenParams) {
public checkGenerateKeyParams(algorithm: HmacKeyGenParams): void {
// hash
this.checkRequiredProperty(algorithm, "hash");
this.checkHashAlgorithm(algorithm.hash as Algorithm, this.hashAlgorithms);
Expand All @@ -43,7 +43,7 @@ export abstract class HmacProvider extends ProviderCrypto {
}
}

public checkImportParams(algorithm: HmacImportParams) {
public checkImportParams(algorithm: HmacImportParams): void {
// hash
this.checkRequiredProperty(algorithm, "hash");
this.checkHashAlgorithm(algorithm.hash as Algorithm, this.hashAlgorithms);
Expand Down
2 changes: 1 addition & 1 deletion src/json/converters/base64_url.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IJsonConverter } from "@peculiar/json-schema";
import { Convert, BufferSourceConverter } from "pvtsutils";
import { Convert } from "pvtsutils";

export const JsonBase64UrlArrayBufferConverter: IJsonConverter<ArrayBuffer, string> = {
fromJSON: (value: string) => Convert.FromBase64Url(value),
Expand Down
6 changes: 3 additions & 3 deletions src/pbkdf/pbkdf2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export abstract class Pbkdf2Provider extends ProviderCrypto {

public usages: KeyUsages = ["deriveBits", "deriveKey"];

public checkAlgorithmParams(algorithm: Pbkdf2Params) {
public checkAlgorithmParams(algorithm: Pbkdf2Params): void {
// hash
this.checkRequiredProperty(algorithm, "hash");
this.checkHashAlgorithm(algorithm.hash as Algorithm, this.hashAlgorithms);
Expand All @@ -31,8 +31,8 @@ export abstract class Pbkdf2Provider extends ProviderCrypto {
}
}

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

0 comments on commit c83d87e

Please sign in to comment.