我有一个函数可以获取一个文件并找到它的 SHA256 哈希。每次我重新提交文件时,它都会为同一个文件生成不同的哈希值。
在第一次提交时,它会产生正确的哈希。每次重新提交都会产生一个不正确的哈希值。如果我以相同的顺序重新提交相同的文件,它们都会产生相同的(不正确的)哈希。
我认为缓冲区可能正在增加。或者也许是别的什么?我试图弄清楚如何清除缓冲区数组。
有任何想法吗?
import React, { Component } from "react";
const crypto = require("crypto");
const hash = crypto.createHash("sha256");
class SearchPage extends Component {
constructor(props) {
super(props);
this.state = {
hashOutput: "",
fileName: "",
};
}
onChange(e) {
let files = e.target.files;
this.setState({ fileName: files[0].name });
let reader = new FileReader();
reader.readAsArrayBuffer(files[0]);
reader.onload = e => {
hash.update(Buffer.from(e.target.result));
const hashOutput = hash.digest("hex");
this.setState({ hashOutput });
console.log(hashOutput);
};
}
render() {
return (
<div onSubmit={this.onFormSubmit}>
<input type="file" name="file" onChange={e => this.onChange(e)} />
</div>
);
}
}
export default SearchPage;