From f7d8aeb19b234cc47f7d74f82aacab8e78d8ee91 Mon Sep 17 00:00:00 2001 From: Kevin Aleman Date: Tue, 1 Oct 2024 13:17:42 -0600 Subject: [PATCH] use sha256 instead of random --- apps/meteor/app/e2e/client/helper.js | 7 +++++++ apps/meteor/app/e2e/client/rocketchat.e2e.room.js | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/apps/meteor/app/e2e/client/helper.js b/apps/meteor/app/e2e/client/helper.js index 49b157c5ccf4..ddf49b262b91 100644 --- a/apps/meteor/app/e2e/client/helper.js +++ b/apps/meteor/app/e2e/client/helper.js @@ -146,3 +146,10 @@ export async function generateMnemonicPhrase(n, sep = ' ') { } return result.join(sep); } + +export async function createSha256Hash(data) { + const hash = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(data)); + return Array.from(new Uint8Array(hash)) + .map((b) => b.toString(16).padStart(2, '0')) + .join(''); +} diff --git a/apps/meteor/app/e2e/client/rocketchat.e2e.room.js b/apps/meteor/app/e2e/client/rocketchat.e2e.room.js index 5b8423e5e647..1b2357067028 100644 --- a/apps/meteor/app/e2e/client/rocketchat.e2e.room.js +++ b/apps/meteor/app/e2e/client/rocketchat.e2e.room.js @@ -1,6 +1,5 @@ import { Base64 } from '@rocket.chat/base64'; import { Emitter } from '@rocket.chat/emitter'; -import { Random } from '@rocket.chat/random'; import EJSON from 'ejson'; import { RoomManager } from '../../../client/lib/RoomManager'; @@ -25,6 +24,7 @@ import { readFileAsArrayBuffer, encryptAESCTR, generateAESCTRKey, + createSha256Hash, } from './helper'; import { log, logError } from './logger'; import { e2e } from './rocketchat.e2e'; @@ -285,7 +285,7 @@ export class E2ERoom extends Emitter { // When a new e2e room is created, it will be initialized without an e2e key id // This will prevent new rooms from storing `undefined` as the keyid if (!this.keyID) { - this.keyID = this.roomKeyId || Random.id(12); + this.keyID = this.roomKeyId || (await createSha256Hash(this.sessionKeyExportedString)).slice(0, 12); } // Import session key for use. @@ -314,7 +314,7 @@ export class E2ERoom extends Emitter { try { const sessionKeyExported = await exportJWKKey(this.groupSessionKey); this.sessionKeyExportedString = JSON.stringify(sessionKeyExported); - this.keyID = Random.id(12); + this.keyID = (await createSha256Hash(this.sessionKeyExportedString)).slice(0, 12); await sdk.call('e2e.setRoomKeyID', this.roomId, this.keyID); await this.encryptKeyForOtherParticipants();