I am creating a facility tracking application at work which consists of a central server with Rails for the webfrontend and Java for the standalone application on a PDA (Motorola Symbol MC75).
The password hash for the login on the PDA is the same as on the server so I had to figure out how restful-authentication creates the hash. The code is written for Java 1.3 and should work from Java 1.1 on.
The REST_AUTH_SITE_KEY can be found in the server environment files (/config/environments).
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Authentication {
private static final byte[] HEX_CHAR_TABLE = { (byte) '0', (byte) '1', (byte) '2',
(byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) 'a', (byte) 'b', (byte) 'c',
(byte) 'd', (byte) 'e', (byte) 'f' };
private static String getHexString(byte[] raw) throws UnsupportedEncodingException {
byte[] hex = new byte[2 * raw.length];
int index = 0;
for (int i = 0; i < raw.length; i++) {
byte b = raw[i];
int v = b & 0xFF;
hex[index++] = HEX_CHAR_TABLE[v >>> 4];
hex[index++] = HEX_CHAR_TABLE[v & 0xF];
}
return new String(hex, "ASCII");
}
public static String createDigest(String REST_AUTH_SITE_KEY,String salt, String password){
String digest=REST_AUTH_SITE_KEY;
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA1");
for(int i=0;i<10;i++){
md.update((digest+"--"+salt+"--"+password+"--"+REST_AUTH_SITE_KEY).getBytes());
digest=getHexString(md.digest());
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return digest;
}
}