简体   繁体   English

为LDAP用户设置已经哈希的密码(使用Java)

[英]Set already hashed password for LDAP users (with Java)

I try to migrate a legacy application (which does its own user management) to LDAP. 我尝试将遗留应用程序(它自己的用户管理)迁移到LDAP。 The legacy application stores its users in a database table with hashed passwords. 旧应用程序将其用户存储在具有散列密码的数据库表中。 I know the hashing algorithm (SSHA-256) as well as the salt (the username) and I'm able to recreate the hashes with a couple of lines of code (when I know the password, eg for one of the test users). 我知道散列算法(SSHA-256)以及salt(用户名),我能够用几行代码重新创建散列(当我知道密码时,例如对于其中一个测试用户) 。

This is how the hashes have been created: 这就是哈希的创建方式:

public static String hash(String password, String salt) throws Exception {

    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
    String text = password + "{" + salt + "}";
    messageDigest.update(text.getBytes("UTF-8"));
    byte[] digest = messageDigest.digest();
    StringBuilder stringBuilder = new StringBuilder(digest.length * 2);
    for(byte b: digest)
        stringBuilder.append(String.format("%02x", b & 0xff));
    return stringBuilder.toString();
}

The result of this method is stored in the legacy database. 此方法的结果存储在旧数据库中。 I want to use this password hash for the users in the LDAP, too (otherwise, every user would have to create a new password after the migration). 我也想为LDAP中的用户使用此密码哈希(否则,每个用户必须在迁移后创建新密码)。 I tried the following to set the password in LDAP: 我尝试了以下在LDAP中设置密码:

Attribute attribute = new BasicAttribute("userpassword", someHashFromTheMethodAbove);
ModificationItem[] modifications = new ModificationItem[1];
modifications[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attribute);
context.modifyAttributes("uid=testuser,ou=User,dc=users,dc=de", modifications);

The actual setting of the password works, I can see that the password of the has been changed. 密码的实际设置有效,我可以看到密码已被更改。 However, I am unable to authenticate with the new password. 但是,我无法使用新密码进行身份验证。

在此输入图像描述

In most of the LDAP servers, the SSHA hashing scheme is based on SHA1 and not SHA-256. 在大多数LDAP服务器中,SSHA散列方案基于SHA1而不是SHA-256。 You might want to try with a {SSHA256} prefix. 您可能想尝试使用{SSHA256}前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM