Skip to content

Commit

Permalink
use sha256 instead of random
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Oct 1, 2024
1 parent 37ba14c commit f7d8aeb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 7 additions & 0 deletions apps/meteor/app/e2e/client/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}
6 changes: 3 additions & 3 deletions apps/meteor/app/e2e/client/rocketchat.e2e.room.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -25,6 +24,7 @@ import {
readFileAsArrayBuffer,
encryptAESCTR,
generateAESCTRKey,
createSha256Hash,
} from './helper';
import { log, logError } from './logger';
import { e2e } from './rocketchat.e2e';
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f7d8aeb

Please sign in to comment.