Skip to content

Commit

Permalink
fix: Align types with webcrypto-core and TS
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Oct 26, 2021
1 parent e768b16 commit 37dff39
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export class Crypto extends core.Crypto {

public subtle = new SubtleCrypto();

public getRandomValues<T extends ArrayBufferView>(array: T): T {
public getRandomValues<T extends ArrayBufferView | null>(array: T): T {
if (!ArrayBuffer.isView(array)) {
throw new TypeError("Failed to execute 'getRandomValues' on 'Crypto': parameter 1 is not of type 'ArrayBufferView'");
}
const buffer = Buffer.from(array.buffer);
crypto.randomFillSync(buffer);
return array;
Expand Down
4 changes: 2 additions & 2 deletions src/mechs/aes/aes_cmac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ export class AesCmacProvider extends core.AesCmacProvider {
return setCryptoKey(key);
}

public async onSign(algorithm: AesCmacParams, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
public async onSign(algorithm: core.AesCmacParams, key: AesCryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
const result = aesCmac(getCryptoKey(key).data, Buffer.from(data));
return new Uint8Array(result).buffer;
}

public async onVerify(algorithm: AesCmacParams, key: AesCryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> {
public async onVerify(algorithm: core.AesCmacParams, key: AesCryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> {
const signature2 = await this.sign(algorithm, key, data);
return Buffer.from(signature).compare(Buffer.from(signature2)) === 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/ed/ecdh_es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EdCrypto } from "./crypto";

export class EcdhEsProvider extends core.EcdhEsProvider {

public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await EdCrypto.generateKey(
{
name: this.name,
Expand Down
10 changes: 5 additions & 5 deletions src/mechs/ed/eddsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EdPublicKey } from "./public_key";

export class EdDsaProvider extends core.EdDsaProvider {

public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await EdCrypto.generateKey(
{
name: this.name,
Expand All @@ -21,19 +21,19 @@ export class EdDsaProvider extends core.EdDsaProvider {
publicKey: setCryptoKey(keys.publicKey as CryptoKey),
};
}

public async onSign(algorithm: EcdsaParams, key: CryptoKey, data: ArrayBuffer): Promise<ArrayBuffer> {
return EdCrypto.sign(algorithm, getCryptoKey(key) as EdPrivateKey, new Uint8Array(data));
}

public async onVerify(algorithm: EcdsaParams, key: CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise<boolean> {
return EdCrypto.verify(algorithm, getCryptoKey(key) as EdPublicKey, new Uint8Array(signature), new Uint8Array(data));
}

public async onExportKey(format: KeyFormat, key: CryptoKey): Promise<ArrayBuffer | JsonWebKey> {
return EdCrypto.exportKey(format, getCryptoKey(key));
}

public async onImportKey(format: KeyFormat, keyData: ArrayBuffer | JsonWebKey, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKey> {
const key = await EdCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages);
return setCryptoKey(key);
Expand Down
4 changes: 2 additions & 2 deletions src/mechs/rsa/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class RsaCrypto {
public static publicKeyUsages = ["verify", "encrypt", "wrapKey"];
public static privateKeyUsages = ["sign", "decrypt", "unwrapKey"];

public static async generateKey(algorithm: RsaHashedKeyGenParams | RsaKeyGenParams, extractable: boolean, keyUsages: string[]): Promise<CryptoKeyPair> {
public static async generateKey(algorithm: RsaHashedKeyGenParams | RsaKeyGenParams, extractable: boolean, keyUsages: string[]): Promise<core.CryptoKeyPair> {
const privateKey = new RsaPrivateKey();
privateKey.algorithm = algorithm as RsaHashedKeyAlgorithm;
privateKey.extractable = extractable;
Expand Down Expand Up @@ -50,7 +50,7 @@ export class RsaCrypto {
privateKey.data = keys.privateKey;
publicKey.data = keys.publicKey;

const res: CryptoKeyPair = {
const res = {
privateKey,
publicKey,
};
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class RsaEsProvider extends core.ProviderCrypto {
privateKey: ["decrypt", "unwrapKey"] as core.KeyUsages,
};

public async onGenerateKey(algorithm: RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await RsaCrypto.generateKey(
{
...algorithm,
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_oaep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { RsaPublicKey } from "./public_key";

export class RsaOaepProvider extends core.RsaOaepProvider {

public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await RsaCrypto.generateKey(
{
...algorithm,
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_pss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RsaPublicKey } from "./public_key";

export class RsaPssProvider extends core.RsaPssProvider {

public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await RsaCrypto.generateKey(
{
...algorithm,
Expand Down
2 changes: 1 addition & 1 deletion src/mechs/rsa/rsa_ssa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RsaPublicKey } from "./public_key";

export class RsaSsaProvider extends core.RsaSsaProvider {

public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKeyPair> {
const keys = await RsaCrypto.generateKey(
{
...algorithm,
Expand Down

0 comments on commit 37dff39

Please sign in to comment.