1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

rsaDecrypt implementation

This commit is contained in:
Kyle Spearrin
2018-04-19 00:00:53 -04:00
parent e20e878b8b
commit 97fe01e131
3 changed files with 34 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
import * as crypto from 'crypto';
import * as constants from 'constants';
import { CryptoFunctionService } from '../abstractions/cryptoFunction.service';
@@ -56,6 +57,20 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
return Promise.resolve(decBuf.buffer);
}
rsaDecrypt(data: ArrayBuffer, key: ArrayBuffer, algorithm: 'sha1' | 'sha256'): Promise<ArrayBuffer> {
if (algorithm !== 'sha1') {
throw new Error('only sha1 is supported on this platform.');
}
const nodeData = this.toNodeBuffer(data);
const rsaKey: crypto.RsaPrivateKey = {
key: this.toPem(key),
padding: constants.RSA_PKCS1_OAEP_PADDING,
};
const decBuf = crypto.publicDecrypt(rsaKey, nodeData);
return Promise.resolve(decBuf.buffer);
}
randomBytes(length: number): Promise<ArrayBuffer> {
return new Promise<ArrayBuffer>((resolve, reject) => {
crypto.randomBytes(length, (error, bytes) => {
@@ -81,4 +96,9 @@ export class NodeCryptoFunctionService implements CryptoFunctionService {
private toNodeBuffer(value: ArrayBuffer): Buffer {
return Buffer.from(new Uint8Array(value) as any);
}
private toPem(key: ArrayBuffer): string {
const b64Key = ''; // TODO: key to b84
return '-----BEGIN PRIVATE KEY-----\n' + b64Key + '\n-----END PRIVATE KEY-----';
}
}