Apply formatting changes

This commit is contained in:
cirospaciari
2024-09-09 18:26:44 +00:00
committed by github-actions[bot]
parent 8531376b86
commit 68ba3671ae
3 changed files with 57 additions and 52 deletions

View File

@@ -1,7 +1,7 @@
import { bench, run } from "mitata";
const crypto = require("node:crypto");
const keyPair = crypto.generateKeyPairSync('rsa', {
const keyPair = crypto.generateKeyPairSync("rsa", {
modulusLength: 2048,
publicKeyEncoding: {
type: "spki",

View File

@@ -12186,8 +12186,7 @@ function toCryptoKey(key, asPublic) {
function doAsymmetricCipher(key, message, operation, isEncrypt) {
// Our crypto bindings expect the key to be a `JSCryptoKey` property within an object.
const cryptoKey = toCryptoKey(key, isEncrypt);
const oaepLabel =
typeof key.oaepLabel === "string" ? Buffer.from(key.oaepLabel, key.encoding) : key.oaepLabel;
const oaepLabel = typeof key.oaepLabel === "string" ? Buffer.from(key.oaepLabel, key.encoding) : key.oaepLabel;
const keyObject = {
key: cryptoKey,
oaepHash: key.oaepHash,
@@ -12198,13 +12197,13 @@ function doAsymmetricCipher(key, message, operation, isEncrypt) {
return operation(keyObject, buffer);
}
crypto_exports.publicEncrypt = function(key, message) {
crypto_exports.publicEncrypt = function (key, message) {
return doAsymmetricCipher(key, message, publicEncrypt, true);
}
};
crypto_exports.privateDecrypt = function(key, message) {
crypto_exports.privateDecrypt = function (key, message) {
return doAsymmetricCipher(key, message, privateDecrypt, false);
}
};
__export(crypto_exports, {
DEFAULT_ENCODING: () => DEFAULT_ENCODING,

View File

@@ -244,39 +244,18 @@ function test_rsa(padding, encryptOaepHash, decryptOaepHash, exceptionThrown) {
padding = constants[padding];
const encryptedBuffer = crypto.publicEncrypt(
{
key: rsaPubPem,
padding: padding,
oaepHash: encryptOaepHash,
},
bufferToEncrypt,
);
const encryptedBuffer = crypto.publicEncrypt(
{
key: rsaPubPem,
padding: padding,
oaepHash: encryptOaepHash,
},
bufferToEncrypt,
);
if (padding === constants.RSA_PKCS1_PADDING) {
expect(() => {
crypto.privateDecrypt(
{
key: rsaKeyPem,
padding: padding,
oaepHash: decryptOaepHash,
},
encryptedBuffer,
);
}).toThrow(expect.objectContaining({ code: "ERR_INVALID_ARG_VALUE" }));
expect(() => {
crypto.privateDecrypt(
{
key: rsaPkcs8KeyPem,
padding: padding,
oaepHash: decryptOaepHash,
},
encryptedBuffer,
);
}).toThrow(expect.objectContaining({ code: "ERR_INVALID_ARG_VALUE" }));
} else {
const decryptedBuffer = crypto.privateDecrypt(
if (padding === constants.RSA_PKCS1_PADDING) {
expect(() => {
crypto.privateDecrypt(
{
key: rsaKeyPem,
padding: padding,
@@ -284,9 +263,10 @@ function test_rsa(padding, encryptOaepHash, decryptOaepHash, exceptionThrown) {
},
encryptedBuffer,
);
expect(decryptedBuffer).toEqual(input);
}).toThrow(expect.objectContaining({ code: "ERR_INVALID_ARG_VALUE" }));
const decryptedBufferPkcs8 = crypto.privateDecrypt(
expect(() => {
crypto.privateDecrypt(
{
key: rsaPkcs8KeyPem,
padding: padding,
@@ -294,8 +274,28 @@ function test_rsa(padding, encryptOaepHash, decryptOaepHash, exceptionThrown) {
},
encryptedBuffer,
);
expect(decryptedBufferPkcs8).toEqual(input);
}
}).toThrow(expect.objectContaining({ code: "ERR_INVALID_ARG_VALUE" }));
} else {
const decryptedBuffer = crypto.privateDecrypt(
{
key: rsaKeyPem,
padding: padding,
oaepHash: decryptOaepHash,
},
encryptedBuffer,
);
expect(decryptedBuffer).toEqual(input);
const decryptedBufferPkcs8 = crypto.privateDecrypt(
{
key: rsaPkcs8KeyPem,
padding: padding,
oaepHash: decryptOaepHash,
},
encryptedBuffer,
);
expect(decryptedBufferPkcs8).toEqual(input);
}
}
test(`RSA with RSA_NO_PADDING`, () => {
@@ -367,9 +367,11 @@ describe("Invalid oaepHash and oaepLabel options", () => {
},
Buffer.alloc(10),
);
}).toThrow(expect.objectContaining({
code: "ERR_CRYPTO_INVALID_DIGEST",
}));
}).toThrow(
expect.objectContaining({
code: "ERR_CRYPTO_INVALID_DIGEST",
}),
);
[0, false, null, Symbol(), () => {}].forEach(oaepHash => {
expect(() => {
@@ -380,9 +382,11 @@ describe("Invalid oaepHash and oaepLabel options", () => {
},
Buffer.alloc(10),
);
}).toThrow(expect.objectContaining({
code: "ERR_INVALID_ARG_TYPE",
}));
}).toThrow(
expect.objectContaining({
code: "ERR_INVALID_ARG_TYPE",
}),
);
});
});
@@ -396,9 +400,11 @@ describe("Invalid oaepHash and oaepLabel options", () => {
},
Buffer.alloc(10),
);
}).toThrow(expect.objectContaining({
code: "ERR_INVALID_ARG_TYPE",
}));
}).toThrow(
expect.objectContaining({
code: "ERR_INVALID_ARG_TYPE",
}),
);
});
});
});