1- import { RSA , RSAImpl } from 'crypto/ciphers' ;
1+ import { RSA , RSADefaults } from 'crypto/ciphers' ;
22import { HashedObject } from '../model/HashedObject' ;
33import { RSAPublicKey } from './RSAPublicKey' ;
44import { Hashing } from 'data/model/Hashing' ;
@@ -17,57 +17,52 @@ class RSAKeyPair extends HashedObject {
1717 static className = 'hhs/v0/RSAKeyPair' ;
1818
1919 static async generate ( bits : number ) {
20- let rsa = new RSAImpl ( ) ;
21- rsa . generateKey ( bits ) ;
20+ let rsa = new RSADefaults . impl ( ) ;
21+ await rsa . generateKey ( bits ) ;
2222
23- return RSAKeyPair . fromKeys ( rsa . getFormat ( ) , rsa . getPublicKey ( ) , rsa . getPrivateKey ( ) ) ;
23+ return RSAKeyPair . fromKeys ( rsa . getPublicKey ( ) , rsa . getPrivateKey ( ) ) ;
2424 }
2525
26- static async fromKeys ( format : string , publicKey : string , privateKey : string ) {
26+ static async fromKeys ( publicKey : string , privateKey ? : string ) {
2727 let keyPair = new RSAKeyPair ( ) ;
28- keyPair . format = format ;
2928 keyPair . publicKey = publicKey ;
3029 keyPair . privateKey = privateKey ;
31- await keyPair . initRSA ( ) ;
30+ keyPair . init ( ) ;
3231 await keyPair . selfSign ( ) ;
3332 return keyPair ;
3433 }
3534
36- format ?: string ;
3735 publicKey ?: string ;
3836 privateKey ?: string ;
3937 privateKeySignature ?: string ;
4038
41- _rsa ?: RSA ;
39+ _rsaPromise ?: Promise < RSA > ;
4240
4341 constructor ( ) {
4442 super ( ) ;
4543 }
4644
47- async init ( ) {
48- this . initRSA ( ) ;
49- if ( ! this . checkSelfSignature ( ) ) {
50- throw new Error ( "Self signature check failed for private key" ) ;
51- }
45+ init ( ) {
46+ this . _rsaPromise = this . initRSA ( ) ;
5247 }
5348
5449 async validate ( ) {
55- await this . initRSA ( ) ;
5650 return this . checkSelfSignature ( ) ;
5751 }
5852
59- private async initRSA ( ) {
60- this . _rsa = new RSAImpl ( ) ;
61- this . _rsa . loadKeyPair ( this . getFormat ( ) , this . getPublicKey ( ) , this . getPrivateKey ( ) ) ;
53+ private async initRSA ( ) : Promise < RSA > {
54+ const _rsa = new RSADefaults . impl ( ) ;
55+ await _rsa . loadKeyPair ( this . getPublicKey ( ) , this . getPrivateKey ( ) ) ;
56+ return _rsa ;
6257 }
6358
6459 private async selfSign ( ) {
6560
66- if ( this . _rsa === undefined ) {
61+ if ( this . _rsaPromise === undefined ) {
6762 throw new Error ( 'Attempting to self sign keypair, but RSA has not been initialized.' ) ;
6863 }
6964
70- this . privateKeySignature = await this . _rsa . sign ( this . privateKey as string ) ;
65+ this . privateKeySignature = await ( await this . _rsaPromise ) . sign ( this . privateKey as string ) ;
7166 }
7267
7368 private checkSelfSignature ( ) {
@@ -79,11 +74,7 @@ class RSAKeyPair extends HashedObject {
7974 }
8075
8176 customHash ( seed ?: string ) {
82- return RSAKeyPair . hashPublicKeyPart ( this . format as string , this . publicKey as string , seed ) ;
83- }
84-
85- getFormat ( ) : string {
86- return this . format as string ;
77+ return RSAKeyPair . hashPublicKeyPart ( this . publicKey as string , seed ) ;
8778 }
8879
8980 getPublicKey ( ) {
@@ -95,42 +86,47 @@ class RSAKeyPair extends HashedObject {
9586 }
9687
9788 makePublicKey ( ) {
98- return RSAPublicKey . fromKeys ( this . getFormat ( ) , this . getPublicKey ( ) ) ;
89+ return RSAPublicKey . fromKeys ( this . getPublicKey ( ) ) ;
9990 }
10091
101- sign ( text : string ) {
92+ async sign ( text : string ) {
10293
103- if ( this . _rsa === undefined ) {
94+ if ( this . _rsaPromise === undefined ) {
10495 throw new Error ( 'Attempting to create signature, but RSA has not been initialized.' ) ;
10596 }
10697
107- return this . _rsa . sign ( text ) ;
98+ return ( await this . _rsaPromise ) . sign ( text ) ;
10899 }
109100
110- verifySignature ( text : string , signature : string ) {
111- return this . _rsa ?. verify ( text , signature ) ;
101+ async verifySignature ( text : string , signature : string ) {
102+
103+ if ( this . _rsaPromise === undefined ) {
104+ throw new Error ( 'Attempting to verify signature, but RSA has not been initialized.' ) ;
105+ }
106+
107+ return ( await this . _rsaPromise ) . verify ( text , signature ) ;
112108 }
113109
114- encrypt ( plainText : string ) {
110+ async encrypt ( plainText : string ) {
115111
116- if ( this . _rsa === undefined ) {
112+ if ( this . _rsaPromise === undefined ) {
117113 throw new Error ( 'Attempting to encrypt, but RSA has not been initialized.' ) ;
118114 }
119115
120- return this . _rsa . encrypt ( plainText ) ;
116+ return ( await this . _rsaPromise ) . encrypt ( plainText ) ;
121117 }
122118
123- decrypt ( cypherText : string ) {
119+ async decrypt ( cypherText : string ) {
124120
125- if ( this . _rsa === undefined ) {
121+ if ( this . _rsaPromise === undefined ) {
126122 throw new Error ( 'Attempting to decrypt, but RSA has not been initialized.' ) ;
127123 }
128124
129- return this . _rsa ? .decrypt ( cypherText ) ;
125+ return ( await this . _rsaPromise ) . decrypt ( cypherText ) ;
130126 }
131127
132- static hashPublicKeyPart ( format : string , publicKey : string , seed ?: string ) {
133- return Hashing . forValue ( { '_type' : 'custom_hashed_object' , '_class' : RSAKeyPair . className , '_contents' : { 'format' : format , ' publicKey' : publicKey } } , seed ) ;
128+ static hashPublicKeyPart ( publicKey : string , seed ?: string ) {
129+ return Hashing . forValue ( { '_type' : 'custom_hashed_object' , '_class' : RSAKeyPair . className , '_contents' : { 'publicKey' : publicKey } } , seed ) ;
134130 }
135131}
136132
0 commit comments