@onyx-p/imlib-web 2.0.8 → 2.0.9
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.
- package/index.esm.js +63 -4
- package/index.umd.js +63 -4
- package/package.json +1 -1
package/index.esm.js
CHANGED
@@ -20940,9 +20940,31 @@ class MessageCache {
|
|
20940
20940
|
DB_VERSION = 2;
|
20941
20941
|
STORE_NAME = 'messages';
|
20942
20942
|
DIALOG_STATE_STORE = 'dialogStates';
|
20943
|
+
encryptKey = {
|
20944
|
+
key: ''
|
20945
|
+
};
|
20946
|
+
SALT_PREFIX = 'im_message_cache_salt_prefix';
|
20947
|
+
SALT_SUFFIX = 'im_message_cache_salt_suffix';
|
20943
20948
|
constructor(appKey, userId) {
|
20949
|
+
this.generateEncryptKey(appKey, userId);
|
20944
20950
|
this.init(appKey, userId);
|
20945
20951
|
}
|
20952
|
+
generateEncryptKey(appKey, userId) {
|
20953
|
+
const saltedInput = `${this.SALT_PREFIX}${appKey}_${userId}${this.SALT_SUFFIX}`;
|
20954
|
+
const keyMaterial = CryptoJS.SHA256(saltedInput).toString();
|
20955
|
+
const iv = keyMaterial.substring(0, 16);
|
20956
|
+
const key = keyMaterial.substring(16, 48);
|
20957
|
+
const additionalSalt = CryptoJS.SHA256(`${iv}:${this.SALT_PREFIX}:${userId}:${appKey}`).toString().substring(0, 8);
|
20958
|
+
const finalKey = CryptoJS.PBKDF2(key, additionalSalt, {
|
20959
|
+
keySize: 256 / 32,
|
20960
|
+
iterations: 1000,
|
20961
|
+
hasher: CryptoJS.algo.SHA256
|
20962
|
+
}).toString();
|
20963
|
+
this.encryptKey = {
|
20964
|
+
key: finalKey.substring(0, 32),
|
20965
|
+
iv: iv
|
20966
|
+
};
|
20967
|
+
}
|
20946
20968
|
async init(appKey, userId) {
|
20947
20969
|
this.currentAppKey = appKey;
|
20948
20970
|
this.currentUserId = userId;
|
@@ -21080,6 +21102,28 @@ class MessageCache {
|
|
21080
21102
|
};
|
21081
21103
|
});
|
21082
21104
|
}
|
21105
|
+
encryptContent(content) {
|
21106
|
+
if (!content) return '';
|
21107
|
+
try {
|
21108
|
+
const contentStr = JSON.stringify(content);
|
21109
|
+
return aes256Encrypt(contentStr, this.encryptKey);
|
21110
|
+
} catch (error) {
|
21111
|
+
console.error('加密消息内容失败', error);
|
21112
|
+
return '';
|
21113
|
+
}
|
21114
|
+
}
|
21115
|
+
decryptContent(encryptedHex) {
|
21116
|
+
if (!encryptedHex) {
|
21117
|
+
return null;
|
21118
|
+
}
|
21119
|
+
try {
|
21120
|
+
const decryptedStr = aes256Decrypt(encryptedHex, this.encryptKey);
|
21121
|
+
return JSON.parse(decryptedStr);
|
21122
|
+
} catch (error) {
|
21123
|
+
console.error('解密消息内容失败', error);
|
21124
|
+
return null;
|
21125
|
+
}
|
21126
|
+
}
|
21083
21127
|
async addMessages(messages, toConversation, isEnd = undefined) {
|
21084
21128
|
const dialogId = getFullDialogId(toConversation);
|
21085
21129
|
if (isDef(isEnd)) {
|
@@ -21096,6 +21140,10 @@ class MessageCache {
|
|
21096
21140
|
...message,
|
21097
21141
|
dialogId
|
21098
21142
|
};
|
21143
|
+
if (messageToStore.content) {
|
21144
|
+
const encryptedContent = this.encryptContent(messageToStore.content);
|
21145
|
+
messageToStore.content = encryptedContent;
|
21146
|
+
}
|
21099
21147
|
store.put(messageToStore);
|
21100
21148
|
});
|
21101
21149
|
transaction.oncomplete = () => {
|
@@ -21138,7 +21186,18 @@ class MessageCache {
|
|
21138
21186
|
const cursor = event.target.result;
|
21139
21187
|
if (cursor) {
|
21140
21188
|
if (processedCount < count) {
|
21141
|
-
|
21189
|
+
const message = {
|
21190
|
+
...cursor.value
|
21191
|
+
};
|
21192
|
+
if (typeof message.content === 'string' && message.content) {
|
21193
|
+
try {
|
21194
|
+
message.content = this.decryptContent(message.content);
|
21195
|
+
} catch (error) {
|
21196
|
+
console.error('解密消息内容失败', error);
|
21197
|
+
message.content = null;
|
21198
|
+
}
|
21199
|
+
}
|
21200
|
+
messages.push(message);
|
21142
21201
|
processedCount++;
|
21143
21202
|
cursor.continue();
|
21144
21203
|
} else {
|
@@ -21149,13 +21208,13 @@ class MessageCache {
|
|
21149
21208
|
finishQuery();
|
21150
21209
|
}
|
21151
21210
|
};
|
21152
|
-
|
21211
|
+
const finishQuery = () => {
|
21153
21212
|
messages.sort((a, b) => Long.fromString(a.sentTime).compare(Long.fromString(b.sentTime)));
|
21154
21213
|
resolve({
|
21155
21214
|
messages,
|
21156
21215
|
hasMore: hasMoreMessages || totalCount > count || !isLoaded
|
21157
21216
|
});
|
21158
|
-
}
|
21217
|
+
};
|
21159
21218
|
};
|
21160
21219
|
transaction.onerror = event => {
|
21161
21220
|
console.error('获取消息事务失败', event);
|
@@ -21291,7 +21350,6 @@ class MessageCache {
|
|
21291
21350
|
return new Promise((resolve, reject) => {
|
21292
21351
|
const transaction = this.db.transaction(this.STORE_NAME, 'readwrite');
|
21293
21352
|
const store = transaction.objectStore(this.STORE_NAME);
|
21294
|
-
new Set(event.messageUIdList);
|
21295
21353
|
let completed = 0;
|
21296
21354
|
let hasError = false;
|
21297
21355
|
event.messageUIdList.forEach(id => {
|
@@ -21457,6 +21515,7 @@ class MessageCache {
|
|
21457
21515
|
}
|
21458
21516
|
this.currentAppKey = appKey;
|
21459
21517
|
this.currentUserId = userId;
|
21518
|
+
this.generateEncryptKey(appKey, userId);
|
21460
21519
|
await this.checkAndResetDatabase();
|
21461
21520
|
await this.initDatabase();
|
21462
21521
|
}
|
package/index.umd.js
CHANGED
@@ -20946,9 +20946,31 @@
|
|
20946
20946
|
DB_VERSION = 2;
|
20947
20947
|
STORE_NAME = 'messages';
|
20948
20948
|
DIALOG_STATE_STORE = 'dialogStates';
|
20949
|
+
encryptKey = {
|
20950
|
+
key: ''
|
20951
|
+
};
|
20952
|
+
SALT_PREFIX = 'im_message_cache_salt_prefix';
|
20953
|
+
SALT_SUFFIX = 'im_message_cache_salt_suffix';
|
20949
20954
|
constructor(appKey, userId) {
|
20955
|
+
this.generateEncryptKey(appKey, userId);
|
20950
20956
|
this.init(appKey, userId);
|
20951
20957
|
}
|
20958
|
+
generateEncryptKey(appKey, userId) {
|
20959
|
+
const saltedInput = `${this.SALT_PREFIX}${appKey}_${userId}${this.SALT_SUFFIX}`;
|
20960
|
+
const keyMaterial = CryptoJS.SHA256(saltedInput).toString();
|
20961
|
+
const iv = keyMaterial.substring(0, 16);
|
20962
|
+
const key = keyMaterial.substring(16, 48);
|
20963
|
+
const additionalSalt = CryptoJS.SHA256(`${iv}:${this.SALT_PREFIX}:${userId}:${appKey}`).toString().substring(0, 8);
|
20964
|
+
const finalKey = CryptoJS.PBKDF2(key, additionalSalt, {
|
20965
|
+
keySize: 256 / 32,
|
20966
|
+
iterations: 1000,
|
20967
|
+
hasher: CryptoJS.algo.SHA256
|
20968
|
+
}).toString();
|
20969
|
+
this.encryptKey = {
|
20970
|
+
key: finalKey.substring(0, 32),
|
20971
|
+
iv: iv
|
20972
|
+
};
|
20973
|
+
}
|
20952
20974
|
async init(appKey, userId) {
|
20953
20975
|
this.currentAppKey = appKey;
|
20954
20976
|
this.currentUserId = userId;
|
@@ -21086,6 +21108,28 @@
|
|
21086
21108
|
};
|
21087
21109
|
});
|
21088
21110
|
}
|
21111
|
+
encryptContent(content) {
|
21112
|
+
if (!content) return '';
|
21113
|
+
try {
|
21114
|
+
const contentStr = JSON.stringify(content);
|
21115
|
+
return aes256Encrypt(contentStr, this.encryptKey);
|
21116
|
+
} catch (error) {
|
21117
|
+
console.error('加密消息内容失败', error);
|
21118
|
+
return '';
|
21119
|
+
}
|
21120
|
+
}
|
21121
|
+
decryptContent(encryptedHex) {
|
21122
|
+
if (!encryptedHex) {
|
21123
|
+
return null;
|
21124
|
+
}
|
21125
|
+
try {
|
21126
|
+
const decryptedStr = aes256Decrypt(encryptedHex, this.encryptKey);
|
21127
|
+
return JSON.parse(decryptedStr);
|
21128
|
+
} catch (error) {
|
21129
|
+
console.error('解密消息内容失败', error);
|
21130
|
+
return null;
|
21131
|
+
}
|
21132
|
+
}
|
21089
21133
|
async addMessages(messages, toConversation, isEnd = undefined) {
|
21090
21134
|
const dialogId = getFullDialogId(toConversation);
|
21091
21135
|
if (isDef(isEnd)) {
|
@@ -21102,6 +21146,10 @@
|
|
21102
21146
|
...message,
|
21103
21147
|
dialogId
|
21104
21148
|
};
|
21149
|
+
if (messageToStore.content) {
|
21150
|
+
const encryptedContent = this.encryptContent(messageToStore.content);
|
21151
|
+
messageToStore.content = encryptedContent;
|
21152
|
+
}
|
21105
21153
|
store.put(messageToStore);
|
21106
21154
|
});
|
21107
21155
|
transaction.oncomplete = () => {
|
@@ -21144,7 +21192,18 @@
|
|
21144
21192
|
const cursor = event.target.result;
|
21145
21193
|
if (cursor) {
|
21146
21194
|
if (processedCount < count) {
|
21147
|
-
|
21195
|
+
const message = {
|
21196
|
+
...cursor.value
|
21197
|
+
};
|
21198
|
+
if (typeof message.content === 'string' && message.content) {
|
21199
|
+
try {
|
21200
|
+
message.content = this.decryptContent(message.content);
|
21201
|
+
} catch (error) {
|
21202
|
+
console.error('解密消息内容失败', error);
|
21203
|
+
message.content = null;
|
21204
|
+
}
|
21205
|
+
}
|
21206
|
+
messages.push(message);
|
21148
21207
|
processedCount++;
|
21149
21208
|
cursor.continue();
|
21150
21209
|
} else {
|
@@ -21155,13 +21214,13 @@
|
|
21155
21214
|
finishQuery();
|
21156
21215
|
}
|
21157
21216
|
};
|
21158
|
-
|
21217
|
+
const finishQuery = () => {
|
21159
21218
|
messages.sort((a, b) => Long.fromString(a.sentTime).compare(Long.fromString(b.sentTime)));
|
21160
21219
|
resolve({
|
21161
21220
|
messages,
|
21162
21221
|
hasMore: hasMoreMessages || totalCount > count || !isLoaded
|
21163
21222
|
});
|
21164
|
-
}
|
21223
|
+
};
|
21165
21224
|
};
|
21166
21225
|
transaction.onerror = event => {
|
21167
21226
|
console.error('获取消息事务失败', event);
|
@@ -21297,7 +21356,6 @@
|
|
21297
21356
|
return new Promise((resolve, reject) => {
|
21298
21357
|
const transaction = this.db.transaction(this.STORE_NAME, 'readwrite');
|
21299
21358
|
const store = transaction.objectStore(this.STORE_NAME);
|
21300
|
-
new Set(event.messageUIdList);
|
21301
21359
|
let completed = 0;
|
21302
21360
|
let hasError = false;
|
21303
21361
|
event.messageUIdList.forEach(id => {
|
@@ -21463,6 +21521,7 @@
|
|
21463
21521
|
}
|
21464
21522
|
this.currentAppKey = appKey;
|
21465
21523
|
this.currentUserId = userId;
|
21524
|
+
this.generateEncryptKey(appKey, userId);
|
21466
21525
|
await this.checkAndResetDatabase();
|
21467
21526
|
await this.initDatabase();
|
21468
21527
|
}
|