【问题标题】:How to convert SHA-256 Java to JavaScript( IONIC with NPM )?如何将 SHA-256 Java 转换为 JavaScript(带有 NPM 的 IONIC)?
【发布时间】:2020-04-15 07:26:01
【问题描述】:

我在JAVA 中给出了以下代码。我需要像IONIC Framework 一样转换 JavaScript。我尝试了IONIC AppNPM "crypto-js""js-sha256"。但是,我无法确定解决方案。

    String generatedPassword = null;
    String **SALTKEY** = 'mysecret';
    String inputPassword = 'mypassword';
    try {

         MessageDigest md = MessageDigest.getInstance("SHA-256");
         md.update(SALTKEY.getBytes());
         byte[] bytes = md.digest(inputPassword.getBytes());

         StringBuilder sb = new StringBuilder();
          for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
          }

      generatedPassword = sb.toString();

     } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
     }
     return generatedPassword;

【问题讨论】:

    标签: javascript java ionic-framework npm sha256


    【解决方案1】:

    是的。终于我得到了答案:)

         import { sha256, sha224 } from 'js-sha256';
         import * as forge from 'node-forge';
    
         const SALTKEY = 'mysecret'; 
         const inputPassword = 'mypassword';
    

    方法:1 // js-sha256 - NPM

         var hash = sha256.create();
         hash.update(SALTKEY);
         hash.update(inputPassword);
         console.log(hash.hex());
    

    方法 : 2 // 节点锻造 - NPM

         var md = forge.md.sha256.create();
         md.update(SALTKEY);
         md.update(inputPassword);
         console.log(md.digest().toHex());
    

    【讨论】:

      【解决方案2】:

      你可以使用crypto-js

      检查这个Working Example

      使用 npm npm i js-sha256

      安装

      https://www.npmjs.com/package/js-sha256

      import { sha256, sha224 } from 'js-sha256';
      

      然后

      sha256('Your Message to hash');
      sha224('Your Message to hash');
      
      var yourHash = sha256.create();
      yourHash.update('Message to hash');
      yourHash.hex();
      
      var yourHash2 = sha256.update('Message to hash');
      yourHash2.update('Message2 to hash');
      yourHash2.array();
      

      使用钥匙

      var hash = sha256.hmac.create('key');
      hash.update('Message to hash');
      hash.hex();
      

      【讨论】:

      • 感谢您的回复和宝贵的时间。你能告诉我上面的解决方案“我怎么能包括 SALTKEY 和 PASSWORD”?
      • npmjs.com/package/js-sha256 。 ()var hash = sha256.hmac.create('key'); hash.update('要散列的消息'); hash.hex();
      猜你喜欢
      • 2015-08-21
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 2014-08-29
      • 2017-01-01
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多