前端实现MD5加密,源码

绝大部分系统的开发都是从登录开始的,登录就回涉及到密码,就会考虑到安全性。常用的加密算法MD5,附上源码,方便再开发中使用。

function MD5(instring){
   
   
     var hexcase = 0;   /* hex output format. 0 - lowercase; 1 - uppercase        */
     var b64pad  = "";  /* base-64 pad character. "=" for strict RFC compliance   */

     /*
      * These are the functions you'll usually want to call
      * They take string arguments and return either hex or base-64 encoded strings
      */
     function hex_md5(s)    {
   
    return rstr2hex(rstr_md5(str2rstr_utf8(s))); }
     function b64_md5(s)    {
   
    return rstr2b64(rstr_md5(str2rstr_utf8(s))); }
     function any_md5(s, e) {
   
    return rstr2any(rstr_md5(str2rstr_utf8(s)), e); }
     function hex_hmac_md5(k, d)
       {
   
    return rstr2hex(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
     function b64_hmac_md5(k, d)
       {
   
    return rstr2b64(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d))); }
     function any_hmac_md5(k, d, e)
       {
   
    return rstr2any(rstr_hmac_md5(str2rstr_utf8(k), str2rstr_utf8(d)), e); }

     /*
      * Perform a simple self-test to see if the VM is working
      */
     function md5_vm_test()
     {
   
   
       return hex_md5("abc").toLowerCase() == "900150983cd24fb0d6963f7d28e17f72";
     }

     /*
      * Calculate the MD5 of a raw string
      */
     function rstr_md5(s)
     {
   
   
       return binl2rstr(binl_md5(rstr2binl(s), s.length * 8));
     }

     /*
      * Calculate the HMAC-MD5, of a key and some data (raw strings)
      */
     function rstr_hmac_md5(key, data)
     {
   
   
       var bkey = rstr2binl(key);
       if(bkey.length > 16) bkey = binl_md5(bkey, key.length * 8);

       var ipad = Array(16), opad = Array(16);
       for(var i = 0; i < 16; i++)
       {
   
   
         ipad[i] = bkey[i] ^ 0x36363636;
         opad[i] = bkey[i] ^ 0x5C5C5C5C;
       }

       var hash = binl_md5(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
       return binl2rstr(binl_md5(opad.concat(hash), 512 + 128));
     }

     /*
      * Convert a raw string to a hex string
      */
     function rstr2hex(input)
     {
   
   
       try {
   
    hexcase } catch(e) {
   
    hexcase=0; }
       var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
       var output = "";
       var x;
       for(var i = 0; i < input.length; i++)
       {
   
   
         x = input.charCodeAt(i);
         output += hex_tab.charAt((x >>> 4) & 0x0F)
                +  hex_tab.charAt( x        & 0x0F);
       }
       return output;
     }

     /*
      * Convert a raw string to a base-64 string
      */
     function rstr2b64(input)
     {
   
   
       try {
   
    b64pad } catch(e) {
   
    b64pad=''; }
       var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
       var output = "";
       var len = input.length;
       for(var i = 0; i < len; i += 3)
       {
   
   
         var triplet = (input.charCodeAt(i) << 16)
                     | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
                     | (i + 2 < len ? input.charCodeAt(i+2)      : 0);
         for(var j = 0; j < 4; j++)
         {
   
   
           if(i * 8 + j * 6 > input.length * 8) output += b64pad;
           else output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
         }
       }
       return output;
     }

     /*
      * Convert a raw string to an arbitrary string encoding
      */
     function rstr2any
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值