[Pkg-javascript-commits] [node-sha.js] 32/237: support hex encoding

Bastien Roucariès rouca at moszumanska.debian.org
Fri May 5 09:02:53 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 b1488b5dd416c4525e6bc817d7b87da27b275c94
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date:   Fri Dec 27 11:40:05 2013 +0700

    support hex encoding
---
 hash.js | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/hash.js b/hash.js
index 340a2c4..f3d3352 100644
--- a/hash.js
+++ b/hash.js
@@ -12,6 +12,13 @@ function Hash (blockSize, finalSize) {
   this._l = 0
 }
 
+function lengthOf(data, enc) {
+  if(enc == null)     return data.byteLength || data.length
+  if(enc == 'ascii')  return data.length
+  if(enc == 'hex')    return data.length/2
+  if(enc == 'base64') return data.length/3
+}
+
 Hash.prototype.update = function (data, enc) {
   //for encoding/decoding utf8, see here:
   //https://github.com/chrisdickinson/bops/blob/master/typedarray/from.js#L36-L57
@@ -19,12 +26,16 @@ Hash.prototype.update = function (data, enc) {
   var bl = this._block.byteLength
   //for now, assume ascii.
 
-  var l = this._len += data.length
+  if('string' === typeof data && !enc)
+    throw new Error('encoding is mandatory when data is string')
+
+  var length = lengthOf(data, enc)
+  var l = this._len += length
   var s = this._s = (this._s || 0)
   var f = 0
   while(s < l) {
-    var t = Math.min(data.length, f + bl)
-    u.write(this._block.buffer, data, 'ascii', s%bl, f, t, true)
+    var t = Math.min(length, f + bl)
+    u.write(this._block.buffer, data, enc, s%bl, f, t, true)
     s += (t - f)
 
     if(!(s%bl)) {

-- 
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