Tengo un código simple en el backend de Java que está procesando algunos bytes.
import org.bouncycastle.jcajce.provider.digest.SHA256; import java.util.Arrays; public class ShaTest { public static void main(String... args) { var input = new byte[] {0, 1, 2, 3, 4, 5, 6, 7}; var result = new SHA256.Digest().digest(input); System.out.println(Arrays.toString(result)); } } El resultado es igual a [-118, -123, 31, -8, 46, -25, 4, -118, -48, -98, -61, -124, 127, 29, -33, 68, -108, 65, 4, -46, -53, -47, 126, -12, -29, -37, 34, -58, 120, 90, 13, 69]
Ahora, en el lado frontal, necesito codificar los mismos bytes y espero tener el mismo resultado usando la biblioteca js-sha256 . La función hash es tan simple como
hash(input: any): any { return sha.sha256.update(input).digest(); }Y estoy tratando de hacer hash usando varias entradas diferentes
const rawInput = [0, 1, 2, 3, 4, 5, 6, 7]; console.log('rawInput', this.hash(rawInput)); console.log('uint8Input', this.hash(new Uint8Array(rawInput))); console.log('int8Input', this.hash(new Int8Array(rawInput))); const intArr = new Int8Array(new ArrayBuffer(8)); intArr.set(rawInput); console.log('intArrayWithBuffer', this.hash(intArr)); const uintArr = new Uint8Array(new ArrayBuffer(8)); uintArr.set(rawInput); console.log('uintArrayWithBuffer', this.hash(uintArr)); Sin embargo, el resultado es diferente que en el lado del backend. Frontend en cambio produce [138, 133, 31, 248, 46, 231, 4, 138, 208, 158, 195, 132, 127, 29, 223, 68, 148, 65, 4, 210, 203, 209, 126, 244, 227, 219, 34, 198, 120, 90, 13, 69]
¿Por qué está pasando eso?