JavaScript SHA1 Encryption algorithm implements detailed code _javascript techniques

Source: Internet
Author: User
Tags sha1 sha1 encryption

This article examples for you to introduce the JavaScript SHA1 encryption algorithm, for your reference, the specific contents are as follows

* * A JavaScript implementation of the Secure Hash algorithm, SHA-1, as defined * in FIPS 180-1 * Version 2.2 Copyrig
 HT Paul Johnston 2000-2009. * Other Contributors:greg Holt, Andrew Kepert, Ydnar, lostinet * Distributed under the BSD License * http://pajhome
 . ORG.UK/CRYPT/MD5 for details. * http://www.sharejs.com/* * Configurable variables.
 You could need to tweak this compatible with * server-side, but the defaults work in most. * * 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 argument s and return either HEX or BASE-64 encoded strings/function HEX_SHA1 (s) {return Rstr2hex (RSTR_SHA1 (s)) ); function B64_sha1 (s) {return rstr2b64 (RSTR_SHA1 (Str2rstr_utf8 (s)))} function Any_sha1 (S. e) {return Rstr2any (rstr_s HA1 (Str2rstr_utf8 (s)), E); } functiOn HEX_HMAC_SHA1 (k, D) {return Rstr2hex (Rstr_hmac_sha1 (Str2rstr_utf8 (k), Str2rstr_utf8 (d)));} function B64_hmac_sha1 ( K, D) {return rstr2b64 (Rstr_hmac_sha1 (Str2rstr_utf8 (k), Str2rstr_utf8 (d)));} function Any_hmac_sha1 (k, D, E) {return Rstr2any (Rstr_hmac_sha1 (Str2rstr_utf8 (k), Str2rstr_utf8 (d)), E); } * * Perform a simple self-test to, if the VM is working/function sha1_vm_test () {return HEX_SHA1 ("abc"). ToL
Owercase () = = "a9993e364706816aba3e25717850c26c9cd0d89d"; /* * Calculate the SHA1 of a raw string/function Rstr_sha1 (s) {return binb2rstr BINB_SHA1 (RSTR2BINB (s), S.lengt
H * 8)); } * * Calculate the HMAC-SHA1 of a key and some data (raw strings)/function Rstr_hmac_sha1 (key, data) {var bkey
 = RSTR2BINB (key);
 
 if (Bkey.length >) bkey = BINB_SHA1 (Bkey, key.length * 8);
 var ipad = array, Opad = Array (16);
  for (var i = 0; i < i++) {Ipad[i] = bkey[i] ^ 0x36363636;
 Opad[i] = bkey[i] ^ 0x5c5c5c5c; var hash = binb_sha1 (ipad. Concat (data), RSTR2BINB + data.length * 8);
Return Binb2rstr (BINB_SHA1 (Opad.concat (hash), 512 + 160));
 /* 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 (input, encoding) {var divisor = enc
 Oding.length;
 var remainders = Array ();
 
 var i, q, x, quotient;  /* Convert to an array of 16-bit Big-endian values, forming the dividend/var dividend = Array (Math.ceil (Input.length/
 2));  for (i = 0; i < dividend.length i++) {Dividend[i] = (Input.charcodeat (i * 2) << 8) | input.charcodeat (i * 2 +
 1); /* * Repeatedly perform a long division. The binary array forms the dividend, * the length of the encoding is the divisor. Once computed, the quotient * forms the dividend for the next step.
  We Stop the dividend is zero.
  * All remainders are stored for later use.
  */while (Dividend.length > 0) {quotient = Array ();
  x = 0; for (i = 0; i < dividend.length; i++) {x = (x <<) + dividend[i];
   Q = Math.floor (x/divisor);
   x = q * DIVISOR;
  if (Quotient.length > 0 | | | q > 0) quotient[quotient.length] = q;
  } Remainders[remainders.length] = x;
 Dividend = quotient;
 } * Convert the remainders to the output string */var output = "";
 
 for (i = remainders.length-1 i >= 0; i--) output + = Encoding.charat (remainders[i)); /* Append Leading Zero equivalents * * var full_length = Math.ceil (Input.length * 8/(Math.log encoding
 
 . length)/Math.log (2)) for (i = Output.length i < full_length; i++) output = encoding[0] + output;
return output;
 } * * Encode a string as Utf-8.
 * For efficiency, this assumes the input is valid utf-16.
 * * Function Str2rstr_utf8 (input) {var output = "";
 var i =-1;
 
 var x, y;
  while (++i < input.length) {/* Decode utf-16 Surrogate pairs/x = Input.charcodeat (i); y = i + 1 < input.length?
  Input.charcodeat (i + 1): 0; if (0xd800 <= x && x <= 0xDBFF && 0xdc00 <= y && y <= 0xDFFF) {x = 0x10000 + (x & 0x0
   3FF) << + (Y & 0x03ff);
  i++;
  }/* Encode output as Utf-8 */if (x <= 0x7F) output + + = String.fromCharCode (x); else if (x <= 0x7ff) Output + = String.fromCharCode (0xc0 | ((x >>> 6) & 0x1F), 0x80 |
  (x & 0x3F)); else if (x <= 0xFFFF) Output + = String.fromCharCode (0xe0 | ((x >>>) & 0x0f), 0x80 | ((x >>> 6) & 0x3F), 0x80 |
  (x & 0x3F)); else if (x <= 0x1fffff) Output + = String.fromCharCode (0xF0 | ((x >>>%) & 0x07), 0x80 | ((x >>>) & 0x3F), 0x80 | ((x >>> 6) & 0x3F), 0x80 |
 (x & 0x3F));
return output;
 } * * Encode a string as utf-16 */function Str2rstr_utf16le (input) {var output = ""; For (Var i = 0; i < input.length; i++) output + + String.fromCharCode (input.charcodeat (i) & 0xFF, (Input.charcodeat (i) >>&gt ;
 8) & 0xFF);
return output;
 function Str2rstr_utf16be (input) {var output = "";
                  for (var i = 0; i < input.length i++) output + + String.fromCharCode ((input.charcodeat (i) >>> 8) & 0xFF,
 Input.charcodeat (i) & 0xFF);
return output; }/* Convert a raw string to an array of Big-endian words * Characters >255 have their high-byte silently ignored
 .
 */function Rstr2binb (input) {var output = Array (input.length >> 2);
 for (var i = 0; i < output.length i++) output[i] = 0; for (var i = 0; i < input.length * 8; i + + 8) output[i>>5] |= (Input.charcodeat (I/8) & 0xFF) << (24
 -I% 32);
return output;
 }/* Convert an array of Big-endian words to a string/function binb2rstr (input) {var output = ""; for (var i = 0; i < Input.length *+ + 8 Output + = String.fromCharCode ((input[i>>5) >>> (24-i%)) & 0xFF);
return output; } * * Calculate the SHA-1 of an array of Big-endian words, and a bit length/function binb_sha1 (x, len) {/* Appen
 D padding * * X[len >> 5] |= 0x80 << (24-len% 32);
 
 x[(len + >> 9) << 4) = Len;
 var w = Array (80);
 var a = 1732584193;
 var b =-271733879;
 var c =-1732584194;
 var d = 271733878;
 
 var e =-1009589776;
  for (var i = 0; i < x.length i + =) {var Olda = A;
  var oldb = b;
  var oldc = c;
  var oldd = D;
 
  var olde = e;
   for (var j = 0; J <; J + +) {if (J <) W[j] = X[i + j];
   else w[j] = Bit_rol (w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
   var t = safe_add (Safe_add (Bit_rol (A, 5), sha1_ft (J, B, C, D)), Safe_add (Safe_add (E, w[j)), Sha1_kt (j)));
   e = D;
   D = C;
   c = Bit_rol (b, 30);
   b = A;
  A = t;
  A = Safe_add (A, Olda);
  b = Safe_add (b, oldb); c = Safe_add (c, OLDC);
  D = Safe_add (d, OLDD);
 E = Safe_add (e, Olde);
 
Return Array (A, B, C, D, E); } * * Perform the appropriate triplet combination function for the current * iteration * function sha1_ft (t, B, C, D) {if (T <) return (b & c) |
 ((~b) & D);
 if (T <) return b ^ C ^ D; if (T < a) return (b & c) | (b & D) |
 (C & D);
Return b ^ C ^ D; } * * Determine the appropriate additive constant for the current iteration/function Sha1_kt (t) {return (T < 20)? 1518500249: (T < 40)? 1859775393: (T < 60)?
-1894007588:-899497514; * * * Add integers, wrapping at 2^32.
 This is uses 16-bit operations internally * To work around bugs in some JS interpreters.
 */function Safe_add (x, y) {var LSW = (x & 0xFFFF) + (Y & 0xFFFF);
 var MSW = (x >>) + (y >>) + (LSW >> 16); Return (MSW << 16) |
(LSW & 0xFFFF);
 /* * Bitwise rotate a 32-bit number to the left. */function Bit_rol (num, CNT) {return (num << cnt) |
(Num >>> (32-cnt));

 }

The above is the full content of this article, I hope to help you, I hope you will continue to pay attention to the latest content of the cloud-dwelling community.

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.