Skip to content

Commit

Permalink
Remove highlighting and correct one module mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
flakjacket95 committed Sep 22, 2024
1 parent f61bdf0 commit ae9054d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/core/operations/SM2Decrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license Apache-2.0
*/

import OperationError from "../errors/OperationError.mjs";
import Operation from "../Operation.mjs";

import { SM2 } from "../lib/SM2.mjs";
Expand Down Expand Up @@ -54,6 +55,10 @@ class SM2Decrypt extends Operation {
run(input, args) {
const [privateKey, inputFormat, curveName] = args;

if (privateKey.length !== 64) {
throw new OperationError("Input private key must be in hex; and should be 32 bytes");
}

const sm2 = new SM2(curveName, inputFormat);
sm2.setPrivateKey(privateKey);

Expand Down
28 changes: 6 additions & 22 deletions src/core/operations/SM2Encrypt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* @license Apache-2.0
*/

import OperationError from "../errors/OperationError.mjs";
import Operation from "../Operation.mjs";

import { SM2 } from "../lib/SM2.mjs";
Expand All @@ -20,7 +21,7 @@ class SM2Encrypt extends Operation {
super();

this.name = "SM2 Encrypt";
this.module = "Ciphers";
this.module = "Crypto";
this.description = "Encrypts a message utilizing the SM2 standard";
this.infoURL = ""; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
this.inputType = "ArrayBuffer";
Expand Down Expand Up @@ -61,33 +62,16 @@ class SM2Encrypt extends Operation {
const [publicKeyX, publicKeyY, outputFormat, curveName] = args;
this.outputFormat = outputFormat;

if (publicKeyX.length !== 64 || publicKeyY.length !== 64) {
throw new OperationError("Invalid Public Key - Ensure each component is 32 bytes in size and in hex");
}

const sm2 = new SM2(curveName, outputFormat);
sm2.setPublicKey(publicKeyX, publicKeyY);

const result = sm2.encrypt(new Uint8Array(input));
return result;
}

/**
* Highlight SM2 Encrypt
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
const outputFormat = args[2];
const num = pos[0].end - pos[0].start;
let adjust = 128;
if (outputFormat === "C1C3C2") {
adjust = 192;
}
pos[0].start = Math.ceil(pos[0].start + adjust);
pos[0].end = Math.floor(pos[0].end + adjust + num);
return pos;
}
}

export default SM2Encrypt;

0 comments on commit ae9054d

Please sign in to comment.