Skip to content

Instantly share code, notes, and snippets.

@ggicci
Last active November 11, 2021 06:41
Show Gist options
  • Save ggicci/a7699ff9509b6c739077cb117af39158 to your computer and use it in GitHub Desktop.
Save ggicci/a7699ff9509b6c739077cb117af39158 to your computer and use it in GitHub Desktop.
CryptoJS - Get MD5 of Binary Files
import CryptoMD5 from "crypto-js/md5";
import CryptoLatin1Encoder from "crypto-js/enc-latin1";
function handleUploadClick(e: React.ChangeEvent<HTMLInputElement>) {
if (!e.target.files) {
return;
}
const selectedFile = e.target.files[0];
const reader = new FileReader();
reader.readAsBinaryString(selectedFile);
reader.onloadend = (e) => {
// Calculate md5 by using CryptoJS after content loaded.
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader
const gotMD5 = CryptoMD5(
CryptoLatin1Encoder.parse(e.target!.result as string)
);
const hexMD4 = gotMD5.toString();
};
}
@ggicci
Copy link
Author

ggicci commented Nov 11, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment