Hi guys,
I'm having difficulties to get the same string in Javascript and I'm thinking that I'm doing something wrong...
Java code:
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.GregorianCalendar;
import sun.misc.BASE64Encoder;
private static String getBase64Code(String input) throws
UnsupportedEncodingException, NoSuchAlgorithmException {
String base64 = "";
byte[] txt = input.getBytes("UTF8");
byte[] text = new byte[txt.length+3];
text[0] = (byte)239;
text[1] = (byte)187;
text[2] = (byte)191;
for(int i=0; i<txt.length; i++)
text[i+3] = txt[i];
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(text);
byte digest[] = md.digest();
BASE64Encoder encoder = new BASE64Encoder();
base64 = encoder.encode(digest);
return base64;
}
I'm trying this using Paul's MD5 script as well Farhadi Base 64 Encode script
but my tests fail completely :(
my code:
function CalculateCredentialsSecret(type, user, pwd) {
var days = days_between(new Date(), new Date(2000, 1, 1));
var str = type.toUpperCase() + user.toUpperCase() + pwd.toUpperCase() + days;
var padding_data = String.fromCharCode(239) +
String.fromCharCode(187) +
String.fromCharCode(191);
var md5 = hex_md5(padding_data + str);
var b64 = base64Encode(md5);
return encodeURIComponent(b64);
}
Does anyone know how can I convert this Java method into a Javascript one?
Thank you
Tests (for today (29-09-2010), 3740 days after January 1st, 2000)
var secret = CalculateCredentialsSecret('AAA', 'BBB', 'CCC');
// secret SHOULD be: S3GYAfGWlmrhuoNsIJF94w==