1- import { SignatureKeyPair } from 'crypto/sign/SignatureKeyPair' ;
21import { WebCryptoRSASigKP } from 'crypto/sign/WebCryptoRSASigKP' ;
3- import { EncodingKeyPair } from './EncodingKeyPair' ;
4- import { RSA } from './RSA' ;
52import { WebCryptoRSAEncKP } from './WebCryptoRSAEncKP' ;
63
4+ import { DelegatingRSAImpl } from './DelegatingRSAImpl' ;
5+ import { RSA } from './RSA' ;
76
8- class WebCryptoRSA implements RSA {
9-
10- encKeyPair ? : EncodingKeyPair ;
11- signKeyPair ? : SignatureKeyPair ;
12-
13- async generateKey ( bits : number ) : Promise < void > {
14-
15- this . signKeyPair = new WebCryptoRSASigKP ( ) ;
16- await this . signKeyPair . generateKey ( { b : bits } ) ;
17-
18- this . encKeyPair = new WebCryptoRSAEncKP ( ) ;
19- await this . encKeyPair . loadKeyPair ( this . signKeyPair . getPublicKey ( ) , this . signKeyPair . getPrivateKey ( ) ) ;
20-
21- }
22-
23- async loadKeyPair ( format : string , publicKey : string , privateKey ?: string ) : Promise < void > {
24-
25- format ; // ignore
26-
27- this . signKeyPair = new WebCryptoRSASigKP ( ) ;
28- await this . signKeyPair . loadKeyPair ( publicKey , privateKey ) ;
29-
30- this . encKeyPair = new WebCryptoRSAEncKP ( ) ;
31- await this . encKeyPair . loadKeyPair ( publicKey , privateKey ) ;
32-
33- }
34-
35- getPublicKey ( ) : string {
36-
37- if ( this . signKeyPair === undefined ) {
38- throw new Error ( 'Trying to retrieve public key from uninitialized WebCrypto RSA KeyPair wrapper.' )
39- }
40-
41- return this . signKeyPair . getPublicKey ( ) ;
42- }
43-
44- getPrivateKey ( ) : string | undefined {
45-
46- if ( this . signKeyPair === undefined ) {
47- throw new Error ( 'Trying to retrieve private key from uninitialized WebCrypto RSA KeyPair wrapper.' )
48- }
49-
50-
51- return this . signKeyPair . getPrivateKey ( ) ;
52- }
53-
54- async sign ( text : string ) : Promise < string > {
55-
56- if ( this . signKeyPair === undefined ) {
57- throw new Error ( 'Trying to create signature using uninitialized WebCrypto RSA KeyPair wrapper.' )
58- }
59-
60- return this . signKeyPair ?. sign ( text ) ;
61- }
62-
63- async verify ( text : string , signature : string ) : Promise < boolean > {
64-
65- if ( this . signKeyPair === undefined ) {
66- throw new Error ( 'Trying to verify signature using uninitialized WebCrypto RSA KeyPair wrapper.' )
67- }
68-
69- return this . signKeyPair . verify ( text , signature ) ;
70- }
71-
72- async encrypt ( plainText : string ) : Promise < string > {
737
74- if ( this . encKeyPair === undefined ) {
75- throw new Error ( 'Trying to encrypt using uninitialized WebCrypto RSA KeyPair wrapper.' )
76- }
8+ class WebCryptoRSA extends DelegatingRSAImpl implements RSA {
779
78- return this . encKeyPair . encrypt ( plainText ) ;
10+ constructor ( ) {
11+ super ( new WebCryptoRSAEncKP ( ) , new WebCryptoRSASigKP ) ;
7912 }
8013
81- async decrypt ( cypherText : string ) : Promise < string > {
82-
83- if ( this . encKeyPair === undefined ) {
84- throw new Error ( 'Trying to decrypt using uninitialized WebCrypto RSA KeyPair wrapper.' )
85- }
86-
87- return this . encKeyPair . decrypt ( cypherText ) ;
88- }
14+ }
8915
90- }
16+ export { WebCryptoRSA } ;
0 commit comments