1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-12-02 17:02:31 +03:00

Merge remote-tracking branch 'origin/t3chguy/ts/4' into t3chguy/ts/4

This commit is contained in:
Michael Telatynski
2020-10-13 22:34:22 +01:00

View File

@@ -52,15 +52,17 @@ async function encryptNode(data: string, key: Uint8Array, name: string, ivStr?:
const [aesKey, hmacKey] = deriveKeysNode(key, name); const [aesKey, hmacKey] = deriveKeysNode(key, name);
const cipher = crypto.createCipheriv("aes-256-ctr", aesKey, iv); const cipher = crypto.createCipheriv("aes-256-ctr", aesKey, iv);
const ciphertext = cipher.update(data, "utf8", "base64") const ciphertext = Buffer.concat([
+ cipher.final("base64"); cipher.update(data, "utf8"),
cipher.final(),
]);
const hmac = crypto.createHmac("sha256", hmacKey) const hmac = crypto.createHmac("sha256", hmacKey)
.update(ciphertext, "base64").digest("base64"); .update(ciphertext).digest("base64");
return { return {
iv: encodeBase64(iv), iv: encodeBase64(iv),
ciphertext: ciphertext, ciphertext: ciphertext.toString("base64"),
mac: hmac, mac: hmac,
}; };
} }
@@ -84,7 +86,8 @@ async function decryptNode(data: IData, key: Uint8Array, name: string) {
const [aesKey, hmacKey] = deriveKeysNode(key, name); const [aesKey, hmacKey] = deriveKeysNode(key, name);
const hmac = crypto.createHmac("sha256", hmacKey) const hmac = crypto.createHmac("sha256", hmacKey)
.update(data.ciphertext, "base64").digest("base64").replace(/=+$/g, ''); .update(Buffer.from(data.ciphertext, "base64"))
.digest("base64").replace(/=+$/g, '');
if (hmac !== data.mac.replace(/=+$/g, '')) { if (hmac !== data.mac.replace(/=+$/g, '')) {
throw new Error(`Error decrypting secret ${name}: bad MAC`); throw new Error(`Error decrypting secret ${name}: bad MAC`);