将 Java 密码哈希方法转换为 JS 实现
Translating Java password hashing method to JS implementation
我想翻译 Blynk IoT 软件中使用的 Java 代码密码散列,我可以在 Express.js 应用程序中使用。感谢您的帮助!
Java代码:
https://www.onlinegdb.com/HJe19lyFB
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Main {
public static void main(String[] args){
System.out.println(makeHash("password","mail@gmail.com"));
}
private static final String SHA_256 = "SHA-256";
private Main() {
try {
MessageDigest.getInstance(SHA_256);
} catch (NoSuchAlgorithmException e) {
}
}
public static String makeHash(String password, String salt) {
try {
MessageDigest md = MessageDigest.getInstance(SHA_256);
md.update(password.getBytes(StandardCharsets.UTF_8));
byte[] byteData = md.digest(makeHash(salt.toLowerCase()));
return Base64.getEncoder().encodeToString(byteData);
} catch (Exception e) {
//ignore, will never happen.
}
return password;
}
private static byte[] makeHash(String val) throws NoSuchAlgorithmException {
return MessageDigest.getInstance(SHA_256).digest(val.getBytes(StandardCharsets.UTF_8));
}
}
目前无效解决方案:
https://repl.it/@patryk0493/blynk-password-hashing
const btoa = require('btoa');
var util = require('util');
const KJUR = require('jsrsasign');
const password = 'password';
const email = 'mail@gmail.com';
const options = {"alg": "sha256", "prov": "cryptojs"}
makeHash = (str) => {
const md = new KJUR.crypto.MessageDigest(options);
return md.digestString(new util.TextEncoder().encode(str.toLowerCase()))
}
const md = new KJUR.crypto.MessageDigest(options);
md.updateString(new util.TextEncoder().encode(password));
const byteData = md.digest(makeHash(email.toLowerCase()));
const base64 = btoa(byteData)
console.log(base64);
您可以使用标准 Node.js crypto 模块重新实现 Java 密码散列:
const crypto = require('crypto');
const makeHash = (data) => {
const hash = crypto.createHash('sha256');
return hash.update(data, 'utf8');
}
const password = "password";
const salt = "mail@gmail.com";
const result = makeHash(password)
.update(makeHash(salt).digest())
.digest('base64')
console.log(result);
我想翻译 Blynk IoT 软件中使用的 Java 代码密码散列,我可以在 Express.js 应用程序中使用。感谢您的帮助!
Java代码: https://www.onlinegdb.com/HJe19lyFB
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class Main {
public static void main(String[] args){
System.out.println(makeHash("password","mail@gmail.com"));
}
private static final String SHA_256 = "SHA-256";
private Main() {
try {
MessageDigest.getInstance(SHA_256);
} catch (NoSuchAlgorithmException e) {
}
}
public static String makeHash(String password, String salt) {
try {
MessageDigest md = MessageDigest.getInstance(SHA_256);
md.update(password.getBytes(StandardCharsets.UTF_8));
byte[] byteData = md.digest(makeHash(salt.toLowerCase()));
return Base64.getEncoder().encodeToString(byteData);
} catch (Exception e) {
//ignore, will never happen.
}
return password;
}
private static byte[] makeHash(String val) throws NoSuchAlgorithmException {
return MessageDigest.getInstance(SHA_256).digest(val.getBytes(StandardCharsets.UTF_8));
}
}
目前无效解决方案: https://repl.it/@patryk0493/blynk-password-hashing
const btoa = require('btoa');
var util = require('util');
const KJUR = require('jsrsasign');
const password = 'password';
const email = 'mail@gmail.com';
const options = {"alg": "sha256", "prov": "cryptojs"}
makeHash = (str) => {
const md = new KJUR.crypto.MessageDigest(options);
return md.digestString(new util.TextEncoder().encode(str.toLowerCase()))
}
const md = new KJUR.crypto.MessageDigest(options);
md.updateString(new util.TextEncoder().encode(password));
const byteData = md.digest(makeHash(email.toLowerCase()));
const base64 = btoa(byteData)
console.log(base64);
您可以使用标准 Node.js crypto 模块重新实现 Java 密码散列:
const crypto = require('crypto');
const makeHash = (data) => {
const hash = crypto.createHash('sha256');
return hash.update(data, 'utf8');
}
const password = "password";
const salt = "mail@gmail.com";
const result = makeHash(password)
.update(makeHash(salt).digest())
.digest('base64')
console.log(result);