Skip to content

Commit fcec301

Browse files
wip convert Node Crypto to class
haven't formatted this — will do once contents of class are correct Use of `satisfies` comes from suggestion in [1]. [1] "Allow specifying interface implements clauses for the static side of classes": microsoft/TypeScript#33892
1 parent c32b106 commit fcec301

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/platform/nodejs/lib/util/crypto.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ var Crypto = (function () {
105105
* concatenated with the resulting raw ciphertext to construct the "ciphertext"
106106
* data passed to the recipient.
107107
*/
108-
function Crypto() {}
108+
class Crypto {
109109

110110
/**
111111
* Obtain a complete CipherParams instance from the provided params, filling
@@ -116,7 +116,7 @@ var Crypto = (function () {
116116
* base64-encoded string. May optionally also contain: algorithm (defaults to
117117
* AES), mode (defaults to 'cbc')
118118
*/
119-
Crypto.getDefaultParams = function (params: API.Types.CipherParamOptions) {
119+
static getDefaultParams(params: API.Types.CipherParamOptions) {
120120
var key;
121121

122122
if (!params.key) {
@@ -147,15 +147,15 @@ var Crypto = (function () {
147147

148148
validateCipherParams(cipherParams);
149149
return cipherParams;
150-
};
150+
}
151151

152152
/**
153153
* Generate a random encryption key from the supplied keylength (or the
154154
* default keyLength if none supplied) as a Buffer
155155
* @param keyLength (optional) the required keyLength in bits
156156
* @param callback (optional) (err, key)
157157
*/
158-
Crypto.generateRandomKey = function (keyLength?: number, callback?: API.Types.StandardCallback<API.Types.CipherKey>) {
158+
static generateRandomKey(keyLength?: number, callback?: API.Types.StandardCallback<API.Types.CipherKey>) {
159159
if (arguments.length == 1 && typeof keyLength == 'function') {
160160
callback = keyLength;
161161
keyLength = undefined;
@@ -168,14 +168,14 @@ var Crypto = (function () {
168168
generateRandom((keyLength || DEFAULT_KEYLENGTH) / 8, (err, buf) => {
169169
callback!(err ? ErrorInfo.fromValues(err) : null, buf);
170170
});
171-
};
171+
}
172172

173173
/**
174174
* Internal; get a ChannelCipher instance based on the given cipherParams
175175
* @param params either a CipherParams instance or some subset of its
176176
* fields that includes a key
177177
*/
178-
Crypto.getCipher = function (
178+
static getCipher(
179179
params: IGetCipherParams
180180
) {
181181
var cipherParams = isInstCipherParams(params) ? params : Crypto.getDefaultParams(params);
@@ -185,7 +185,11 @@ var Crypto = (function () {
185185
cipherParams: cipherParams /* CipherOptions.cipher */,
186186
cipher: new CBCCipher(cipherParams, iv) /* CipherOptions.channelCipher */,
187187
};
188-
};
188+
}
189+
190+
}
191+
192+
Crypto satisfies ICrypto;
189193

190194
const CBCCipher = function CBCCipher(this: { algorithm: string; key: NodeCipherKey; iv: Buffer | null; encryptCipher: NodeCipher; blockLength: number; }, params: API.Types.CipherParams, iv: Buffer | BrowserBufferlike) {
191195
this.algorithm = params.algorithm + '-' + String(params.keyLength) + '-' + params.mode;

0 commit comments

Comments
 (0)