js_lis 1.0.14 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/VirtualKeyboard.js +38 -67
  2. package/package.json +1 -1
@@ -12,77 +12,47 @@ export class VirtualKeyboard {
12
12
  this.offsetY = 0;
13
13
  this.shiftActive = false;
14
14
  this.capsLockActive = false;
15
- this.secretKey = '1234567890abcdef'; // คีย์ที่ใช้ในการเข้ารหัส
16
- // ใช้ Web Crypto API สำหรับการจัดการคีย์
17
- this.cryptoKey = null;
18
- this.iv = window.crypto.getRandomValues(new Uint8Array(12)); // สร้าง IV สำหรับการเข้ารหัส/ถอดรหัส
19
-
20
- this.initialize();
21
- }
22
15
 
23
- async initialize() {
24
- try {
25
- await this.generateKey();
26
- this.render();
27
- this.initializeInputListeners();
28
- console.log("VirtualKeyboard initialized successfully.");
29
- } catch (error) {
30
- console.error("Error initializing VirtualKeyboard:", error);
31
- }
32
- }
33
-
34
- async generateKey() {
35
- try {
36
- this.cryptoKey = await window.crypto.subtle.generateKey(
37
- { name: "AES-GCM", length: 256 },
38
- true,
39
- ["encrypt", "decrypt"]
40
- );
41
- } catch (error) {
42
- console.error("Error generating key:", error);
43
- throw error;
44
- // rethrow the error to be caught in initialize
45
- }
46
- }
16
+ // กำหนดคีย์และ IV สำหรับการเข้ารหัส
17
+ this.secretKey = '1234567890abcdef1234567890abcdef'; // คีย์ความยาว 32 bytes
18
+ this.iv = CryptoJS.lib.WordArray.random(16); // สร้าง IV ความยาว 16 bytes
47
19
 
48
- async encodeText(text) {
49
- const encodedData = new TextEncoder().encode(text);
50
- const encryptedData = await window.crypto.subtle.encrypt(
51
- { name: "AES-GCM", iv: this.iv },
52
- this.cryptoKey,
53
- encodedData
54
- );
55
- return this.arrayBufferToBase64(encryptedData); // แปลงเป็น Base64
20
+ this.initialize();
56
21
  }
57
22
 
58
- async decodeText(base64Text) {
59
- const encryptedData = this.base64ToArrayBuffer(base64Text);
60
- const decryptedData = await window.crypto.subtle.decrypt(
61
- { name: "AES-GCM", iv: this.iv },
62
- this.cryptoKey,
63
- encryptedData
64
- );
65
- return new TextDecoder().decode(decryptedData);
23
+ async initialize() {
24
+ try {
25
+ this.render();
26
+ this.initializeInputListeners();
27
+ console.log("VirtualKeyboard initialized successfully.");
28
+ } catch (error) {
29
+ console.error("Error initializing VirtualKeyboard:", error);
30
+ }
66
31
  }
67
32
 
68
- arrayBufferToBase64(buffer) {
69
- let binary = '';
70
- const bytes = new Uint8Array(buffer);
71
- const len = bytes.byteLength;
72
- for (let i = 0; i < len; i++) {
73
- binary += String.fromCharCode(bytes[i]);
74
- }
75
- return window.btoa(binary);
33
+ encodeText(text) {
34
+ const encrypted = CryptoJS.AES.encrypt(text, CryptoJS.enc.Hex.parse(this.secretKey), {
35
+ iv: this.iv,
36
+ mode: CryptoJS.mode.GCM,
37
+ format: CryptoJS.format.Hex
38
+ });
39
+ return {
40
+ encrypted: encrypted.ciphertext.toString(CryptoJS.enc.Base64),
41
+ iv: this.iv.toString(CryptoJS.enc.Base64)
42
+ };
76
43
  }
77
44
 
78
- base64ToArrayBuffer(base64) {
79
- const binaryString = window.atob(base64);
80
- const len = binaryString.length;
81
- const bytes = new Uint8Array(len);
82
- for (let i = 0; i < len; i++) {
83
- bytes[i] = binaryString.charCodeAt(i);
84
- }
85
- return bytes.buffer;
45
+ decodeText(encryptedData) {
46
+ const decrypted = CryptoJS.AES.decrypt(
47
+ { ciphertext: CryptoJS.enc.Base64.parse(encryptedData.encrypted) },
48
+ CryptoJS.enc.Hex.parse(this.secretKey),
49
+ {
50
+ iv: CryptoJS.enc.Base64.parse(encryptedData.iv),
51
+ mode: CryptoJS.mode.GCM,
52
+ format: CryptoJS.format.Hex
53
+ }
54
+ );
55
+ return decrypted.toString(CryptoJS.enc.Utf8);
86
56
  }
87
57
 
88
58
  getLayoutName(layout) {
@@ -93,6 +63,7 @@ export class VirtualKeyboard {
93
63
  case 'thSc': return 'Thai scrambled';
94
64
  case 'numpad': return 'Numpad Keyboard';
95
65
  case 'scNum': return 'Scrambled Keyboard';
66
+ default: return 'Unknown Layout';
96
67
  }
97
68
  }
98
69
 
@@ -111,10 +82,10 @@ export class VirtualKeyboard {
111
82
  }
112
83
  }, true);
113
84
 
114
- document.addEventListener('click', (e) => {
115
- const target = document.getElementById("toggle");
116
- target.addEventListener('click', this.toggle.bind(this), { once: true });
117
- });
85
+ const toggle = document.getElementById("toggle");
86
+ if (toggle) {
87
+ toggle.addEventListener('click', this.toggle.bind(this));
88
+ }
118
89
  }
119
90
 
120
91
  setCurrentInput(inputElement) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js_lis",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"