1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-28 06:03:40 +00:00

extract public from private key

This commit is contained in:
Kyle Spearrin
2018-07-02 23:53:44 -04:00
parent e22915818c
commit 2bc7ae0da2
5 changed files with 49 additions and 1 deletions

View File

@@ -204,6 +204,25 @@ export class WebCryptoFunctionService implements CryptoFunctionService {
return await this.subtle.decrypt(rsaParams, impKey, data);
}
async rsaExtractPublicKey(privateKey: ArrayBuffer): Promise<ArrayBuffer> {
const rsaParams = {
name: 'RSA-OAEP',
// Have to specify some algorithm
hash: { name: this.toWebCryptoAlgorithm('sha1') },
};
const impPrivateKey = await this.subtle.importKey('pkcs8', privateKey, rsaParams, true, ['decrypt']);
const jwkPrivateKey = await this.subtle.exportKey('jwk', impPrivateKey);
const jwkPublicKeyParams = {
kty: 'RSA',
e: jwkPrivateKey.e,
n: jwkPrivateKey.n,
alg: 'RSA-OAEP',
ext: true,
};
const impPublicKey = await this.subtle.importKey('jwk', jwkPublicKeyParams, rsaParams, true, ['encrypt']);
return await this.subtle.exportKey('spki', impPublicKey);
}
randomBytes(length: number): Promise<ArrayBuffer> {
const arr = new Uint8Array(length);
this.crypto.getRandomValues(arr);