0

I'm using react-dropzone, I would need a way to be able to calculate the sha256 of the file when the file is selected then inside the onDrop() function, to use it as a checksum for the checks I'm doing.

I thought of using CryptoJS, but it doesn't return to me what I hope it should return to me, that is, a 64-character code.

function Dropzone(props) {
    const { acceptedFiles, getRootProps, getInputProps } = useDropzone({
        onDrop: props.onDrop
    });
    const file = acceptedFiles[0];
    return (
        <div>
            <Typography>File Name</Typography>
            {file ? (
                <p>
                    {file.path} - {file.size} bytes
                </p>
            ) : (
                <p />
            )}
            <div {...getRootProps({ className: 'drop-zone' })}>
                <input {...getInputProps()} multiple={false} />
                <p>Drag 'n' drop files here, or click to select files</p>
            </div>
        </div>
    );
}

const onDrop = files => {
        console.log(files);
    };

<Dropzone onDrop={onDrop} multiple={false} />
Paul
  • 3,644
  • 9
  • 47
  • 113
  • have you tried js-sha256? It supports ByteArray as input. https://www.npmjs.com/package/js-sha256 – Jeppe Hasseriis Feb 27 '20 at 11:59
  • I'm seeing it right now, but I'm not understanding how to convert the File to Hash, if I do something like this it gives me error `sha256 (files [0])` Uncaught (in promise) Error: input is invalid type     at Sha256.push ../ node_modules / js-sha256 / src / sha256.js.Sha256.update – Paul Feb 27 '20 at 12:06
  • If you look at the usage, there's an example with the FileReader API which can help you with `readAsArrayBuffer`, https://github.com/react-dropzone/react-dropzone#usage – Jeppe Hasseriis Feb 27 '20 at 12:14

0 Answers0