@@ -8,6 +8,25 @@ const testConfigHeader = require("./mock/config-header");
88
99const iv = "6f38f3ecd8b92c2fd2537a7235deb9a8" ;
1010const secretKey = "bab78b5ec588274a4dd2a60834efcf60" ;
11+ const encryptionCertificateText = '-----BEGIN CERTIFICATE-----' +
12+ 'MIIDITCCAgmgAwIBAgIJANLIazc8xI4iMA0GCSqGSIb3DQEBBQUAMCcxJTAjBgNV' +
13+ 'BAMMHHd3dy5qZWFuLWFsZXhpcy1hdWZhdXZyZS5jb20wHhcNMTkwMjIxMDg1MTM1' +
14+ 'WhcNMjkwMjE4MDg1MTM1WjAnMSUwIwYDVQQDDBx3d3cuamVhbi1hbGV4aXMtYXVm' +
15+ 'YXV2cmUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9Mp6gEFp' +
16+ '9E+/1SS5XrUyYKMbE7eU0dyJCfmJPz8YOkOYV7ohqwXQvjlaP/YazZ6bbmYfa2WC' +
17+ 'raOpW0o2BYijHgQ7z2a2Az87rKdAtCpZSKFW82Ijnsw++lx7EABI3tFF282ZV7LT' +
18+ '13n9m4th5Kldukk9euy+TuJqCvPu4xzE/NE+l4LFMr8rfD47EPQkrun5w/TXwkmJ' +
19+ 'rdnG9ejl3BLQO06Ns6Bs516geiYZ7RYxtI8Xnu0ZC0fpqDqjCPZBTORkiFeLocEP' +
20+ 'RbTgo1H+0xQFNdsMH1/0F1BI+hvdxlbc3+kHZFZFoeBMkR3jC8jDXOXNCMNWb13T' +
21+ 'in6HqPReO0KW8wIDAQABo1AwTjAdBgNVHQ4EFgQUDtqNZacrC6wR53kCpw/BfG2C' +
22+ 't3AwHwYDVR0jBBgwFoAUDtqNZacrC6wR53kCpw/BfG2Ct3AwDAYDVR0TBAUwAwEB' +
23+ '/zANBgkqhkiG9w0BAQUFAAOCAQEAJ09tz2BDzSgNOArYtF4lgRtjViKpV7gHVqtc' +
24+ '3xQT9ujbaxEgaZFPbf7/zYfWZfJggX9T54NTGqo5AXM0l/fz9AZ0bOm03rnF2I/F' +
25+ '/ewhSlHYzvKiPM+YaswaRo1M1UPPgKpLlRDMO0u5LYiU5ICgCNm13TWgjBlzLpP6' +
26+ 'U4z2iBNq/RWBgYxypi/8NMYZ1RcCrAVSt3QnW6Gp+vW/HrE7KIlAp1gFdme3Xcx1' +
27+ 'vDRpA+MeeEyrnc4UNIqT/4bHGkKlIMKdcjZgrFfEJVFav3eJ4CZ7ZSV6Bx+9yRCL' +
28+ 'DPGlRJLISxgwsOTuUmLOxjotRxO8TdR5e1V+skEtfEctMuSVYA==' +
29+ '-----END CERTIFICATE-----'
1130
1231describe ( "Field Level Crypto" , ( ) => {
1332 describe ( "#new Crypto" , ( ) => {
@@ -169,6 +188,33 @@ describe("Field Level Crypto", () => {
169188 / C o n f i g n o t v a l i d : f o u n d m u l t i p l e c o n f i g u r a t i o n s e n c r y p t \/ d e c r y p t w i t h r o o t m a p p i n g /
170189 ) ;
171190 } ) ;
191+
192+ it ( "With useCertificateContent enabled, without valid encryption certificate content" , ( ) => {
193+ const config = JSON . parse ( JSON . stringify ( testConfig ) ) ;
194+ config . useCertificateContent = true ;
195+ assert . throws (
196+ ( ) => new Crypto ( config ) ,
197+ / I n v a l i d P E M f o r m a t t e d m e s s a g e ./
198+ ) ;
199+ } ) ;
200+
201+ it ( "With useCertificateContent enabled, with valid encryption certificate content and without private key" , ( ) => {
202+ const config = JSON . parse ( JSON . stringify ( testConfig ) ) ;
203+ config . useCertificateContent = true ;
204+ config . encryptionCertificate = encryptionCertificateText ;
205+ delete config [ "privateKey" ] ;
206+ assert . doesNotThrow ( ( ) => new Crypto ( config ) ) ;
207+ } ) ;
208+
209+ it ( "With useCertificateContent enabled, without encryptionCertificate" , ( ) => {
210+ const config = JSON . parse ( JSON . stringify ( testConfig ) ) ;
211+ config . useCertificateContent = true ;
212+ config . encryptionCertificate = null ;
213+ assert . throws (
214+ ( ) => new Crypto ( config ) ,
215+ / C o n f i g n o t v a l i d : p l e a s e c h e c k t h a t a l l t h e p r o p e r t i e s a r e d e f i n e d /
216+ ) ;
217+ } ) ;
172218 } ) ;
173219
174220 describe ( "#encryptData()" , ( ) => {
@@ -238,6 +284,27 @@ describe("Field Level Crypto", () => {
238284 assert . ok ( ! resp . iv ) ;
239285 assert . ok ( resp . oaepHashingAlgorithm ) ;
240286 } ) ;
287+
288+ it ( "with useCertificateContent" , ( ) => {
289+ const config = JSON . parse ( JSON . stringify ( testConfig ) ) ;
290+ config . ivFieldName = 'IvCustomName' ;
291+ config . useCertificateContent = true ;
292+ config . encryptionCertificate = encryptionCertificateText ;
293+ delete config [ "privateKey" ] ;
294+ const crypto = new Crypto ( config ) ;
295+ const data = JSON . stringify ( { text : "message with custom ivFieldName" } ) ;
296+ const resp = crypto . encryptData ( {
297+ data : data ,
298+ } ) ;
299+ assert . ok ( resp ) ;
300+ assert . ok ( resp . encryptedKey ) ;
301+ assert . ok ( resp . encryptedData ) ;
302+ assert . ok ( resp . IvCustomName ) ;
303+ assert . ok ( ! resp . iv ) ;
304+ assert . ok ( resp . oaepHashingAlgorithm ) ;
305+ } ) ;
306+
307+
241308 } ) ;
242309
243310
0 commit comments