Complete example of JavaScript-implemented SHA-1 encryption algorithm _ javascript tips-js tutorial

Source: Internet
Author: User
Tags sha1
This article mainly introduces the SHA-1 encryption algorithm implemented by JavaScript, and analyzes the implementation skills of the SHA-1 encryption algorithm in the form of a complete example, for more information about the SHA-1 encryption algorithm implemented by JavaScript, see the example in this article. We will share this with you for your reference. The details are as follows:

/**** Secure Hash Algorithm (SHA1)* http://www.webtoolkit.info/***/function SHA1 (msg) {  function rotate_left(n,s) {    var t4 = ( n<>>(32-s));    return t4;  };  function lsb_hex(val) {    var str="";    var i;    var vh;    var vl;    for( i=0; i<=6; i+=2 ) {      vh = (val>>>(i*4+4))&0x0f;      vl = (val>>>(i*4))&0x0f;      str += vh.toString(16) + vl.toString(16);    }    return str;  };  function cvt_hex(val) {    var str="";    var i;    var v;    for( i=7; i>=0; i-- ) {      v = (val>>>(i*4))&0x0f;      str += v.toString(16);    }    return str;  };  function Utf8Encode(string) {    string = string.replace(/\r\n/g,"\n");    var utftext = "";    for (var n = 0; n < string.length; n++) {      var c = string.charCodeAt(n);      if (c < 128) {        utftext += String.fromCharCode(c);      }      else if((c > 127) && (c < 2048)) {        utftext += String.fromCharCode((c >> 6) | 192);        utftext += String.fromCharCode((c & 63) | 128);      }      else {        utftext += String.fromCharCode((c >> 12) | 224);        utftext += String.fromCharCode(((c >> 6) & 63) | 128);        utftext += String.fromCharCode((c & 63) | 128);      }    }    return utftext;  };  var blockstart;  var i, j;  var W = new Array(80);  var H0 = 0x67452301;  var H1 = 0xEFCDAB89;  var H2 = 0x98BADCFE;  var H3 = 0x10325476;  var H4 = 0xC3D2E1F0;  var A, B, C, D, E;  var temp;  msg = Utf8Encode(msg);  var msg_len = msg.length;  var word_array = new Array();  for( i=0; i
  
   >>29 );  word_array.push( (msg_len<<3)&0x0ffffffff );  for ( blockstart=0; blockstart
   
    

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.