Backup encryption and improvements
This commit is contained in:
parent
d2850bdbd9
commit
87ea909ae9
15 changed files with 775 additions and 295 deletions
37
ts/util/appendMacStream.ts
Normal file
37
ts/util/appendMacStream.ts
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { createHmac } from 'crypto';
|
||||
import { Transform } from 'stream';
|
||||
import type { Duplex } from 'stream';
|
||||
|
||||
import { HashType } from '../types/Crypto';
|
||||
|
||||
export const MAC_KEY_SIZE = 32;
|
||||
|
||||
export const MAC_SIZE = 32;
|
||||
|
||||
export function appendMacStream(macKey: Uint8Array): Duplex {
|
||||
if (macKey.byteLength !== MAC_KEY_SIZE) {
|
||||
throw new Error('appendMacStream: invalid macKey length');
|
||||
}
|
||||
|
||||
const hmac = createHmac(HashType.size256, macKey);
|
||||
return new Transform({
|
||||
transform(chunk, _encoding, callback) {
|
||||
try {
|
||||
hmac.update(chunk);
|
||||
callback(null, chunk);
|
||||
} catch (error) {
|
||||
callback(error);
|
||||
}
|
||||
},
|
||||
flush(callback) {
|
||||
try {
|
||||
callback(null, hmac.digest());
|
||||
} catch (error) {
|
||||
callback(error);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue