Skip to content

Commit 2e63577

Browse files
committed
fix: import issue on windows
1 parent efb7454 commit 2e63577

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

Diff for: test/crypto.test.ts

+87-8
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,99 @@
11
import * as semver from 'semver';
22
import * as process from 'node:process';
3+
import * as crypto from 'node:crypto';
34
import * as path from 'node:path';
45
import * as fs from 'node:fs';
56
import * as sinon from 'sinon';
67
import { EJSON, BSON, Binary } from 'bson';
78
import { MongoCrypt, MongoCryptConstructor } from '../src';
89
import { expect } from 'chai';
910

11+
function makeAES256Hook(method, mode) {
12+
return function (key, iv, input, output) {
13+
let result;
14+
try {
15+
const cipher = crypto[method](mode, key, iv);
16+
cipher.setAutoPadding(false);
17+
result = cipher.update(input);
18+
const final = cipher.final();
19+
if (final.length > 0) {
20+
result = Buffer.concat([result, final]);
21+
}
22+
} catch (e) {
23+
return e;
24+
}
25+
result.copy(output);
26+
return result.length;
27+
};
28+
}
29+
30+
function randomHook(buffer, count) {
31+
try {
32+
crypto.randomFillSync(buffer, 0, count);
33+
} catch (e) {
34+
return e;
35+
}
36+
return count;
37+
}
38+
39+
function sha256Hook(input, output) {
40+
let result;
41+
try {
42+
result = crypto.createHash('sha256').update(input).digest();
43+
} catch (e) {
44+
return e;
45+
}
46+
result.copy(output);
47+
return result.length;
48+
}
49+
50+
function makeHmacHook(algorithm) {
51+
return (key, input, output) => {
52+
let result;
53+
try {
54+
result = crypto.createHmac(algorithm, key).update(input).digest();
55+
} catch (e) {
56+
return e;
57+
}
58+
result.copy(output);
59+
return result.length;
60+
};
61+
}
62+
63+
function signRsaSha256Hook(key, input, output) {
64+
let result;
65+
try {
66+
const signer = crypto.createSign('sha256WithRSAEncryption');
67+
const privateKey = Buffer.from(
68+
`-----BEGIN PRIVATE KEY-----\n${key.toString('base64')}\n-----END PRIVATE KEY-----\n`
69+
);
70+
result = signer.update(input).end().sign(privateKey);
71+
} catch (e) {
72+
return e;
73+
}
74+
result.copy(output);
75+
return result.length;
76+
}
77+
78+
const aes256CbcEncryptHook = makeAES256Hook('createCipheriv', 'aes-256-cbc');
79+
const aes256CbcDecryptHook = makeAES256Hook('createDecipheriv', 'aes-256-cbc');
80+
const aes256CtrEncryptHook = makeAES256Hook('createCipheriv', 'aes-256-ctr');
81+
const aes256CtrDecryptHook = makeAES256Hook('createDecipheriv', 'aes-256-ctr');
82+
const hmacSha512Hook = makeHmacHook('sha512');
83+
const hmacSha256Hook = makeHmacHook('sha256');
84+
85+
export const cryptoCallbacks = {
86+
randomHook,
87+
sha256Hook,
88+
signRsaSha256Hook,
89+
aes256CbcEncryptHook,
90+
aes256CbcDecryptHook,
91+
aes256CtrEncryptHook,
92+
aes256CtrDecryptHook,
93+
hmacSha512Hook,
94+
hmacSha256Hook
95+
};
96+
1097
const NEED_MONGO_KEYS = 3;
1198
const READY = 5;
1299
const ERROR = 0;
@@ -58,14 +145,6 @@ function createEncryptedDocument(mongoCrypt: MongoCrypt) {
58145
}
59146

60147
describe('Crypto hooks', () => {
61-
let cryptoCallbacks;
62-
63-
before(async () => {
64-
const module = path.join(__dirname, 'benchmarks', 'crypto_callbacks.mjs');
65-
const { cryptoCallbacks: c } = await eval(`import(${JSON.stringify(module)})`);
66-
cryptoCallbacks = c;
67-
});
68-
69148
describe('when openssl 3 available', () => {
70149
beforeEach('check ssl version', function () {
71150
const openssl = semver.coerce(process.versions.openssl);

0 commit comments

Comments
 (0)