@@ -4,7 +4,7 @@ import Platform from '../../../../common/platform';
4
4
import crypto from 'crypto' ;
5
5
import ErrorInfo from '../../../../common/lib/types/errorinfo' ;
6
6
import * as API from '../../../../../ably' ;
7
- import { IGetCipherParams , CryptoTypes } from '../../../../common/types/ICrypto' ;
7
+ import ICrypto , { IGetCipherParams , CryptoTypes } from '../../../../common/types/ICrypto' ;
8
8
import ICipher from '../../../../common/types/ICipher' ;
9
9
import { Cipher as NodeCipher , CipherKey as NodeCipherKey } from 'crypto' ;
10
10
import { Bufferlike , Output as BufferUtilsOutput , WordArrayLike } from './bufferutils' ;
@@ -88,18 +88,6 @@ var Crypto = (function () {
88
88
return typeof bufferOrString == 'string' ? Buffer . from ( bufferOrString , 'binary' ) : bufferOrString ;
89
89
}
90
90
91
- interface _CipherParams extends API . Types . CipherParams {
92
- algorithm : string ;
93
- keyLength : number ;
94
- mode : string ;
95
- key : NodeCipherKey ;
96
- iv : unknown ;
97
- }
98
-
99
- interface _CipherParamsConstructor {
100
- new ( algorithm : string , keyLength : number , mode : string , key : NodeCipherKey ) : _CipherParams ;
101
- }
102
-
103
91
/**
104
92
* A class encapsulating the client-specifiable parameters for
105
93
* the cipher.
@@ -110,8 +98,14 @@ var Crypto = (function () {
110
98
* Clients may instance a CipherParams directly and populate it, or may
111
99
* query the implementation to obtain a default system CipherParams.
112
100
*/
113
- const CipherParams = function (
114
- this : _CipherParams ,
101
+ class CipherParams implements API . Types . CipherParams {
102
+ algorithm : string ;
103
+ keyLength : number ;
104
+ mode : string ;
105
+ key : NodeCipherKey ;
106
+ iv : unknown ;
107
+
108
+ constructor (
115
109
algorithm : string ,
116
110
keyLength : number ,
117
111
mode : string ,
@@ -122,7 +116,8 @@ var Crypto = (function () {
122
116
this . mode = mode ;
123
117
this . key = key ;
124
118
this . iv = null ;
125
- } as unknown as _CipherParamsConstructor ;
119
+ }
120
+ }
126
121
127
122
function isInstCipherParams (
128
123
params : API . Types . CipherParams | API . Types . CipherParamOptions
@@ -150,9 +145,9 @@ var Crypto = (function () {
150
145
* concatenated with the resulting raw ciphertext to construct the "ciphertext"
151
146
* data passed to the recipient.
152
147
*/
153
- function Crypto ( ) { }
148
+ class Crypto {
154
149
155
- Crypto . CipherParams = CipherParams ;
150
+ static CipherParams = CipherParams ;
156
151
157
152
/**
158
153
* Obtain a complete CipherParams instance from the provided params, filling
@@ -163,7 +158,7 @@ var Crypto = (function () {
163
158
* base64-encoded string. May optionally also contain: algorithm (defaults to
164
159
* AES), mode (defaults to 'cbc')
165
160
*/
166
- Crypto . getDefaultParams = function ( params : API . Types . CipherParamOptions ) {
161
+ static getDefaultParams ( params : API . Types . CipherParamOptions ) {
167
162
var key : Buffer ;
168
163
169
164
if ( ! params . key ) {
@@ -198,15 +193,15 @@ var Crypto = (function () {
198
193
199
194
validateCipherParams ( cipherParams ) ;
200
195
return cipherParams ;
201
- } ;
196
+ }
202
197
203
198
/**
204
199
* Generate a random encryption key from the supplied keylength (or the
205
200
* default keyLength if none supplied) as a Buffer
206
201
* @param keyLength (optional) the required keyLength in bits
207
202
* @param callback (optional) (err, key)
208
203
*/
209
- Crypto . generateRandomKey = function ( keyLength ?: number , callback ?: API . Types . StandardCallback < API . Types . CipherKey > ) {
204
+ static generateRandomKey ( keyLength ?: number , callback ?: API . Types . StandardCallback < API . Types . CipherKey > ) {
210
205
if ( arguments . length == 1 && typeof keyLength == 'function' ) {
211
206
callback = keyLength ;
212
207
keyLength = undefined ;
@@ -219,49 +214,43 @@ var Crypto = (function () {
219
214
generateRandom ( ( keyLength || DEFAULT_KEYLENGTH ) / 8 , ( err , buf ) => {
220
215
callback ! ( err ? ErrorInfo . fromValues ( err ) : null , buf ) ;
221
216
} ) ;
222
- } ;
217
+ }
223
218
224
219
/**
225
220
* Internal; get a ChannelCipher instance based on the given cipherParams
226
221
* @param params either a CipherParams instance or some subset of its
227
222
* fields that includes a key
228
223
*/
229
- Crypto . getCipher = function ( params : IGetCipherParams < IV > ) {
230
- var cipherParams = isInstCipherParams ( params ) ? ( params as _CipherParams ) : Crypto . getDefaultParams ( params ) ;
224
+ static getCipher ( params : IGetCipherParams < IV > ) {
225
+ var cipherParams = isInstCipherParams ( params ) ? ( params as CipherParams ) : Crypto . getDefaultParams ( params ) ;
231
226
232
227
var iv = params . iv || generateRandom ( DEFAULT_BLOCKLENGTH ) ;
233
228
return {
234
229
cipherParams : cipherParams ,
235
230
cipher : new CBCCipher ( cipherParams , iv ) ,
236
231
} ;
237
- } ;
232
+ }
238
233
239
- interface _CBCCipher extends ICipher < InputPlaintext , OutputCiphertext , InputCiphertext , OutputPlaintext > {
234
+ }
235
+
236
+ Crypto satisfies ICrypto < IV , InputPlaintext , OutputCiphertext , InputCiphertext , OutputPlaintext > ;
237
+
238
+ class CBCCipher implements ICipher < InputPlaintext , OutputCiphertext , InputCiphertext , OutputPlaintext > {
240
239
algorithm : string ;
241
240
key : NodeCipherKey ;
242
241
iv : Buffer | null ;
243
242
encryptCipher : NodeCipher ;
244
243
blockLength : number ;
245
- getIv : ( ) => Buffer ;
246
- }
247
244
248
- interface _CBCCipherConstructor {
249
- new ( params : _CipherParams , iv : Buffer ) : _CBCCipher ;
250
- }
251
-
252
- const CBCCipher = function CBCCipher ( this : _CBCCipher , params : _CipherParams , iv : Buffer ) {
245
+ constructor ( params : CipherParams , iv : Buffer ) {
253
246
this . algorithm = params . algorithm + '-' + String ( params . keyLength ) + '-' + params . mode ;
254
247
this . key = params . key ;
255
248
this . iv = iv ;
256
249
this . encryptCipher = crypto . createCipheriv ( this . algorithm , this . key , this . iv ) ;
257
250
this . blockLength = this . iv . length ;
258
- } as unknown as _CBCCipherConstructor ;
251
+ }
259
252
260
- CBCCipher . prototype . encrypt = function (
261
- this : _CBCCipher ,
262
- plaintext : InputPlaintext ,
263
- callback : ( error : Error | null , data : OutputCiphertext | null ) => void
264
- ) {
253
+ encrypt ( plaintext : InputPlaintext , callback : ( error : Error | null , data : OutputPlaintext | null ) => void ) {
265
254
Logger . logAction ( Logger . LOG_MICRO , 'CBCCipher.encrypt()' , '' ) ;
266
255
var plaintextBuffer = Platform . BufferUtils . toBuffer ( plaintext ) ;
267
256
var plaintextLength = plaintextBuffer . length ,
@@ -272,18 +261,18 @@ var Crypto = (function () {
272
261
) ;
273
262
var ciphertext = Buffer . concat ( [ iv , toBuffer ( cipherOut ) ] ) ;
274
263
return callback ( null , ciphertext ) ;
275
- } ;
264
+ }
276
265
277
- CBCCipher . prototype . decrypt = function ( this : _CBCCipher , ciphertext : InputCiphertext ) {
266
+ decrypt ( ciphertext : InputCiphertext ) {
278
267
var blockLength = this . blockLength ,
279
268
decryptCipher = crypto . createDecipheriv ( this . algorithm , this . key , ciphertext . slice ( 0 , blockLength ) ) ,
280
269
plaintext = toBuffer ( decryptCipher . update ( ciphertext . slice ( blockLength ) ) ) ,
281
270
final = decryptCipher . final ( ) ;
282
271
if ( final && final . length ) plaintext = Buffer . concat ( [ plaintext , toBuffer ( final ) ] ) ;
283
272
return plaintext ;
284
- } ;
273
+ }
285
274
286
- CBCCipher . prototype . getIv = function ( this : _CBCCipher ) {
275
+ getIv ( ) {
287
276
if ( this . iv ) {
288
277
var iv = this . iv ;
289
278
this . iv = null ;
@@ -295,7 +284,8 @@ var Crypto = (function () {
295
284
* sets a new iv (= aes(randomBlock XOR lastCipherText)) as well as
296
285
* returning it */
297
286
return toBuffer ( this . encryptCipher . update ( randomBlock ) ) ;
298
- } ;
287
+ }
288
+ }
299
289
300
290
return Crypto ;
301
291
} ) ( ) ;
0 commit comments