user1076813
user1076813

Reputation: 487

How can I convert a Base64 encode of MD5 password in javascript

From this stackoverflow question: Convert 'String' to Base64 encode of MD5 'String' in c# .net how can I port this code/algorithm to frontend javascript?

From: password

To: X03MO1qnZdYdgyfeuILPmQ==

I've tried btoa etc. but yielded different results

Upvotes: 0

Views: 1393

Answers (1)

user1076813
user1076813

Reputation: 487

Suggested by Leyon of using Crypto-JS library, I've added a code to convert it to Base64 from Hex output of Crypto-JS. I think this is not the best answer but this helps me, for now.

jsfiddle

var md5 = function(value) {
  return CryptoJS.MD5(value).toString();
}

function hexToBase64(str) {
  return btoa(String.fromCharCode.apply(null,
    str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" "))
  );
}

$("input").keyup(function () {
    var value = $(this).val(),
        hash = md5(value);
    $(".test").html(hash);
    $(".base64").html(hexToBase64(hash));
});

Upvotes: 2

Related Questions