crypto: fix nodejs native encryption causing file corruption

This commit is contained in:
Abdullah Atta
2023-09-20 10:38:30 +05:00
committed by Abdullah Atta
parent 6d9aa56084
commit 3492bef2c7

View File

@@ -364,7 +364,7 @@ export function to_base64(
input: string | Uint8Array, input: string | Uint8Array,
variant: base64_variants = base64_variants.URLSAFE_NO_PADDING variant: base64_variants = base64_variants.URLSAFE_NO_PADDING
): string { ): string {
const base64 = Buffer.from(toBuffer(input)).toString( const base64 = toBuffer(input).toString(
variant === base64_variants.URLSAFE || variant === base64_variants.URLSAFE ||
variant === base64_variants.URLSAFE_NO_PADDING variant === base64_variants.URLSAFE_NO_PADDING
? "base64url" ? "base64url"
@@ -383,7 +383,9 @@ export function from_hex(input: string): Uint8Array {
} }
export function to_string(input: Uint8Array): string { export function to_string(input: Uint8Array): string {
return Buffer.from(input).toString("utf-8"); return Buffer.from(input, input.byteOffset, input.byteLength).toString(
"utf-8"
);
} }
export { export {
@@ -413,7 +415,7 @@ function toBuffer<TInput extends ToBufferInput>(
input: TInput input: TInput
): ToBufferResult<TInput> { ): ToBufferResult<TInput> {
if (input instanceof Uint8Array) { if (input instanceof Uint8Array) {
return Buffer.from(input.buffer, 0, input.byteLength); return Buffer.from(input.buffer, input.byteOffset, input.byteLength);
} else if (typeof input === "undefined" || input === null) { } else if (typeof input === "undefined" || input === null) {
return undefined as ToBufferResult<TInput>; return undefined as ToBufferResult<TInput>;
} else { } else {