Java to JavaScript (Encryptation related)
- by balexandre
Hi guys,
I'm having dificulties 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 Paj's MD5 script as well Farhadi Base 64 Encode script
but my tests fail completly :(
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 md5 = any_md5('', str);
var b64 = base64Encode(md5);
return encodeURIComponent(b64);
}
Does anyone know how can I convert this Java method into a Javascript one?
Thank you