JavaScript implements base64 MD5 SHA1 password encryption _javascript techniques

Source: Internet
Author: User
Tags base64 bitwise hmac lowercase md5 encryption rfc sha1 sha1 encryption


1, Base64 encryption



The Base64.js file is introduced into the page, and the method is called:


<! DOCTYPE html>


2, MD5 encryption



Referencing the Md5.js file in the page, calling the method


<! DOCTYPE html>


3, SHA1 encryption



It's said to be the safest encryption.



The page is introduced with Sha1.js, and the method is called


<! DOCTYPE html>


Encrypted download address included in the article:



The text contains the JS file package download Address



Here's a little bit more knowledge: The very popular JavaScript MD5 encryption



On the internet to see the JavaScript MD5 encryption, look better, on the excerpt, for reference



 <HTML> <HEAD> <meta http-equiv= ' content-type ' content= ' text/html; CHARSET=GB ' > <TITLE> very popular JS's MD encryption method </TITLE> </HEAD> <body > <input id=test value=webasp&
 Gt <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 he
 X 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, if the VM is working/function md_vm_test () {return HEX_MD ("abc") = "cdf
 Bdfdef ";  } * * 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); functio N 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, 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 is 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 silen
 Tly 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?
 "ABCDEF": "ABCDEF";
 var str = ""; for (var i =; I < Binarray.length *; i++) {
str = Hex_tab.charat (BINARRAY[I&GT;&GT;] >> ((i%) *+)) & XF) + Hex_tab.charat ((binarray[i>>] &G
 T;> ((i%) *)) & XF);
return str; } * * Convert an array of Little-endian words to a base-string/function Binlb (binarray) {var tab = "Abcdefghijklm
 nopqrstuvwxyzabcdefghijklmnopqrstuvwxyz+/";
 var str = ""; for (var i =; I < binarray.length *; i + =) {
var triplet = ((binarray[i >>] >> * (i%)) & Xff) & lt;<) | (((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><HTML>
 <HEAD>
 <META http-equiv='Content-Type' content='text/html; charset=gb'>
 <TITLE> Very popular JS md encryption method</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
 * to work around bug
s 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 ? "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>



The above is a small set of JavaScript to introduce the implementation of Base64 MD5 SHA1 password encryption, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.