@@ -15,15 +15,16 @@ const serializers = require('./serializers')
1515const generateCEK = require ( './generate_cek' )
1616const validateHeaders = require ( './validate_headers' )
1717
18- const AAD = Symbol ( 'AAD' )
19- const CEK = Symbol ( 'CEK' )
20- const CLEARTEXT = Symbol ( 'CLEARTEXT' )
2118const PROCESS_RECIPIENT = Symbol ( 'PROCESS_RECIPIENT' )
22- const PROTECTED = Symbol ( 'PROTECTED' )
23- const RECIPIENTS = Symbol ( 'RECIPIENTS' )
24- const UNPROTECTED = Symbol ( 'UNPROTECTED' )
2519
2620class Encrypt {
21+ #aad
22+ #cek
23+ #unprotected
24+ #protected
25+ #cleartext
26+ #recipients
27+
2728 constructor ( cleartext , protectedHeader , unprotectedHeader , aad ) {
2829 if ( ! Buffer . isBuffer ( cleartext ) && typeof cleartext !== 'string' ) {
2930 throw new TypeError ( 'cleartext argument must be a Buffer or a string' )
@@ -43,13 +44,11 @@ class Encrypt {
4344 throw new TypeError ( 'unprotectedHeader argument must be a plain object when provided' )
4445 }
4546
46- Object . assign ( this , {
47- [ CLEARTEXT ] : cleartext ,
48- [ RECIPIENTS ] : [ ] ,
49- [ PROTECTED ] : protectedHeader ? deepClone ( protectedHeader ) : undefined ,
50- [ UNPROTECTED ] : unprotectedHeader ? deepClone ( unprotectedHeader ) : undefined ,
51- [ AAD ] : aad
52- } )
47+ this . #recipients = [ ]
48+ this . #cleartext = cleartext
49+ this . #aad = aad
50+ this . #unprotected = unprotectedHeader ? deepClone ( unprotectedHeader ) : undefined
51+ this . #protected = protectedHeader ? deepClone ( protectedHeader ) : undefined
5352 }
5453
5554 /*
@@ -64,7 +63,7 @@ class Encrypt {
6463 throw new TypeError ( 'header argument must be a plain object when provided' )
6564 }
6665
67- this [ RECIPIENTS ] . push ( {
66+ this . #recipients . push ( {
6867 key,
6968 header : header ? deepClone ( header ) : undefined
7069 } )
@@ -76,7 +75,9 @@ class Encrypt {
7675 * @private
7776 */
7877 [ PROCESS_RECIPIENT ] ( recipient ) {
79- const { [ PROTECTED ] : protectedHeader , [ UNPROTECTED ] : unprotectedHeader , [ RECIPIENTS ] : { length : recipientCount } } = this
78+ const unprotectedHeader = this . #unprotected
79+ const protectedHeader = this . #protected
80+ const { length : recipientCount } = this . #recipients
8081
8182 const jweHeader = {
8283 ...protectedHeader ,
@@ -107,7 +108,7 @@ class Encrypt {
107108 if ( protectedHeader ) {
108109 protectedHeader . alg = alg
109110 } else {
110- this [ PROTECTED ] = { alg }
111+ this . #protected = { alg }
111112 }
112113 } else {
113114 if ( recipient . header ) {
@@ -122,11 +123,11 @@ class Encrypt {
122123 let generatedHeader
123124
124125 if ( key . kty === 'oct' && alg === 'dir' ) {
125- this [ CEK ] = importKey ( key [ KEYOBJECT ] , { use : 'enc' , alg : enc } )
126+ this . #cek = importKey ( key [ KEYOBJECT ] , { use : 'enc' , alg : enc } )
126127 } else {
127- ( { wrapped, header : generatedHeader } = wrapKey ( alg , key , this [ CEK ] [ KEYOBJECT ] . export ( ) , { enc, alg } ) )
128+ ( { wrapped, header : generatedHeader } = wrapKey ( alg , key , this . #cek [ KEYOBJECT ] . export ( ) , { enc, alg } ) )
128129 if ( alg === 'ECDH-ES' ) {
129- this [ CEK ] = importKey ( createSecretKey ( wrapped ) , { use : 'enc' , alg : enc } )
130+ this . #cek = importKey ( createSecretKey ( wrapped ) , { use : 'enc' , alg : enc } )
130131 }
131132 }
132133
@@ -150,58 +151,58 @@ class Encrypt {
150151 throw new TypeError ( 'serialization must be one of "compact", "flattened", "general"' )
151152 }
152153
153- if ( ! this [ RECIPIENTS ] . length ) {
154+ if ( ! this . #recipients . length ) {
154155 throw new JWEInvalid ( 'missing recipients' )
155156 }
156157
157- serializer . validate ( this [ PROTECTED ] , this [ UNPROTECTED ] , this [ AAD ] , this [ RECIPIENTS ] )
158+ serializer . validate ( this . #protected , this . #unprotected , this . #aad , this . #recipients )
158159
159- let enc = validateHeaders ( this [ PROTECTED ] , this [ UNPROTECTED ] , this [ RECIPIENTS ] , false , this [ PROTECTED ] ? this [ PROTECTED ] . crit : undefined )
160+ let enc = validateHeaders ( this . #protected , this . #unprotected , this . #recipients , false , this . #protected ? this . #protected . crit : undefined )
160161 if ( ! enc ) {
161162 enc = 'A128CBC-HS256'
162- if ( this [ PROTECTED ] ) {
163- this [ PROTECTED ] . enc = enc
163+ if ( this . #protected ) {
164+ this . #protected . enc = enc
164165 } else {
165- this [ PROTECTED ] = { enc }
166+ this . #protected = { enc }
166167 }
167168 }
168169 const final = { }
169- this [ CEK ] = generateCEK ( enc )
170+ this . #cek = generateCEK ( enc )
170171
171- this [ RECIPIENTS ] . forEach ( this [ PROCESS_RECIPIENT ] . bind ( this ) )
172+ this . #recipients . forEach ( this [ PROCESS_RECIPIENT ] . bind ( this ) )
172173
173174 const iv = generateIV ( enc )
174175 final . iv = base64url . encodeBuffer ( iv )
175176
176- if ( this [ RECIPIENTS ] . length === 1 && this [ RECIPIENTS ] [ 0 ] . generatedHeader ) {
177- const [ { generatedHeader } ] = this [ RECIPIENTS ]
178- delete this [ RECIPIENTS ] [ 0 ] . generatedHeader
179- this [ PROTECTED ] = Object . assign ( { } , this [ PROTECTED ] , generatedHeader )
177+ if ( this . #recipients . length === 1 && this . #recipients [ 0 ] . generatedHeader ) {
178+ const [ { generatedHeader } ] = this . #recipients
179+ delete this . #recipients [ 0 ] . generatedHeader
180+ this . #protected = Object . assign ( { } , this . #protected , generatedHeader )
180181 }
181182
182- if ( this [ PROTECTED ] ) {
183- final . protected = base64url . JSON . encode ( this [ PROTECTED ] )
183+ if ( this . #protected ) {
184+ final . protected = base64url . JSON . encode ( this . #protected )
184185 }
185- final . unprotected = this [ UNPROTECTED ]
186+ final . unprotected = this . #unprotected
186187
187188 let aad
188- if ( this [ AAD ] ) {
189- final . aad = base64url . encode ( this [ AAD ] )
189+ if ( this . #aad ) {
190+ final . aad = base64url . encode ( this . #aad )
190191 aad = Buffer . concat ( [ Buffer . from ( final . protected || '' ) , Buffer . from ( '.' ) , Buffer . from ( final . aad ) ] )
191192 } else {
192193 aad = Buffer . from ( final . protected || '' )
193194 }
194195
195- let cleartext = this [ CLEARTEXT ]
196- if ( this [ PROTECTED ] && 'zip' in this [ PROTECTED ] ) {
196+ let cleartext = this . #cleartext
197+ if ( this . #protected && 'zip' in this . #protected ) {
197198 cleartext = deflateRawSync ( cleartext )
198199 }
199200
200- const { ciphertext, tag } = encrypt ( enc , this [ CEK ] , cleartext , { iv, aad } )
201+ const { ciphertext, tag } = encrypt ( enc , this . #cek , cleartext , { iv, aad } )
201202 final . tag = base64url . encodeBuffer ( tag )
202203 final . ciphertext = base64url . encodeBuffer ( ciphertext )
203204
204- return serializer ( final , this [ RECIPIENTS ] )
205+ return serializer ( final , this . #recipients )
205206 }
206207}
207208
0 commit comments