我想要加密ssha中的密码。有没有办法做到这一点?我发现了这个,但它在沙城。
private String encrypt(final String plaintext) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage());
}
try {
md.update(plaintext.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage());
}
byte raw[] = md.digest();
String hash = (new BASE64Encoder()).encode(raw);
return hash;
}发布于 2017-02-28 00:31:30
OpenLDAP有一个命令行工具来生成SSHA密码:
# slappasswd -h {SSHA} -s test123
{SSHA}FOJDrfbduQe6mWrz70NKVr3uEBPoUBf9此代码将生成带有OpenLDAP可以使用的输出的加盐的SHA-1密码:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
private static final int SALT_LENGTH = 4;
public static String generateSSHA(byte[] password)
throws NoSuchAlgorithmException {
SecureRandom secureRandom = new SecureRandom();
byte[] salt = new byte[SALT_LENGTH];
secureRandom.nextBytes(salt);
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(password);
crypt.update(salt);
byte[] hash = crypt.digest();
byte[] hashPlusSalt = new byte[hash.length + salt.length];
System.arraycopy(hash, 0, hashPlusSalt, 0, hash.length);
System.arraycopy(salt, 0, hashPlusSalt, hash.length, salt.length);
return new StringBuilder().append("{SSHA}")
.append(Base64.getEncoder().encodeToString(hashPlusSalt))
.toString();
}发布于 2016-01-28 23:49:40
SSHA只是一颗种子的SHA。在标准java平台中,没有这样做的可能的解决方案(https://stackoverflow.com/a/3983415/1976843)。您需要实现自己的库或使用第三方库。我知道在spring security中有LdapPasswordEncoder
https://stackoverflow.com/questions/35065529
复制相似问题