[Pkg-javascript-commits] [node-sha.js] 09/237: separate basic stuff into Hash function

Bastien Roucariès rouca at moszumanska.debian.org
Fri May 5 09:02:50 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-sha.js.

commit fe59f0cb949fa9e4b5f162f54ebe7b3312b1fc0f
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Thu Dec 26 01:00:47 2013 +0700

    separate basic stuff into Hash function
---
 hash.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)

diff --git a/hash.js b/hash.js
new file mode 100644
index 0000000..578efe7
--- /dev/null
+++ b/hash.js
@@ -0,0 +1,67 @@
+var u = require('./util')
+var hexpp = require('./hexpp')
+
+module.exports = Hash
+
+//prototype class for hash functions
+function Hash (blockSize) {
+  this._block = new Uint32Array(blockSize/4)
+  this._len = 0
+  this._l = 0
+}
+
+Hash.prototype.update = function (data, enc) {
+  //convert to array of ints.
+  //since this is probably a string, copy it into the array,
+  //if it's over 16 words (and so, we have filled _i)
+  //then call _update(). if it's equal less, we have to wait,
+  //because this might be the last block, and so we have to wait for final()
+
+  //for encoding/decoding utf8, see here:
+  //https://github.com/chrisdickinson/bops/blob/master/typedarray/from.js#L36-L57
+  //https://github.com/chrisdickinson/to-utf8
+
+  //for now, assume ascii.
+  var start = this._l || 0
+  this._len += data.length
+  console.log('update', JSON.stringify(data), start, data.length)
+  var bl = this._block.byteLength
+
+  if(data.length <= bl - start) {
+    u.write(this._x.buffer, data, 'ascii', start, 0, data.length)
+    this._l = (this._l || 0) + data.length
+  }
+  else {
+    var from = 0
+    var to = (from + bl) - this._l
+    while(from < data.length) {
+      console.log('OVERFLOW')
+      u.write(this._x.buffer, data, 'ascii', this._l % bl, from, to)
+      from = to
+      to = Math.min(bl - this._l, data.length)
+      this._update()
+      this._l = 0
+    }
+  }
+  console.log('---WRITTEN---')
+  console.log(hexpp(this._x))
+  return this
+
+}
+
+Hash.prototype.digest = function (enc) {
+  this._final()
+  //reverse byte order, so that the individual bytes are in correct order.
+  return u.toHex(this._h.buffer)
+
+}
+
+Hash.prototype._update = function () {
+  throw new Error('_update must be implemented by subclass')
+}
+
+Hash.prototype._final = function () {
+  throw new Error('_final must be implemented by subclass')
+}
+
+

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-sha.js.git



More information about the Pkg-javascript-commits mailing list