4
4
* @license Apache-2.0
5
5
*/
6
6
7
+ import OperationError from "../errors/OperationError.mjs" ;
7
8
import Operation from "../Operation.mjs" ;
8
9
9
10
import { SM2 } from "../lib/SM2.mjs" ;
@@ -20,7 +21,7 @@ class SM2Encrypt extends Operation {
20
21
super ( ) ;
21
22
22
23
this . name = "SM2 Encrypt" ;
23
- this . module = "Ciphers " ;
24
+ this . module = "Crypto " ;
24
25
this . description = "Encrypts a message utilizing the SM2 standard" ;
25
26
this . infoURL = "" ; // Usually a Wikipedia link. Remember to remove localisation (i.e. https://wikipedia.org/etc rather than https://en.wikipedia.org/etc)
26
27
this . inputType = "ArrayBuffer" ;
@@ -61,33 +62,16 @@ class SM2Encrypt extends Operation {
61
62
const [ publicKeyX , publicKeyY , outputFormat , curveName ] = args ;
62
63
this . outputFormat = outputFormat ;
63
64
65
+ if ( publicKeyX . length !== 64 || publicKeyY . length !== 64 ) {
66
+ throw new OperationError ( "Invalid Public Key - Ensure each component is 32 bytes in size and in hex" ) ;
67
+ }
68
+
64
69
const sm2 = new SM2 ( curveName , outputFormat ) ;
65
70
sm2 . setPublicKey ( publicKeyX , publicKeyY ) ;
66
71
67
72
const result = sm2 . encrypt ( new Uint8Array ( input ) ) ;
68
73
return result ;
69
74
}
70
-
71
- /**
72
- * Highlight SM2 Encrypt
73
- *
74
- * @param {Object[] } pos
75
- * @param {number } pos[].start
76
- * @param {number } pos[].end
77
- * @param {Object[] } args
78
- * @returns {Object[] } pos
79
- */
80
- highlight ( pos , args ) {
81
- const outputFormat = args [ 2 ] ;
82
- const num = pos [ 0 ] . end - pos [ 0 ] . start ;
83
- let adjust = 128 ;
84
- if ( outputFormat === "C1C3C2" ) {
85
- adjust = 192 ;
86
- }
87
- pos [ 0 ] . start = Math . ceil ( pos [ 0 ] . start + adjust ) ;
88
- pos [ 0 ] . end = Math . floor ( pos [ 0 ] . end + adjust + num ) ;
89
- return pos ;
90
- }
91
75
}
92
76
93
77
export default SM2Encrypt ;
0 commit comments