From 461bd820b4c89d261ade6576874dd5bd1edb4341 Mon Sep 17 00:00:00 2001 From: glooca <47379602+glooca@users.noreply.github.com> Date: Fri, 14 Jan 2022 19:01:55 +0200 Subject: [PATCH] Crc leading zero fix for streams --- mod.ts | 4 ++-- test.ts | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mod.ts b/mod.ts index bcd47ad..ac663e3 100644 --- a/mod.ts +++ b/mod.ts @@ -16,7 +16,7 @@ export function crc32(arr: Uint8Array | string): string { crc = (crc >>> 8) ^ temp; } - return numberToHex(crc ^ -1).padStart(8, "0"); + return numberToHex(crc ^ -1); } export class Crc32Stream { @@ -69,7 +69,7 @@ export class Crc32Stream { } export function numberToHex(n: number): string { - return (n >>> 0).toString(16); + return (n >>> 0).toString(16).padStart(8, "0"); } export function hexToUint8Array(str: string): Uint8Array { diff --git a/test.ts b/test.ts index 2ecbe23..1fa2115 100644 --- a/test.ts +++ b/test.ts @@ -26,7 +26,8 @@ Deno.test("Crc32Stream", () => { const crc32Stream = new Crc32Stream(); crc32Stream.append("d"); // 98dd4acc === crc32("d") crc32Stream.append("e"); // 7d90298b === crc32("de") - crc32Stream.append("n"); // 21f6953 === crc32("den") + crc32Stream.append("n"); // 021f6953 === crc32("den") + assertEquals(crc32Stream.crc32, "021f6953"); assertEquals(crc32Stream.append("o"), crc32_deno); assertEquals(crc32Stream.crc32, crc32_deno); assertEquals(crc32Stream.crc32, crc32_deno); @@ -35,7 +36,8 @@ Deno.test("Crc32Stream", () => { crc32Stream.append("d"); // 98dd4acc === crc32("d") crc32Stream.append("e"); // 7d90298b === crc32("de") - crc32Stream.append("n"); // 21f6953 === crc32("den") + crc32Stream.append("n"); // 021f6953 === crc32("den") + assertEquals(crc32Stream.crc32, "021f6953"); assertEquals(crc32Stream.append("o"), crc32_deno); assertEquals(crc32Stream.crc32, crc32_deno); assertEquals(crc32Stream.crc32, crc32_deno);