Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
// public
const jwk: JsonWebKey = {
crv: (key.algorithm as EcKeyGenParams).namedCurve,
ext: key.extractable,
x: Base64Url.encode(hex2buffer(hexX, true)),
y: Base64Url.encode(hex2buffer(hexY, true)),
key_ops: key.usages,
kty: "EC",
};
return jwk;
} else {
// private
const jwk: JsonWebKey = {
crv: (key.algorithm as EcKeyGenParams).namedCurve,
ext: key.extractable,
d: Base64Url.encode(hex2buffer(ecKey.getPrivate("hex"), true)),
x: Base64Url.encode(hex2buffer(hexX, true)),
y: Base64Url.encode(hex2buffer(hexY, true)),
key_ops: key.usages,
kty: "EC",
};
return jwk;
}
} else {
throw new LinerError(`Format '${format}' is not implemented`);
}
});
}
nativeKey.exportJwk(type, (err, data) => {
try {
const jwk: JsonWebKey = { kty: "RSA" };
jwk.key_ops = key.usages;
// convert base64 -> base64url for all props
jwk.e = Base64Url.encode(data.e);
jwk.n = Base64Url.encode(data.n);
if (key.type === "private") {
jwk.d = Base64Url.encode(data.d);
jwk.p = Base64Url.encode(data.p);
jwk.q = Base64Url.encode(data.q);
jwk.dp = Base64Url.encode(data.dp);
jwk.dq = Base64Url.encode(data.dq);
jwk.qi = Base64Url.encode(data.qi);
}
resolve(jwk);
} catch (e) {
reject(e);
}
});
break;
.then(() => {
if (format.toLowerCase() === "jwk") {
const jwk: JsonWebKey = {
kty: "RSA",
ext: true,
key_ops: key.usages,
};
jwk.alg = this.alg2jwk(key.algorithm as Algorithm);
jwk.n = Base64Url.encode(removeLeadingZero(key.key[0]));
jwk.e = Base64Url.encode(removeLeadingZero(key.key[1]));
if (key.type === "private") {
jwk.d = Base64Url.encode(removeLeadingZero(key.key[2]));
jwk.p = Base64Url.encode(removeLeadingZero(key.key[3]));
jwk.q = Base64Url.encode(removeLeadingZero(key.key[4]));
jwk.dp = Base64Url.encode(removeLeadingZero(key.key[5]));
jwk.dq = Base64Url.encode(removeLeadingZero(key.key[6]));
jwk.qi = Base64Url.encode(removeLeadingZero(key.key[7]));
}
return jwk;
} else {
throw new LinerError(LinerError.NOT_SUPPORTED);
}
});
}
.then(() => {
if (format.toLowerCase() === "jwk") {
const jwk: JsonWebKey = {
kty: "RSA",
ext: true,
key_ops: key.usages,
};
jwk.alg = this.alg2jwk(key.algorithm as Algorithm);
jwk.n = Base64Url.encode(removeLeadingZero(key.key[0]));
jwk.e = Base64Url.encode(removeLeadingZero(key.key[1]));
if (key.type === "private") {
jwk.d = Base64Url.encode(removeLeadingZero(key.key[2]));
jwk.p = Base64Url.encode(removeLeadingZero(key.key[3]));
jwk.q = Base64Url.encode(removeLeadingZero(key.key[4]));
jwk.dp = Base64Url.encode(removeLeadingZero(key.key[5]));
jwk.dq = Base64Url.encode(removeLeadingZero(key.key[6]));
jwk.qi = Base64Url.encode(removeLeadingZero(key.key[7]));
}
return jwk;
} else {
throw new LinerError(LinerError.NOT_SUPPORTED);
}
});
}
if (format.toLowerCase() === "jwk") {
const jwk: JsonWebKey = {
kty: "RSA",
ext: true,
key_ops: key.usages,
};
jwk.alg = this.alg2jwk(key.algorithm as Algorithm);
jwk.n = Base64Url.encode(removeLeadingZero(key.key[0]));
jwk.e = Base64Url.encode(removeLeadingZero(key.key[1]));
if (key.type === "private") {
jwk.d = Base64Url.encode(removeLeadingZero(key.key[2]));
jwk.p = Base64Url.encode(removeLeadingZero(key.key[3]));
jwk.q = Base64Url.encode(removeLeadingZero(key.key[4]));
jwk.dp = Base64Url.encode(removeLeadingZero(key.key[5]));
jwk.dq = Base64Url.encode(removeLeadingZero(key.key[6]));
jwk.qi = Base64Url.encode(removeLeadingZero(key.key[7]));
}
return jwk;
} else {
throw new LinerError(LinerError.NOT_SUPPORTED);
}
});
}
.then(() => {
if (format.toLowerCase() === "jwk") {
const jwk: JsonWebKey = {
kty: "RSA",
ext: true,
key_ops: key.usages,
};
jwk.alg = this.alg2jwk(key.algorithm as Algorithm);
jwk.n = Base64Url.encode(removeLeadingZero(key.key[0]));
jwk.e = Base64Url.encode(removeLeadingZero(key.key[1]));
if (key.type === "private") {
jwk.d = Base64Url.encode(removeLeadingZero(key.key[2]));
jwk.p = Base64Url.encode(removeLeadingZero(key.key[3]));
jwk.q = Base64Url.encode(removeLeadingZero(key.key[4]));
jwk.dp = Base64Url.encode(removeLeadingZero(key.key[5]));
jwk.dq = Base64Url.encode(removeLeadingZero(key.key[6]));
jwk.qi = Base64Url.encode(removeLeadingZero(key.key[7]));
}
return jwk;
} else {
throw new LinerError(LinerError.NOT_SUPPORTED);
}
});
}
.then(() => {
if (format.toLowerCase() === "jwk") {
const jwk: JsonWebKey = {
kty: "RSA",
ext: true,
key_ops: key.usages,
};
jwk.alg = this.alg2jwk(key.algorithm as Algorithm);
jwk.n = Base64Url.encode(removeLeadingZero(key.key[0]));
jwk.e = Base64Url.encode(removeLeadingZero(key.key[1]));
if (key.type === "private") {
jwk.d = Base64Url.encode(removeLeadingZero(key.key[2]));
jwk.p = Base64Url.encode(removeLeadingZero(key.key[3]));
jwk.q = Base64Url.encode(removeLeadingZero(key.key[4]));
jwk.dp = Base64Url.encode(removeLeadingZero(key.key[5]));
jwk.dq = Base64Url.encode(removeLeadingZero(key.key[6]));
jwk.qi = Base64Url.encode(removeLeadingZero(key.key[7]));
}
return jwk;
} else {
throw new LinerError(LinerError.NOT_SUPPORTED);
}
});
}