SHA1.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.encoding.digests.SHA1"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.encoding.digests.SHA1"] = true;
  8. dojo.provide("dojox.encoding.digests.SHA1");
  9. dojo.require("dojox.encoding.digests._base");
  10. /*
  11. * A port of Paul Johnstone's SHA1 implementation
  12. *
  13. * Version 2.1a Copyright Paul Johnston 2000 - 2002.
  14. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  15. * Distributed under the BSD License
  16. * See http://pajhome.org.uk/crypt/md5 for details.
  17. *
  18. * Dojo port by Tom Trenka
  19. */
  20. (function(){
  21. var dxd=dojox.encoding.digests;
  22. var chrsz=8, // change to 16 for unicode.
  23. mask=(1<<chrsz)-1;
  24. function R(n,c){ return (n<<c)|(n>>>(32-c)); }
  25. function FT(t,b,c,d){
  26. if(t<20){ return (b&c)|((~b)&d); }
  27. if(t<40){ return b^c^d; }
  28. if(t<60){ return (b&c)|(b&d)|(c&d); }
  29. return b^c^d;
  30. }
  31. function KT(t){ return (t<20)?1518500249:(t<40)?1859775393:(t<60)?-1894007588:-899497514; }
  32. function core(x,len){
  33. x[len>>5]|=0x80<<(24-len%32);
  34. x[((len+64>>9)<<4)+15]=len;
  35. var w=new Array(80), a=1732584193, b=-271733879, c=-1732584194, d=271733878, e=-1009589776;
  36. for(var i=0; i<x.length; i+=16){
  37. var olda=a, oldb=b, oldc=c, oldd=d, olde=e;
  38. for(var j=0;j<80;j++){
  39. if(j<16){ w[j]=x[i+j]; }
  40. else { w[j]=R(w[j-3]^w[j-8]^w[j-14]^w[j-16],1); }
  41. var t = dxd.addWords(dxd.addWords(R(a,5),FT(j,b,c,d)),dxd.addWords(dxd.addWords(e,w[j]),KT(j)));
  42. e=d; d=c; c=R(b,30); b=a; a=t;
  43. }
  44. a=dxd.addWords(a,olda);
  45. b=dxd.addWords(b,oldb);
  46. c=dxd.addWords(c,oldc);
  47. d=dxd.addWords(d,oldd);
  48. e=dxd.addWords(e,olde);
  49. }
  50. return [a, b, c, d, e];
  51. }
  52. function hmac(data, key){
  53. var wa=toWord(key);
  54. if(wa.length>16){ wa=core(wa, key.length*chrsz); }
  55. var ipad=new Array(16), opad=new Array(16);
  56. for(var i=0;i<16;i++){
  57. ipad[i]=wa[i]^0x36363636;
  58. opad[i]=wa[i]^0x5c5c5c5c;
  59. }
  60. var hash=core(ipad.concat(toWord(data)),512+data.length*chrsz);
  61. return core(opad.concat(hash), 512+160);
  62. }
  63. function toWord(s){
  64. var wa=[];
  65. for(var i=0, l=s.length*chrsz; i<l; i+=chrsz){
  66. wa[i>>5]|=(s.charCodeAt(i/chrsz)&mask)<<(32-chrsz-i%32);
  67. }
  68. return wa; // word[]
  69. }
  70. function toHex(wa){
  71. // slightly different than the common one.
  72. var h="0123456789abcdef", s=[];
  73. for(var i=0, l=wa.length*4; i<l; i++){
  74. s.push(h.charAt((wa[i>>2]>>((3-i%4)*8+4))&0xF), h.charAt((wa[i>>2]>>((3-i%4)*8))&0xF));
  75. }
  76. return s.join(""); // string
  77. }
  78. function _toString(wa){
  79. var s=[];
  80. for(var i=0, l=wa.length*32; i<l; i+=chrsz){
  81. s.push(String.fromCharCode((wa[i>>5]>>>(32-chrsz-i%32))&mask));
  82. }
  83. return s.join(""); // string
  84. }
  85. function toBase64(/* word[] */wa){
  86. // summary:
  87. // convert an array of words to base64 encoding, should be more efficient
  88. // than using dojox.encoding.base64
  89. var p="=", tab="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", s=[];
  90. for(var i=0, l=wa.length*4; i<l; i+=3){
  91. var t=(((wa[i>>2]>>8*(3-i%4))&0xFF)<<16)|(((wa[i+1>>2]>>8*(3-(i+1)%4))&0xFF)<<8)|((wa[i+2>>2]>>8*(3-(i+2)%4))&0xFF);
  92. for(var j=0; j<4; j++){
  93. if(i*8+j*6>wa.length*32){
  94. s.push(p);
  95. } else {
  96. s.push(tab.charAt((t>>6*(3-j))&0x3F));
  97. }
  98. }
  99. }
  100. return s.join(""); // string
  101. };
  102. // public function
  103. dxd.SHA1=function(/* String */data, /* dojox.encoding.digests.outputTypes? */outputType){
  104. // summary:
  105. // Computes the SHA1 digest of the data, and returns the result according to output type.
  106. var out=outputType||dxd.outputTypes.Base64;
  107. var wa=core(toWord(data), data.length*chrsz);
  108. switch(out){
  109. case dxd.outputTypes.Raw:{
  110. return wa; // word[]
  111. }
  112. case dxd.outputTypes.Hex:{
  113. return toHex(wa); // string
  114. }
  115. case dxd.outputTypes.String:{
  116. return _toString(wa); // string
  117. }
  118. default:{
  119. return toBase64(wa); // string
  120. }
  121. }
  122. }
  123. // make this private, for later use with a generic HMAC calculator.
  124. dxd.SHA1._hmac=function(/* string */data, /* string */key, /* dojox.encoding.digests.outputTypes? */outputType){
  125. // summary:
  126. // computes the digest of data, and returns the result according to type outputType
  127. var out=outputType || dxd.outputTypes.Base64;
  128. var wa=hmac(data, key);
  129. switch(out){
  130. case dxd.outputTypes.Raw:{
  131. return wa; // word[]
  132. }
  133. case dxd.outputTypes.Hex:{
  134. return toHex(wa); // string
  135. }
  136. case dxd.outputTypes.String:{
  137. return _toString(wa); // string
  138. }
  139. default:{
  140. return toBase64(wa); // string
  141. }
  142. }
  143. };
  144. })();
  145. }