-
1. Re: sha1 hashing
jleech Apr 7, 2003 1:10 PM (in response to amirbekyan)Java has a SHA implementation, but typically if you see a SHA'd password (e.g. in LDAP) it is shown Mime-encoded. The netscape LDAP api has a mime-encoder-decoder in it. Here is how I do it:
import netscape.ldap.util.ByteBuf;
import netscape.ldap.util.MimeBase64Decoder;
import netscape.ldap.util.MimeBase64Encoder;
public static String SHA(String data)
throws LDAPException
{
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA");
}
catch (java.security.NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
throw new Exception("SHA algorithm not found.");
}
MimeBase64Encoder me = new MimeBase64Encoder();
md.reset();
md.update(data.getBytes());
byte[] bytes = md.digest();
ByteBuf bbin = new ByteBuf(bytes, 0, bytes.length);
ByteBuf bbout = new ByteBuf();
me.translate(bbin, bbout);
return "{SHA}" + bbout.toString();
}