123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- goog.provide('goog.crypt.Sha1');
- goog.require('goog.crypt.Hash');
- goog.crypt.Sha1 = function() {
- goog.crypt.Sha1.base(this, 'constructor');
- this.blockSize = 512 / 8;
-
- this.chain_ = [];
-
- this.buf_ = [];
-
- this.W_ = [];
-
- this.pad_ = [];
- this.pad_[0] = 128;
- for (var i = 1; i < this.blockSize; ++i) {
- this.pad_[i] = 0;
- }
-
- this.inbuf_ = 0;
-
- this.total_ = 0;
- this.reset();
- };
- goog.inherits(goog.crypt.Sha1, goog.crypt.Hash);
- goog.crypt.Sha1.prototype.reset = function() {
- this.chain_[0] = 0x67452301;
- this.chain_[1] = 0xefcdab89;
- this.chain_[2] = 0x98badcfe;
- this.chain_[3] = 0x10325476;
- this.chain_[4] = 0xc3d2e1f0;
- this.inbuf_ = 0;
- this.total_ = 0;
- };
- goog.crypt.Sha1.prototype.compress_ = function(buf, opt_offset) {
- if (!opt_offset) {
- opt_offset = 0;
- }
- var W = this.W_;
-
- if (goog.isString(buf)) {
- for (var i = 0; i < 16; i++) {
-
-
-
-
-
-
-
-
- W[i] = (buf.charCodeAt(opt_offset) << 24) |
- (buf.charCodeAt(opt_offset + 1) << 16) |
- (buf.charCodeAt(opt_offset + 2) << 8) |
- (buf.charCodeAt(opt_offset + 3));
- opt_offset += 4;
- }
- } else {
- for (var i = 0; i < 16; i++) {
- W[i] = (buf[opt_offset] << 24) | (buf[opt_offset + 1] << 16) |
- (buf[opt_offset + 2] << 8) | (buf[opt_offset + 3]);
- opt_offset += 4;
- }
- }
-
- for (var i = 16; i < 80; i++) {
- var t = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
- W[i] = ((t << 1) | (t >>> 31)) & 0xffffffff;
- }
- var a = this.chain_[0];
- var b = this.chain_[1];
- var c = this.chain_[2];
- var d = this.chain_[3];
- var e = this.chain_[4];
- var f, k;
-
- for (var i = 0; i < 80; i++) {
- if (i < 40) {
- if (i < 20) {
- f = d ^ (b & (c ^ d));
- k = 0x5a827999;
- } else {
- f = b ^ c ^ d;
- k = 0x6ed9eba1;
- }
- } else {
- if (i < 60) {
- f = (b & c) | (d & (b | c));
- k = 0x8f1bbcdc;
- } else {
- f = b ^ c ^ d;
- k = 0xca62c1d6;
- }
- }
- var t = (((a << 5) | (a >>> 27)) + f + e + k + W[i]) & 0xffffffff;
- e = d;
- d = c;
- c = ((b << 30) | (b >>> 2)) & 0xffffffff;
- b = a;
- a = t;
- }
- this.chain_[0] = (this.chain_[0] + a) & 0xffffffff;
- this.chain_[1] = (this.chain_[1] + b) & 0xffffffff;
- this.chain_[2] = (this.chain_[2] + c) & 0xffffffff;
- this.chain_[3] = (this.chain_[3] + d) & 0xffffffff;
- this.chain_[4] = (this.chain_[4] + e) & 0xffffffff;
- };
- goog.crypt.Sha1.prototype.update = function(bytes, opt_length) {
-
- if (bytes == null) {
- return;
- }
- if (!goog.isDef(opt_length)) {
- opt_length = bytes.length;
- }
- var lengthMinusBlock = opt_length - this.blockSize;
- var n = 0;
-
- var buf = this.buf_;
- var inbuf = this.inbuf_;
-
- while (n < opt_length) {
-
-
-
-
- if (inbuf == 0) {
- while (n <= lengthMinusBlock) {
- this.compress_(bytes, n);
- n += this.blockSize;
- }
- }
- if (goog.isString(bytes)) {
- while (n < opt_length) {
- buf[inbuf] = bytes.charCodeAt(n);
- ++inbuf;
- ++n;
- if (inbuf == this.blockSize) {
- this.compress_(buf);
- inbuf = 0;
-
- break;
- }
- }
- } else {
- while (n < opt_length) {
- buf[inbuf] = bytes[n];
- ++inbuf;
- ++n;
- if (inbuf == this.blockSize) {
- this.compress_(buf);
- inbuf = 0;
-
- break;
- }
- }
- }
- }
- this.inbuf_ = inbuf;
- this.total_ += opt_length;
- };
- goog.crypt.Sha1.prototype.digest = function() {
- var digest = [];
- var totalBits = this.total_ * 8;
-
- if (this.inbuf_ < 56) {
- this.update(this.pad_, 56 - this.inbuf_);
- } else {
- this.update(this.pad_, this.blockSize - (this.inbuf_ - 56));
- }
-
- for (var i = this.blockSize - 1; i >= 56; i--) {
- this.buf_[i] = totalBits & 255;
- totalBits /= 256;
- }
- this.compress_(this.buf_);
- var n = 0;
- for (var i = 0; i < 5; i++) {
- for (var j = 24; j >= 0; j -= 8) {
- digest[n] = (this.chain_[i] >> j) & 255;
- ++n;
- }
- }
- return digest;
- };
|