From a403286cfae896e016eae5c9b40d4be4b0effd8e Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 5 Dec 2022 11:06:10 -0500 Subject: [PATCH] refactor(compiler): reduce complexity for legacy i18n digest string output (#48362) When using the legacy digest algorithm for i18n messages, the output hexadecimal string now leverages a number's `toString()` function in addition to the `padStart` string function to generate the result. This removes the need for several helper functions which involved a series of iteration and bitwise operations to previously generate the same output. PR Close #48362 --- packages/compiler/src/i18n/digest.ts | 34 ++++++++++------------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/packages/compiler/src/i18n/digest.ts b/packages/compiler/src/i18n/digest.ts index 0f5b64feacb..2970b83d0d3 100644 --- a/packages/compiler/src/i18n/digest.ts +++ b/packages/compiler/src/i18n/digest.ts @@ -152,7 +152,18 @@ export function sha1(str: string): string { e = add32(e, h4); } - return bytesToHexString(words32ToByteString([a, b, c, d, e])); + // Convert the output parts to a 160-bit hexadecimal string + return toHexU32(a) + toHexU32(b) + toHexU32(c) + toHexU32(d) + toHexU32(e); +} + +/** + * Convert and format a number as a string representing a 32-bit unsigned hexadecimal number. + * @param value The value to format as a string. + * @returns A hexadecimal string representing the value. + */ +function toHexU32(value: number): string { + // unsigned right shift of zero ensures an unsigned 32-bit number + return (value >>> 0).toString(16).padStart(8, '0'); } function fk(index: number, b: number, c: number, d: number): [number, number] { @@ -357,27 +368,6 @@ function wordAt(bytes: Byte[], index: number, endian: Endian): number { return word; } -function words32ToByteString(words32: number[]): Byte[] { - return words32.reduce((bytes, word) => bytes.concat(word32ToByteString(word)), [] as Byte[]); -} - -function word32ToByteString(word: number): Byte[] { - let bytes: Byte[] = []; - for (let i = 0; i < 4; i++) { - bytes.push((word >>> 8 * (3 - i)) & 0xff); - } - return bytes; -} - -function bytesToHexString(bytes: Byte[]): string { - let hex: string = ''; - for (let i = 0; i < bytes.length; i++) { - const b = byteAt(bytes, i); - hex += (b >>> 4).toString(16) + (b & 0x0f).toString(16); - } - return hex.toLowerCase(); -} - /** * Create a shared exponentiation pool for base-256 computations. This shared pool provides memoized * power-of-256 results with memoized power-of-two computations for efficient multiplication.