Subversion Repositories oidplus

Rev

Rev 1403 | Rev 1419 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1403 Rev 1417
Line 1... Line 1...
1
/**
1
/**
2
 * [js-sha3]{@link https://github.com/emn178/js-sha3}
2
 * [js-sha3]{@link https://github.com/emn178/js-sha3}
3
 *
3
 *
4
 * @version 0.9.1
4
 * @version 0.9.0
5
 * @author Chen, Yi-Cyuan [emn178@gmail.com]
5
 * @author Chen, Yi-Cyuan [emn178@gmail.com]
6
 * @copyright Chen, Yi-Cyuan 2015-2023
6
 * @copyright Chen, Yi-Cyuan 2015-2023
7
 * @license MIT
7
 * @license MIT
8
 */
8
 */
9
/*jslint bitwise: true */
9
/*jslint bitwise: true */
Line 56... Line 56...
56
    ArrayBuffer.isView = function (obj) {
56
    ArrayBuffer.isView = function (obj) {
57
      return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
57
      return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
58
    };
58
    };
59
  }
59
  }
60
 
60
 
61
  var formatMessage = function (message) {
-
 
62
    var notString, type = typeof message;
-
 
63
    if (type !== 'string') {
-
 
64
      if (type === 'object') {
-
 
65
        if (message === null) {
-
 
66
          throw new Error(INPUT_ERROR);
-
 
67
        } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
-
 
68
          message = new Uint8Array(message);
-
 
69
        } else if (!Array.isArray(message)) {
-
 
70
          if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
-
 
71
            throw new Error(INPUT_ERROR);
-
 
72
          }
-
 
73
        }
-
 
74
      } else {
-
 
75
        throw new Error(INPUT_ERROR);
-
 
76
      }
-
 
77
      notString = true;
-
 
78
    }
-
 
79
    return [message, notString];
-
 
80
  }
-
 
81
 
-
 
82
  var empty = function (message) {
-
 
83
    return formatMessage(message)[0].length === 0;
-
 
84
  };
-
 
85
 
-
 
86
  var createOutputMethod = function (bits, padding, outputType) {
61
  var createOutputMethod = function (bits, padding, outputType) {
87
    return function (message) {
62
    return function (message) {
88
      return new Keccak(bits, padding, bits).update(message)[outputType]();
63
      return new Keccak(bits, padding, bits).update(message)[outputType]();
89
    };
64
    };
90
  };
65
  };
Line 139... Line 114...
139
 
114
 
140
  var createCshakeMethod = function (bits, padding) {
115
  var createCshakeMethod = function (bits, padding) {
141
    var w = CSHAKE_BYTEPAD[bits];
116
    var w = CSHAKE_BYTEPAD[bits];
142
    var method = createCshakeOutputMethod(bits, padding, 'hex');
117
    var method = createCshakeOutputMethod(bits, padding, 'hex');
143
    method.create = function (outputBits, n, s) {
118
    method.create = function (outputBits, n, s) {
144
      if (empty(n) && empty(s)) {
119
      if (!n && !s) {
145
        return methods['shake' + bits].create(outputBits);
120
        return methods['shake' + bits].create(outputBits);
146
      } else {
121
      } else {
147
        return new Keccak(bits, padding, outputBits).bytepad([n, s], w);
122
        return new Keccak(bits, padding, outputBits).bytepad([n, s], w);
148
      }
123
      }
149
    };
124
    };
Line 211... Line 186...
211
 
186
 
212
  Keccak.prototype.update = function (message) {
187
  Keccak.prototype.update = function (message) {
213
    if (this.finalized) {
188
    if (this.finalized) {
214
      throw new Error(FINALIZE_ERROR);
189
      throw new Error(FINALIZE_ERROR);
215
    }
190
    }
216
    var result = formatMessage(message);
191
    var notString, type = typeof message;
-
 
192
    if (type !== 'string') {
-
 
193
      if (type === 'object') {
217
    message = result[0];
194
        if (message === null) {
-
 
195
          throw new Error(INPUT_ERROR);
-
 
196
        } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
-
 
197
          message = new Uint8Array(message);
-
 
198
        } else if (!Array.isArray(message)) {
-
 
199
          if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
-
 
200
            throw new Error(INPUT_ERROR);
-
 
201
          }
-
 
202
        }
-
 
203
      } else {
-
 
204
        throw new Error(INPUT_ERROR);
-
 
205
      }
218
    var notString = result[1];
206
      notString = true;
-
 
207
    }
219
    var blocks = this.blocks, byteCount = this.byteCount, length = message.length,
208
    var blocks = this.blocks, byteCount = this.byteCount, length = message.length,
220
      blockCount = this.blockCount, index = 0, s = this.s, i, code;
209
      blockCount = this.blockCount, index = 0, s = this.s, i, code;
221
 
210
 
222
    while (index < length) {
211
    while (index < length) {
223
      if (this.reset) {
212
      if (this.reset) {
Line 287... Line 276...
287
    this.update(bytes);
276
    this.update(bytes);
288
    return bytes.length;
277
    return bytes.length;
289
  };
278
  };
290
 
279
 
291
  Keccak.prototype.encodeString = function (str) {
280
  Keccak.prototype.encodeString = function (str) {
-
 
281
    var notString, type = typeof str;
-
 
282
    if (type !== 'string') {
-
 
283
      if (type === 'object') {
-
 
284
        if (str === null) {
-
 
285
          throw new Error(INPUT_ERROR);
-
 
286
        } else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) {
292
    var result = formatMessage(str);
287
          str = new Uint8Array(str);
-
 
288
        } else if (!Array.isArray(str)) {
-
 
289
          if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) {
-
 
290
            throw new Error(INPUT_ERROR);
-
 
291
          }
-
 
292
        }
293
    str = result[0];
293
      } else {
-
 
294
        throw new Error(INPUT_ERROR);
-
 
295
      }
294
    var notString = result[1];
296
      notString = true;
-
 
297
    }
295
    var bytes = 0, length = str.length;
298
    var bytes = 0, length = str.length;
296
    if (notString) {
299
    if (notString) {
297
      bytes = length;
300
      bytes = length;
298
    } else {
301
    } else {
299
      for (var i = 0; i < str.length; ++i) {
302
      for (var i = 0; i < str.length; ++i) {