SHA1.js 4.2 KB

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