【问题标题】:In Angular2 the SHA256 hash of a file generated does not match with SHA256 from other sites在 Angular2 中,生成的文件的 SHA256 哈希与来自其他站点的 SHA256 不匹配
【发布时间】:2017-09-05 01:26:34
【问题描述】:

我正在尝试为文件生成 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

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

【问题讨论】:

    标签: javascript angular sha256 cryptojs


    【解决方案1】:

    您可以使用: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>
    

    【讨论】:

      猜你喜欢
      • 2019-03-03
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 2021-03-18
      相关资源
      最近更新 更多