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
This commit is contained in:
Charles Lyding 2022-12-05 11:06:10 -05:00 committed by Andrew Kushnir
parent 6533f424b7
commit a403286cfa

View file

@ -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.