From c9fb1e70903ed74c1288df40ab90222fe29ac7fd Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 3 Oct 2021 22:07:41 +0200 Subject: [PATCH] Moved manifest hashes generation to signature file --- src/PKPass.ts | 16 ++++++---------- src/signature.ts | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index 00e72ef..2aa38ae 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -4,7 +4,6 @@ import { getModelFolderContents } from "./parser"; import * as Schemas from "./schemas"; import { Stream } from "stream"; import { processDate } from "./processDate"; -import forge from "node-forge"; import * as Signature from "./signature"; import * as Strings from "./StringsUtils"; import { isValidRGB } from "./utils"; @@ -513,16 +512,13 @@ export default class PKPass extends Bundle { private [createManifestSymbol](): Buffer { const manifest = Object.entries(this[filesSymbol]).reduce<{ [key: string]: string; - }>((acc, [fileName, buffer]) => { - const hashFlow = forge.md.sha1.create(); - - hashFlow.update(buffer.toString("binary")); - - return { + }>( + (acc, [fileName, buffer]) => ({ ...acc, - [fileName]: hashFlow.digest().toHex(), - }; - }, {}); + [fileName]: Signature.createHash(buffer), + }), + {}, + ); return Buffer.from(JSON.stringify(manifest)); } diff --git a/src/signature.ts b/src/signature.ts index 555fa25..30e1d35 100644 --- a/src/signature.ts +++ b/src/signature.ts @@ -1,6 +1,20 @@ import forge from "node-forge"; import type * as Schemas from "./schemas"; +/** + * Creates an hash for a buffer. Used by manifest + * + * @param buffer + * @returns + */ + +export function createHash(buffer: Buffer) { + const hashFlow = forge.md.sha1.create(); + hashFlow.update(buffer.toString("binary")); + + return hashFlow.digest().toHex(); +} + /** * Generates the PKCS #7 cryptografic signature for the manifest file. *