Re:Creating a SHA Password for LDAP

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Re:Creating a SHA Password for LDAP

Angel Q
Hi Andrew,

Finaly I have found the solution... not sure why it didn't work, but here is the code to SHA a password for LDAP ApacheDS

import org.apache.commons.codec.binary.Base64;

            String rawPass = "original psw";
            MessageDigest sha;  
            sha = MessageDigest.getInstance("SHA");
            byte[] hash = sha.digest();
            String pswSHA = "{SHA}" + new String(Base64.encodeBase64(hash));

Thanks a lot !!

----- Mensaje original ----
De: Andrew Petro <[hidden email]>
Para: [hidden email]
CC: [hidden email]
Enviado: martes, 4 de diciembre, 2007 20:56:37
Asunto: Re: Creating a SHA Password for LDAP


Among the channels in Toro (free and open source software for use with uPortal derived from the Academus product) is a channel for changing one's LDAP-stored password.

You may find this additional code example helpful.


Angel Q wrote:
Hello Everyone,

I think this might not be directly relatied to this list, but I know someone will know how to help me :)

I am building a class to add users to my LDAP and I am not managing to SHA the password correctly.

These are the 2 options I have tryed:
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] raw = md.digest();
            BASE64Encoder encoder = new BASE64Encoder();          
            String SHAPassword = encoder.encode(raw);

and the second option I took directly from the CAS code
    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
   public String encode(final String password) {
        if (password == null) {
            return null;

        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");

            final byte[] digest = messageDigest.digest();

            return getFormattedText(digest);
        } catch (NoSuchAlgorithmException e) {
            throw new SecurityException(e);
    private String getFormattedText(byte[] bytes) {
        final StringBuilder buf = new StringBuilder(bytes.length * 2);

        for (int j = 0; j < bytes.length; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        return buf.toString();

/************* END OF CODE ************/

Thank you in advanced for your help,


¿Chef por primera vez? - Sé un mejor Cocinillas.
Entra en Yahoo! Respuestas.

Yale CAS mailing list
[hidden email]

¿Chef por primera vez? - Sé un mejor Cocinillas.
Entra en Yahoo! Respuestas.

You are currently subscribed to [hidden email] as: [hidden email]
To unsubscribe, change settings or access archives, see