Skip to content

Commit 2bee978

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 5ca4d03 commit 2bee978

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ var Crypto = (function () {
112112
* concatenated with the resulting raw ciphertext to construct the "ciphertext"
113113
* data passed to the recipient.
114114
*/
115-
function Crypto() {}
115+
class Crypto {
116116

117117
/**
118118
* Obtain a complete CipherParams instance from the provided params, filling
@@ -123,7 +123,7 @@ var Crypto = (function () {
123123
* base64-encoded string. May optionally also contain: algorithm (defaults to
124124
* AES), mode (defaults to 'cbc')
125125
*/
126-
Crypto.getDefaultParams = function (params: API.Types.CipherParamOptions) {
126+
static getDefaultParams(params: API.Types.CipherParamOptions) {
127127
var key;
128128

129129
if (!params.key) {
@@ -154,15 +154,15 @@ var Crypto = (function () {
154154

155155
validateCipherParams(cipherParams);
156156
return cipherParams;
157-
};
157+
}
158158

159159
/**
160160
* Generate a random encryption key from the supplied keylength (or the
161161
* default keyLength if none supplied) as a Buffer
162162
* @param keyLength (optional) the required keyLength in bits
163163
* @param callback (optional) (err, key)
164164
*/
165-
Crypto.generateRandomKey = function (keyLength?: number, callback?: API.Types.StandardCallback<API.Types.CipherKey>) {
165+
static generateRandomKey(keyLength?: number, callback?: API.Types.StandardCallback<API.Types.CipherKey>) {
166166
if (arguments.length == 1 && typeof keyLength == 'function') {
167167
callback = keyLength;
168168
keyLength = undefined;
@@ -175,22 +175,27 @@ var Crypto = (function () {
175175
generateRandom((keyLength || DEFAULT_KEYLENGTH) / 8, (err, buf) => {
176176
callback!(err ? ErrorInfo.fromValues(err) : null, buf);
177177
});
178-
};
178+
}
179179

180180
/**
181181
* Internal; get a ChannelCipher instance based on the given cipherParams
182182
* @param params either a CipherParams instance or some subset of its
183183
* fields that includes a key
184184
*/
185-
Crypto.getCipher = function (params: IGetCipherParams<IV>) {
185+
static getCipher(params: IGetCipherParams<IV>) {
186186
var cipherParams = isInstCipherParams(params) ? params : Crypto.getDefaultParams(params);
187187

188188
var iv = params.iv || generateRandom(DEFAULT_BLOCKLENGTH);
189189
return {
190190
cipherParams: cipherParams /* CipherOptions.cipher */,
191191
cipher: new CBCCipher(cipherParams, iv) /* CipherOptions.channelCipher */,
192192
};
193-
};
193+
}
194+
195+
}
196+
197+
// TODO sort out
198+
// Crypto satisfies ICrypto<IV, InputPlaintext, OutputCiphertext, InputCiphertext, OutputPlaintext>;
194199

195200
const CBCCipher = function CBCCipher(
196201
this: { algorithm: string; key: NodeCipherKey; iv: IV | null; encryptCipher: NodeCipher; blockLength: number },

0 commit comments

Comments
 (0)