在Angular2中,生成的文件的SHA256哈希与来自其他站点的SHA256不匹配

时间:2017-04-10 05:46:38

标签: javascript angular sha256 cryptojs

我试图为文件生成SHA-256哈希。

我已经使用了https://www.npmjs.com/package/crypto-js库。请看下面的代码。

import { Component, OnInit } from '@angular/core';
var SHA256 = require("crypto-js/sha256");
@Component({
  moduleId: module.id,
  selector: 'dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
  hash: string;
  constructor() { }
  ngOnInit() {}

  onFilesChange(fileList : Array<File>){
    this.fileList = fileList;
    console.log(fileList);
    for(var file in fileList){
      this.hash = SHA256(file);
      console.log(this.hash.toString());
    }

  }

}

档案:file for sha-256 上面的代码我得到了sh256哈希:5feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9

但是我引用了许多在线网站,例如http://onlinemd5.com/,http://www.online-convert.com/,https://md5file.com/calculator

表格在线网站我得到sha256哈希: 27bb4358e847d559bed9f34eeee51ca71f51542afb0de4017b80dd66a0656eca

谁能告诉我为什么我会得到不同的哈希?

1 个答案:

答案 0 :(得分:-2)

您可以使用:https://www.jokecamp.com/blog/examples-of-creating-base64-hashes-using-hmac-sha256-in-different-languages/#js

 <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>

<script>
  var hash = CryptoJS.HmacSHA256("Message", "secret");
  var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
  document.write(hashInBase64);
</script>