OpenNMS密码加密方式及PHP调用REST API创建用户的加密方法咨询
Great question! Let's break this down into two clear parts to address both your queries:
OpenNMS User Password Storage Encryption
In OpenNMS 17.0.0 (the version referenced in your docs), user passwords are stored using MD5-Crypt—a salted MD5 hashing algorithm following the traditional Unix crypt format.
Here's what you need to know about the storage format:
- The stored password string starts with
$1$, followed by an 8-character random salt, then another$, and finally the hashed password value. For example:$1$abc123xy$abcdefghijklmnopqrstuvwxyz123456 - The random salt ensures that even identical plaintext passwords will generate unique hashes, preventing rainbow table attacks.
PHP Implementation for OpenNMS REST API User Creation
To create a user via the REST API that can log in successfully, you need to generate a password hash that matches OpenNMS's MD5-Crypt requirement. Here's how to do it in PHP:
- Generate the salted MD5-Crypt hash
Use PHP's built-incrypt()function with theCRYPT_MD5type (which produces the exact format OpenNMS expects). Here's a working code snippet:
$plainTextPassword = "your-desired-password"; // Generate an 8-character random salt (meets MD5-Crypt's salt requirements) $salt = substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), 0, 8); // Create the MD5-Crypt hash $hashedPassword = crypt($plainTextPassword, '$1$' . $salt . '$');
- Send the hash via the REST API
When making your POST request to the OpenNMS user creation endpoint, include the generated$hashedPasswordas thepasswordfield in your JSON payload. Ensure your request sets theContent-Type: application/jsonheader. A sample payload looks like this:
{ "user": { "username": "new-opennms-user", "password": "$1$abc123xy$abcdefghijklmnopqrstuvwxyz123456", "fullName": "New OpenNMS User", "email": "user@example.com", "userRoles": ["ROLE_USER"] } }
Quick note: Most PHP environments support CRYPT_MD5 by default. You can verify support by checking if the CRYPT_MD5 constant exists and has a value of 1.
内容的提问来源于stack exchange,提问作者Vidhi
相关产品推荐
相关产品推荐

