Bonjour,

J'ai écrit une méthode permettant d'encrypter mon mot de passe (MD5) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
public String MD5Password (String credentials) throws NoSuchAlgorithmException  {
    /*
    * Encode la chaîne passée en paramètre avec l'algorithme MD5
    *
    * @param credentials : chaîne à encoder
    * @return la valeur (string) hexadécimale sur 32 bits
    */
 
    byte[] uniqueKey = credentials.getBytes();
    byte[] hash = null;
    try {
      hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
    }
    catch ( NoSuchAlgorithmException e ) {
      throw new Error ("Error : Encryptage du mot de passe! (MD5Password)", e );
    }
    StringBuffer hashString = new StringBuffer();
    for (int i=0; i<hash.length; i++) {
      String hex = Integer.toHexString(hash[i]);
      if (hex.length() == 1) {
        hashString.append('0');
        hashString.append(hex.charAt(hex.length()-1));
      }
      else {
        hashString.append(hex.substring(hex.length()-2));
      }
    }
    return hashString.toString();
  }
Le but est de modifier mon mot de passe dans LDAP.

J'ai écrit une méthode permettant de modifier un attribut tel que le mail. Est-ce que je peux aussi l'utiliser pour modifier mon mot de passe ou y'a-t'il une astuce ?

Voilà ma méthode pour la modification de l'attribut :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 
public void modAttr (String dn, String attribut, String valeur) throws ch.esnig.ldap.ClientLDAPException {
    if (! getAttr (attribut, dn)) {
      System.out.println ("Cet attribut ne contient pas de valeurs!");
    }
    else {
      try {
        // Sauvegarde les attributs d'origine
        Attributes originaux = ctx.getAttributes (dn, new String[] {attribut});
 
        // Spécifie le changement à effectuer
        // Crée un tableau de modifications à effectuer
        ModificationItem[] mods = new ModificationItem[1];
 
        // Remplace l'attribut "mail" avec une nouvelle valeur
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute (attribut, valeur));
 
        // Validation de la modification
        ctx.modifyAttributes (dn, mods);
 
        // Affichage du nouvel attribut
        System.out.println ("**** Nouvel attribut ****");
        printAttributes (dn);
      }
      catch ( javax.naming.NamingException e ) {
        throw new ch.esnig.ldap.ClientLDAPException("Error : Modification d'un attribut! (modAttr)", e );
      }
    }
  }
Ce serait sympa si quelqu'un pouvait m'aider.

Merci d'avance !