@ysgroup/core 0.1.11 → 0.1.12

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/package.json +1 -1
  2. package/src/password.ts +29 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ysgroup/core",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
package/src/password.ts CHANGED
@@ -77,12 +77,12 @@ export async function sealPassword(
77
77
  const recipientRaw = b64uDecode(publicKeyB64);
78
78
  if (recipientRaw.byteLength !== 32) throw new Error("登录公钥长度无效");
79
79
 
80
- const ephemeral = await crypto.subtle.generateKey({ name: "X25519" }, true, [
80
+ const ephemeral = (await crypto.subtle.generateKey({ name: "X25519" }, true, [
81
81
  "deriveBits",
82
- ]);
82
+ ])) as CryptoKeyPair;
83
83
  const recipient = await crypto.subtle.importKey(
84
84
  "raw",
85
- recipientRaw,
85
+ asArrayBuffer(recipientRaw),
86
86
  { name: "X25519" },
87
87
  false,
88
88
  [],
@@ -94,11 +94,20 @@ export async function sealPassword(
94
94
  256,
95
95
  ),
96
96
  );
97
- const hkdfKey = await crypto.subtle.importKey("raw", shared, "HKDF", false, [
98
- "deriveBits",
99
- ]);
97
+ const hkdfKey = await crypto.subtle.importKey(
98
+ "raw",
99
+ asArrayBuffer(shared),
100
+ "HKDF",
101
+ false,
102
+ ["deriveBits"],
103
+ );
100
104
  const aesRaw = await crypto.subtle.deriveBits(
101
- { name: "HKDF", hash: "SHA-256", salt: SALT, info: INFO },
105
+ {
106
+ name: "HKDF",
107
+ hash: "SHA-256",
108
+ salt: asArrayBuffer(SALT),
109
+ info: asArrayBuffer(INFO),
110
+ },
102
111
  hkdfKey,
103
112
  256,
104
113
  );
@@ -112,7 +121,12 @@ export async function sealPassword(
112
121
  const aad = new TextEncoder().encode(`${PREFIX}${kid}`);
113
122
  const packedCt = new Uint8Array(
114
123
  await crypto.subtle.encrypt(
115
- { name: "AES-GCM", iv: NONCE, additionalData: aad, tagLength: 128 },
124
+ {
125
+ name: "AES-GCM",
126
+ iv: asArrayBuffer(NONCE),
127
+ additionalData: aad,
128
+ tagLength: 128,
129
+ },
116
130
  aesKey,
117
131
  new TextEncoder().encode(plaintext),
118
132
  ),
@@ -126,6 +140,13 @@ export async function sealPassword(
126
140
  return `${PREFIX}${kid}.${b64uEncode(packed)}`;
127
141
  }
128
142
 
143
+ function asArrayBuffer(bytes: Uint8Array): ArrayBuffer {
144
+ return bytes.buffer.slice(
145
+ bytes.byteOffset,
146
+ bytes.byteOffset + bytes.byteLength,
147
+ ) as ArrayBuffer;
148
+ }
149
+
129
150
  function b64uEncode(raw: Uint8Array): string {
130
151
  let bin = "";
131
152
  for (const byte of raw) bin += String.fromCharCode(byte);