简体   繁体   English

CryptoJS 为不同的文件创建相同的哈希值

[英]CryptoJS creates the same hash value for different files

I am using CryptoJS to create a hash value of the upload file.我正在使用 CryptoJS 创建上传文件的哈希值。 However, all files that I upload produce the same hash value.但是,我上传的所有文件都会产生相同的哈希值。 I know that the issue is inside my "onFileChange" call, but I'm not sure what I am doing wrong here.我知道问题出在我的“onFileChange”调用中,但我不确定我在这里做错了什么。 Any help would be appreciated.任何帮助,将不胜感激。

import React, { Component } from 'react'; 
import { connect } from 'react-redux';
import '../CSS/FileSelectorCSS.css';
var CryptoJS = require("crypto-js");

class FileSelectorComponent extends Component {

    state = {

        // Initially, no file is selected 
        selectedFile: null
    };

    // On file select (from the pop up) 
    onFileChange = event => {
        var text = '';
        // Update the state 
        this.setState({ selectedFile: event.target.files[0] });

        var reader = new FileReader();

        reader.onloadend = function () {
            text = (reader.result);
        }

        reader.readAsBinaryString(event.target.files[0]);

        var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(text));

        console.log(hash.toString());
    };

    // File content to be displayed after 
    // file upload is complete 
    fileData = () => {

        if (this.state.selectedFile) {

            return (
                <div>
                    <h2>File Details:</h2>
                    <p>File Name: {this.state.selectedFile.name}</p>
                    <p>File Type: {this.state.selectedFile.type}</p>
                    <p>
                        Last Modified:{" "}
                        {this.state.selectedFile.lastModifiedDate.toDateString()}
                    </p>
                </div>
            );
        } 
    };

    render() {

        return (
            <div>
                <div>
                    <input type="file" onChange={this.onFileChange} />
                </div>
                {this.fileData()}
            </div>
        );
    }
} 

export default connect()(FileSelectorComponent);导出默认连接()(FileSelectorComponent);

在此处输入图片说明

Expected value:期望值: 在此处输入图片说明

You are calculating your hash before the reader gets loaded.在读取器加载之前,您正在计算您的哈希值。

The hash "d41d8cd98f00b204e9800998ecf8427e" is the MD5 hash of an empty string.散列“d41d8cd98f00b204e9800998ecf8427e”是空字符串的MD5散列。

There lines :有几行:

reader.onloadend = function () {
  text = (reader.result);
}

mean that the text variable will be assigned the result when the reader finished loading, asynchronously .意味着当读者完成加载时, text变量将被异步分配结果。 But by that time, the rest of the function has already been executed.但是到那个时候,函数的其余部分已经被执行了。

So you need to make sure that the process takes place after the text variable gets its new value, like this :因此,您需要确保在text变量获得新值之后进行该过程,如下所示:

reader.onloadend = function () {
  text = (reader.result);

  reader.readAsBinaryString(event.target.files[0]);
  var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(text));
  console.log(hash.toString());
}
  // On file select (from the pop up) 
onFileChange = event => {
    var text = '';
    // Update the state 
    this.setState({ selectedFile: event.target.files[0] });
    console.log(event.target.files[0]);
    var reader = new FileReader();

    reader.onloadend = function () {
        text = (reader.result);
        var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(text));
        console.log(hash.toString());
    }

    reader.readAsBinaryString(event.target.files[0]);

    //console.log(text);
   

    //console.log(hash.toString());
};

CryptoJS needs to be placed inside the callback function. CryptoJS 需要放在回调函数中。 It fixed my issue.它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM