简体   繁体   中英

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

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));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM