>  기사  >  웹 프론트엔드  >  JavaScript는 base64 md5 sha1 비밀번호 암호화_javascript 기술을 구현합니다.

JavaScript는 base64 md5 sha1 비밀번호 암호화_javascript 기술을 구현합니다.

WBOY
WBOY원래의
2016-05-16 15:39:531563검색

1. Base64 암호화

base64.js 파일을 페이지에 삽입하고 호출 방법은 다음과 같습니다.

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>base64加密</title>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript"> 
    var b = new Base64(); 
    var str = b.encode("admin:admin"); 
    alert("base64 encode:" + str); 
     //解密
    str = b.decode(str); 
    alert("base64 decode:" + str); 
</script> 
</head>
<body>
</body>
</html>

2. md5 암호화

페이지에서 md5.js 파일을 참조하기 위한 호출 방법은

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>md5加密</title>
<script type="text/ecmascript" src="md5.js"></script>
<script type="text/javascript"> 
 var hash = hex_md5("123dafd");
  alert(hash)
</script> 
</head>
<body>
</body>
</html>

3. sha1 암호화

이것이 가장 안전한 암호화라고 합니다

페이지에 sha1.js를 도입하면 호출 방법은

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>sha1加密</title>
<script type="text/ecmascript" src="sha1.js"></script>
<script type="text/javascript">
 var sha = hex_sha1('mima123465')
  alert(sha)  
</script> 
</head>
<body>
</body>
</html>

매우 인기 있는 javascript md5 암호화

인터넷에서 본 자바스크립트의 MD5 암호화가 비교적 잘되어 참고용으로 발췌했습니다

 <HTML>
 <HEAD>
 <META http-equiv='Content-Type' content='text/html; charset=gb'>
 <TITLE>非常流行的JS的md加密办法</TITLE>
 </HEAD>
 <BODY >
 <input id=test value=webasp>
 <input type=button value=md onclick="alert(hex_md(test.value))">
 <script>
 var hexcase = ; /* hex output format. - lowercase; - uppercase    */
 var bpad = ""; /* base- pad character. "=" for strict RFC compliance  */
 var chrsz  = ; /* bits per input character. - ASCII; - Unicode   */
 /*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base- encoded strings
 */
 function hex_md(s){ return binlhex(core_md(strbinl(s), s.length * chrsz));}
 function b_md(s){ return binlb(core_md(strbinl(s), s.length * chrsz));}
 function hex_hmac_md(key, data) { return binlhex(core_hmac_md(key, data)); }
 function b_hmac_md(key, data) { return binlb(core_hmac_md(key, data)); }
 /* Backwards compatibility - same as hex_md() */
 function calcMD(s){ return binlhex(core_md(strbinl(s), s.length * chrsz));}
 /* 
 * Perform a simple self-test to see if the VM is working 
 */
 function md_vm_test()
 {
  return hex_md("abc") == "cdfbdfdef";
 }
 /*
 * Calculate the MD of an array of little-endian words, and a bit length
 */
 function core_md(x, len)
 {
  /* append padding */
  x[len >> ] |= x << ((len) % );
  x[(((len + ) >>> ) << ) + ] = len;
  var a = ;
  var b = -;
  var c = -;
  var d = ;
  for(var i = ; i < x.length; i += )
 {
  var olda = a;
  var oldb = b;
  var oldc = c;
  var oldd = d;
  a = md_ff(a, b, c, d, x[i+ ], , -);
  d = md_ff(d, a, b, c, x[i+ ], , -);
  c = md_ff(c, d, a, b, x[i+ ], , );
  b = md_ff(b, c, d, a, x[i+ ], , -);
  a = md_ff(a, b, c, d, x[i+ ], , -);
  d = md_ff(d, a, b, c, x[i+ ], , );
  c = md_ff(c, d, a, b, x[i+ ], , -);
  b = md_ff(b, c, d, a, x[i+ ], , -);
  a = md_ff(a, b, c, d, x[i+ ], , );
  d = md_ff(d, a, b, c, x[i+ ], , -);
  c = md_ff(c, d, a, b, x[i+], , -);
  b = md_ff(b, c, d, a, x[i+], , -);
  a = md_ff(a, b, c, d, x[i+], , );
  d = md_ff(d, a, b, c, x[i+], , -);
  c = md_ff(c, d, a, b, x[i+], , -);
  b = md_ff(b, c, d, a, x[i+], , );
  a = md_gg(a, b, c, d, x[i+ ], , -);
  d = md_gg(d, a, b, c, x[i+ ], , -);
  c = md_gg(c, d, a, b, x[i+], , );
  b = md_gg(b, c, d, a, x[i+ ], , -);
  a = md_gg(a, b, c, d, x[i+ ], , -);
  d = md_gg(d, a, b, c, x[i+], , );
  c = md_gg(c, d, a, b, x[i+], , -);
  b = md_gg(b, c, d, a, x[i+ ], , -);
  a = md_gg(a, b, c, d, x[i+ ], , );
  d = md_gg(d, a, b, c, x[i+], , -);
  c = md_gg(c, d, a, b, x[i+ ], , -);
  b = md_gg(b, c, d, a, x[i+ ], , );
  a = md_gg(a, b, c, d, x[i+], , -);
  d = md_gg(d, a, b, c, x[i+ ], , -);
  c = md_gg(c, d, a, b, x[i+ ], , );
  b = md_gg(b, c, d, a, x[i+], , -);
  a = md_hh(a, b, c, d, x[i+ ], , -);
  d = md_hh(d, a, b, c, x[i+ ], , -);
  c = md_hh(c, d, a, b, x[i+], , );
  b = md_hh(b, c, d, a, x[i+], , -);
  a = md_hh(a, b, c, d, x[i+ ], , -);
  d = md_hh(d, a, b, c, x[i+ ], , );
  c = md_hh(c, d, a, b, x[i+ ], , -);
  b = md_hh(b, c, d, a, x[i+], , -);
  a = md_hh(a, b, c, d, x[i+], , );
  d = md_hh(d, a, b, c, x[i+ ], , -);
  c = md_hh(c, d, a, b, x[i+ ], , -);
  b = md_hh(b, c, d, a, x[i+ ], , );
  a = md_hh(a, b, c, d, x[i+ ], , -);
  d = md_hh(d, a, b, c, x[i+], , -);
  c = md_hh(c, d, a, b, x[i+], , );
  b = md_hh(b, c, d, a, x[i+ ], , -);
  a = md_ii(a, b, c, d, x[i+ ], , -);
  d = md_ii(d, a, b, c, x[i+ ], , );
  c = md_ii(c, d, a, b, x[i+], , -);
  b = md_ii(b, c, d, a, x[i+ ], , -);
  a = md_ii(a, b, c, d, x[i+], , );
  d = md_ii(d, a, b, c, x[i+ ], , -);
  c = md_ii(c, d, a, b, x[i+], , -);
  b = md_ii(b, c, d, a, x[i+ ], , -);
  a = md_ii(a, b, c, d, x[i+ ], , );
  d = md_ii(d, a, b, c, x[i+], , -);
  c = md_ii(c, d, a, b, x[i+ ], , -);
  b = md_ii(b, c, d, a, x[i+], , );
  a = md_ii(a, b, c, d, x[i+ ], , -);
  d = md_ii(d, a, b, c, x[i+], , -);
  c = md_ii(c, d, a, b, x[i+ ], , );
  b = md_ii(b, c, d, a, x[i+ ], , -);
  a = safe_add(a, olda);
  b = safe_add(b, oldb);
  c = safe_add(c, oldc);
  d = safe_add(d, oldd);
 }
 return Array(a, b, c, d);
}
/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md_cmn(q, a, b, x, s, t)
{
 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md_ff(a, b, c, d, x, s, t)
{
 return md_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md_gg(a, b, c, d, x, s, t)
{
 return md_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md_hh(a, b, c, d, x, s, t)
{
 return md_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md_ii(a, b, c, d, x, s, t)
{
 return md_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
/*
 * Calculate the HMAC-MD, of a key and some data
 */
function core_hmac_md(key, data)
{
 var bkey = strbinl(key);
 if(bkey.length > ) bkey = core_md(bkey, key.length * chrsz);
 var ipad = Array(), opad = Array();
 for(var i = ; i < ; i++) 
 {
  ipad[i] = bkey[i] ^ x;
  opad[i] = bkey[i] ^ xCCCC;
 }
 var hash = core_md(ipad.concat(strbinl(data)), + data.length * chrsz);
 return core_md(opad.concat(hash), + );
}
/*
 * Add integers, wrapping at ^. This uses -bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
 var lsw = (x & xFFFF) + (y & xFFFF);
 var msw = (x >> ) + (y >> ) + (lsw >> );
 return (msw << ) | (lsw & xFFFF);
}
/*
 * Bitwise rotate a -bit number to the left.
 */
function bit_rol(num, cnt)
{
 return (num << cnt) | (num >>> ( - cnt));
}
/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters > have their hi-byte silently ignored.
 */
function strbinl(str)
{
 var bin = Array();
 var mask = ( << chrsz) - ;
 for(var i = ; i < str.length * chrsz; i += chrsz)
  bin[i>>] |= (str.charCodeAt(i / chrsz) & mask) << (i%);
 return bin;
}
/*
 * Convert an array of little-endian words to a hex string.
 */
function binlhex(binarray)
{
 var hex_tab = hexcase &#63; "ABCDEF" : "abcdef";
 var str = "";
 for(var i = ; i < binarray.length * ; i++)
 {
  str += hex_tab.charAt((binarray[i>>] >> ((i%)*+)) & xF) +
      hex_tab.charAt((binarray[i>>] >> ((i%)* )) & xF);
 }
 return str;
}
/*
 * Convert an array of little-endian words to a base- string
 */
function binlb(binarray)
{
 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
 var str = "";
 for(var i = ; i < binarray.length * ; i += )
 {
  var triplet = (((binarray[i  >> ] >> * ( i  %)) & xFF) << )
        | (((binarray[i+ >> ] >> * ((i+)%)) & xFF) << )
        | ((binarray[i+ >> ] >> * ((i+)%)) & xFF);
  for(var j = ; j < ; j++)
  {
   if(i * + j * > binarray.length * ) str += bpad;
   else str += tab.charAt((triplet >> *(-j)) & xF);
  }
 }
 return str;
}
</script> 
</BODY></HTML>

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.