js_lis 3.1.0 → 3.1.1

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 +40 -27
  2. package/package.json +1 -1
@@ -1,27 +1,32 @@
1
1
  import { layouts } from "./layouts.js";
2
2
  import { getCSS, injectCSS } from "./style/kbstyle.js";
3
3
 
4
- //AES-GCM ผ่าน WeakMap
4
+ //AES-GCM ผ่าน WeakMap พร้อม Fallback สำหรับ HTTP
5
5
  class SecureBufferStore {
6
6
  constructor() {
7
7
  this._store = new WeakMap();
8
+ this.hasCrypto = typeof crypto !== 'undefined' && !!crypto.subtle;
8
9
  }
9
10
 
10
11
  //สร้างกุญแจเข้ารหัส
11
12
  async _getEntry(inputEl) {
12
13
  if (!this._store.has(inputEl)) {
13
- const key = await crypto.subtle.generateKey(
14
- { name: "AES-GCM", length: 256 },
15
- false, // ไม่ export ออก
16
- ["encrypt", "decrypt"]
17
- );
18
- const iv = crypto.getRandomValues(new Uint8Array(12));
19
- const ciphertext = await crypto.subtle.encrypt(
20
- { name: "AES-GCM", iv },
21
- key,
22
- new TextEncoder().encode("")
23
- );
24
- this._store.set(inputEl, { key, iv, ciphertext });
14
+ if (this.hasCrypto) {
15
+ const key = await crypto.subtle.generateKey(
16
+ { name: "AES-GCM", length: 256 },
17
+ false, // ไม่ export ออก
18
+ ["encrypt", "decrypt"]
19
+ );
20
+ const iv = crypto.getRandomValues(new Uint8Array(12));
21
+ const ciphertext = await crypto.subtle.encrypt(
22
+ { name: "AES-GCM", iv },
23
+ key,
24
+ new TextEncoder().encode("")
25
+ );
26
+ this._store.set(inputEl, { type: 'crypto', key, iv, ciphertext });
27
+ } else {
28
+ this._store.set(inputEl, { type: 'fallback', text: '' });
29
+ }
25
30
  }
26
31
  return this._store.get(inputEl);
27
32
  }
@@ -29,26 +34,34 @@ class SecureBufferStore {
29
34
  //ถอดรหัสชั่วคราว
30
35
  async read(inputEl) {
31
36
  if (!this._store.has(inputEl)) return "";
32
- const { key, iv, ciphertext } = this._store.get(inputEl);
33
- try {
34
- const plain = await crypto.subtle.decrypt({ name: "AES-GCM", iv }, key, ciphertext);
35
- return new TextDecoder().decode(plain);
36
- } catch {
37
- return "";
37
+ const entry = this._store.get(inputEl);
38
+ if (entry.type === 'crypto') {
39
+ try {
40
+ const plain = await crypto.subtle.decrypt({ name: "AES-GCM", iv: entry.iv }, entry.key, entry.ciphertext);
41
+ return new TextDecoder().decode(plain);
42
+ } catch {
43
+ return "";
44
+ }
45
+ } else {
46
+ return entry.text;
38
47
  }
39
48
  }
40
49
 
41
50
  //เข้ารหัสใหม่
42
51
  async write(inputEl, plaintext) {
43
52
  const entry = await this._getEntry(inputEl);
44
- const iv = crypto.getRandomValues(new Uint8Array(12));
45
- const ciphertext = await crypto.subtle.encrypt(
46
- { name: "AES-GCM", iv },
47
- entry.key,
48
- new TextEncoder().encode(plaintext)
49
- );
50
- entry.iv = iv;
51
- entry.ciphertext = ciphertext;
53
+ if (entry.type === 'crypto') {
54
+ const iv = crypto.getRandomValues(new Uint8Array(12));
55
+ const ciphertext = await crypto.subtle.encrypt(
56
+ { name: "AES-GCM", iv },
57
+ entry.key,
58
+ new TextEncoder().encode(plaintext)
59
+ );
60
+ entry.iv = iv;
61
+ entry.ciphertext = ciphertext;
62
+ } else {
63
+ entry.text = plaintext;
64
+ }
52
65
  }
53
66
 
54
67
  //ค่าเริ่มต้นให้กับ Input
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js_lis",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"