如何将 SHA-256 Java 转换为 JavaScript(带有 NPM 的 IONIC)?
How to convert SHA-256 Java to JavaScript( IONIC with NPM )?
我在 JAVA
中给出了以下代码。我需要像 IONIC Framework
一样 convert JavaScript
。我用 NPM "crypto-js"
和 "js-sha256"
尝试了 IONIC App
。但是,我无法确定解决方案。
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;
你可以使用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();
Yes.Finally 我得到了答案:)
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 // node-forge - NPM
var md = forge.md.sha256.create();
md.update(SALTKEY);
md.update(inputPassword);
console.log(md.digest().toHex());
我在 JAVA
中给出了以下代码。我需要像 IONIC Framework
一样 convert JavaScript
。我用 NPM "crypto-js"
和 "js-sha256"
尝试了 IONIC App
。但是,我无法确定解决方案。
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;
你可以使用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();
Yes.Finally 我得到了答案:)
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 // node-forge - NPM
var md = forge.md.sha256.create();
md.update(SALTKEY);
md.update(inputPassword);
console.log(md.digest().toHex());