Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
extractable,
usages: keyUsages,
});
if (format.toLowerCase() === "jwk") {
const namedCurve = this.getNamedCurve((algorithm as EcKeyImportParams).namedCurve);
console.log(namedCurve);
const ecKey = new elliptic.ec(namedCurve);
if ((keyData as JsonWebKey).d) {
// Private key
key.key = ecKey.keyFromPrivate(Base64Url.decode((keyData as JsonWebKey).d!));
key.type = "private";
} else {
// Public key
const bufferPubKey = concat(
new Uint8Array([4]),
Base64Url.decode((keyData as JsonWebKey).x!),
Base64Url.decode((keyData as JsonWebKey).y!),
);
const hexPubKey = buffer2hex(bufferPubKey);
key.key = ecKey.keyFromPublic(hexPubKey, "hex");
key.type = "public";
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
return key;
});
}
usages: keyUsages,
});
if (format.toLowerCase() === "jwk") {
const namedCurve = this.getNamedCurve((algorithm as EcKeyImportParams).namedCurve);
console.log(namedCurve);
const ecKey = new elliptic.ec(namedCurve);
if ((keyData as JsonWebKey).d) {
// Private key
key.key = ecKey.keyFromPrivate(Base64Url.decode((keyData as JsonWebKey).d!));
key.type = "private";
} else {
// Public key
const bufferPubKey = concat(
new Uint8Array([4]),
Base64Url.decode((keyData as JsonWebKey).x!),
Base64Url.decode((keyData as JsonWebKey).y!),
);
const hexPubKey = buffer2hex(bufferPubKey);
key.key = ecKey.keyFromPublic(hexPubKey, "hex");
key.type = "public";
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
return key;
});
}
.then(() => {
const key: EcCryptoKey = new CryptoKey({
algorithm,
extractable,
usages: keyUsages,
});
if (format.toLowerCase() === "jwk") {
const namedCurve = this.getNamedCurve((algorithm as EcKeyImportParams).namedCurve);
console.log(namedCurve);
const ecKey = new elliptic.ec(namedCurve);
if ((keyData as JsonWebKey).d) {
// Private key
key.key = ecKey.keyFromPrivate(Base64Url.decode((keyData as JsonWebKey).d!));
key.type = "private";
} else {
// Public key
const bufferPubKey = concat(
new Uint8Array([4]),
Base64Url.decode((keyData as JsonWebKey).x!),
Base64Url.decode((keyData as JsonWebKey).y!),
);
const hexPubKey = buffer2hex(bufferPubKey);
key.key = ecKey.keyFromPublic(hexPubKey, "hex");
key.type = "public";
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
.then(() => {
let jwk: JsonWebKey;
const key = new CryptoKey({
algorithm,
extractable,
usages,
});
key.key = [];
if (format.toLowerCase() === "jwk") {
jwk = keyData as JsonWebKey;
key.key[0] = Base64Url.decode(jwk.n!);
key.key[1] = Base64Url.decode(jwk.e!)[0] === 3 ? new Uint8Array([0, 0, 0, 3]) : new Uint8Array([0, 1, 0, 1]);
if (jwk.d) {
key.type = "private";
key.key[2] = Base64Url.decode(jwk.d!);
key.key[3] = Base64Url.decode(jwk.p!);
key.key[4] = Base64Url.decode(jwk.q!);
key.key[5] = Base64Url.decode(jwk.dp!);
key.key[6] = Base64Url.decode(jwk.dq!);
key.key[7] = Base64Url.decode(jwk.qi!);
} else {
key.type = "public";
}
return key;
} else {
throw new LinerError(LinerError.NOT_SUPPORTED);
}
});
function b64_decode(b64url: string): Buffer {
return Buffer.from(Base64Url.decode(b64url));
}
.then(() => {
let jwk: JsonWebKey;
const key = new CryptoKey({
algorithm,
extractable,
usages,
});
key.key = [];
if (format.toLowerCase() === "jwk") {
jwk = keyData as JsonWebKey;
key.key[0] = Base64Url.decode(jwk.n!);
key.key[1] = Base64Url.decode(jwk.e!)[0] === 3 ? new Uint8Array([0, 0, 0, 3]) : new Uint8Array([0, 1, 0, 1]);
if (jwk.d) {
key.type = "private";
key.key[2] = Base64Url.decode(jwk.d!);
key.key[3] = Base64Url.decode(jwk.p!);
key.key[4] = Base64Url.decode(jwk.q!);
key.key[5] = Base64Url.decode(jwk.dp!);
key.key[6] = Base64Url.decode(jwk.dq!);
key.key[7] = Base64Url.decode(jwk.qi!);
} else {
key.type = "public";
}
return key;
} else {
throw new LinerError(LinerError.NOT_SUPPORTED);
}
});
}
public static async importKey(format: string, keyData: JsonWebKey | Uint8Array, algorithm: AlgorithmIdentifier, extractable: boolean, usages: KeyUsage[]): Promise {
let raw: Uint8Array;
if (format.toLowerCase() === "jwk") {
const jwk = keyData as JsonWebKey;
raw = Base64Url.decode(jwk.k!);
} else {
raw = new Uint8Array(keyData as Uint8Array);
}
const key = new CryptoKey({
type: "secret",
algorithm,
extractable,
usages,
});
key.key = raw;
return key;
}