signal-desktop/ts/updater/util.ts

45 lines
953 B
TypeScript
Raw Normal View History

2022-02-24 21:01:41 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { createReadStream } from 'fs';
2022-03-03 22:34:51 +00:00
import { pipeline } from 'stream/promises';
2022-02-24 21:01:41 +00:00
import { createHash } from 'crypto';
import * as Errors from '../types/errors';
export type CheckIntegrityResultType = Readonly<
| {
ok: true;
error?: void;
}
| {
ok: false;
error: string;
}
>;
export async function checkIntegrity(
fileName: string,
sha512: string
): Promise<CheckIntegrityResultType> {
try {
const hash = createHash('sha512');
2022-03-03 22:34:51 +00:00
await pipeline(createReadStream(fileName), hash);
2022-02-24 21:01:41 +00:00
const actualSHA512 = hash.digest('base64');
if (sha512 === actualSHA512) {
return { ok: true };
}
return {
ok: false,
error: `Integrity check failure: expected ${sha512}, got ${actualSHA512}`,
};
} catch (error) {
return {
ok: false,
error: Errors.toLogFormat(error),
};
}
}