Home > OS >  Calculate client side MD5 hash of an image file to match one calculated by Firebase?
Calculate client side MD5 hash of an image file to match one calculated by Firebase?

Time:01-24

Is there a method to calculate the MD5 of an image file on the client side in my Angular Map, that will match the MD5 when I store that file on Firestore? I need to be able to confirm that a user file matches a reference version stored in firebase without having to upload it again.

I can get an array buffer from a file object on the local machine, but can't get the md5 calculation done locally to match what Firebase returns.

The result I got looks nothing like the md5 reported by Firebase, so I don't think I'm on the right track at all:

let file = target.files[0];
let reader = new FileReader();
reader.onload = function (event) {
  data = event.target.result;
  let ret: any = data;
  if (data) {
    let len = ret.byteLength;
    let uintArBuff = new Uint8Array(ret);   //Does an array buffer convert to a Uint8Array?
    let md5 = new Md5();
    let hash = md5.appendByteArray(uintArBuff).end();  
    console.log(hash);
  }  
}
reader.readAsArrayBuffer(file);

The above produced a result that looks like 29e10414c7c7b7adb61330b02f8f3ddc, while the MD5 reported by firebase is KeEEFMfHt622EzCwL4893A== so I think I'm up the wrong tree altogether. Not even the same length hash.. . .

CodePudding user response:

I would have commented but reputation doesn't allow me to.

KeEEFMfHt622EzCwL4893A== is base64 encoded. Yours is hex encoded.

I just used an online converted and it checks out that they match.

  •  Tags:  
  • Related