Skip to content

Commit fad163e

Browse files
committed
Added tests (that can't be run)
1 parent 7ad3992 commit fad163e

File tree

7 files changed

+387
-29
lines changed

7 files changed

+387
-29
lines changed

src/core/config/Categories.json

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
"Scrypt",
108108
"Generate RSA Key Pair",
109109
"RSA Sign",
110+
"RSA Verify",
111+
"RSA Encrypt",
112+
"RSA Decrypt",
110113
"JWT Sign",
111114
"JWT Verify",
112115
"JWT Decode",

src/core/operations/RSADecrypt.mjs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import Operation from "../Operation.mjs";
88
import OperationError from "../errors/OperationError.mjs";
9+
import Utils from "../Utils.mjs";
910
import forge from "node-forge/dist/forge.min.js";
1011
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
1112

@@ -24,7 +25,7 @@ class RSADecrypt extends Operation {
2425
this.module = "Ciphers";
2526
this.description = "Decrypt an RSA encrypted message with a PEM encoded private key.";
2627
this.infoURL = "https://wikipedia.org/wiki/RSA_(cryptosystem)";
27-
this.inputType = "string";
28+
this.inputType = "ArrayBuffer";
2829
this.outputType = "string";
2930
this.args = [
3031
{
@@ -74,7 +75,7 @@ class RSADecrypt extends Operation {
7475
}
7576
try {
7677
const privKey = forge.pki.decryptRsaPrivateKey(pemKey, password);
77-
const dMsg = privKey.decrypt(input, scheme, {md: MD_ALGORITHMS[md].create()});
78+
const dMsg = privKey.decrypt(Utils.arrayBufferToStr(input), scheme, {md: MD_ALGORITHMS[md].create()});
7879
return dMsg;
7980
} catch (err) {
8081
throw new OperationError(err);

src/core/operations/RSAEncrypt.mjs

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import Operation from "../Operation.mjs";
88
import OperationError from "../errors/OperationError.mjs";
9+
import Utils from "../Utils.mjs";
910
import forge from "node-forge/dist/forge.min.js";
1011
import { MD_ALGORITHMS } from "../lib/RSA.mjs";
1112

@@ -25,7 +26,7 @@ class RSAEncrypt extends Operation {
2526
this.description = "Encrypt a message with a PEM encoded RSA public key.";
2627
this.infoURL = "https://wikipedia.org/wiki/RSA_(cryptosystem)";
2728
this.inputType = "string";
28-
this.outputType = "string";
29+
this.outputType = "ArrayBuffer";
2930
this.args = [
3031
{
3132
name: "RSA Public Key (PEM)",
@@ -73,8 +74,11 @@ class RSAEncrypt extends Operation {
7374
const pubKey = forge.pki.publicKeyFromPem(pemKey);
7475
// Encrypt message
7576
const eMsg = pubKey.encrypt(input, scheme, {md: MD_ALGORITHMS[md].create()});
76-
return eMsg;
77+
return Utils.strToArrayBuffer(eMsg);
7778
} catch (err) {
79+
if (err.message === "RSAES-OAEP input message length is too long.") {
80+
throw new OperationError(`RSAES-OAEP input message length (${err.length}) is longer than the maximum allowed length (${err.maxLength}).`);
81+
}
7882
throw new OperationError(err);
7983
}
8084
}

tests/operations/index.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ import "./tests/ParseObjectIDTimestamp.mjs";
105105

106106
// Cannot test operations that use the File type yet
107107
// import "./tests/SplitColourChannels.mjs";
108-
108+
// Cannot test as minified forge does not support node
109+
// import "./tests/RSA.mjs";
109110
const testStatus = {
110111
allTestsPassing: true,
111112
counts: {

tests/operations/samples/Ciphers.mjs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const ASCII_TEXT = "A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.";
2+
3+
export const UTF8_TEXT = "Шанцы на высвятленне таго, што адбываецца на самай справе ў сусвеце настолькі выдаленыя, адзінае, што трэба зрабіць, гэта павесіць пачуццё яго і трымаць сябе занятымі.";
4+
5+
export const ALL_BYTES = [
6+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
7+
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
8+
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f",
9+
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f",
10+
"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
11+
"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f",
12+
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f",
13+
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f",
14+
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f",
15+
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f",
16+
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
17+
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf",
18+
"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf",
19+
"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf",
20+
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef",
21+
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
22+
].join("");

tests/operations/tests/PGP.mjs

+1-24
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,7 @@
66
* @license Apache-2.0
77
*/
88
import TestRegister from "../../lib/TestRegister.mjs";
9-
10-
const ASCII_TEXT = "A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.";
11-
12-
const UTF8_TEXT = "Шанцы на высвятленне таго, што адбываецца на самай справе ў сусвеце настолькі выдаленыя, адзінае, што трэба зрабіць, гэта павесіць пачуццё яго і трымаць сябе занятымі.";
13-
14-
const ALL_BYTES = [
15-
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f",
16-
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f",
17-
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f",
18-
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f",
19-
"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f",
20-
"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f",
21-
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f",
22-
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f",
23-
"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f",
24-
"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f",
25-
"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf",
26-
"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf",
27-
"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf",
28-
"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf",
29-
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef",
30-
"\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff",
31-
].join("");
32-
9+
import {ASCII_TEXT, UTF8_TEXT, ALL_BYTES} from "../samples/Ciphers.mjs";
3310

3411
// RSA-1024
3512
const ALICE_PRIVATE = `-----BEGIN PGP PRIVATE KEY BLOCK-----

0 commit comments

Comments
 (0)