[Pkg-javascript-commits] [node-keygrip] 54/68: refactor into constructor/prototype

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Jun 27 22:13:28 UTC 2014


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

andrewrk-guest pushed a commit to branch master
in repository node-keygrip.

commit 49a161f81a749670f96bc13fd8e669c981095758
Author: Jonathan Ong <jonathanrichardong at gmail.com>
Date:   Sat May 17 15:30:27 2014 -0700

    refactor into constructor/prototype
---
 index.js     | 55 +++++++++++++++++++++++++++++++++----------------------
 package.json |  2 +-
 2 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/index.js b/index.js
index 0fa4c01..21aa5d9 100644
--- a/index.js
+++ b/index.js
@@ -5,36 +5,47 @@ var constantTimeCompare = require('scmp')
 module.exports = Keygrip
 
 function Keygrip(keys, algorithm, encoding) {
-  if (!algorithm) algorithm = "sha1";
-  if (!encoding) encoding = "base64";
+  if (!Array.isArray(keys) || !keys.length) throw new Error("Keys must be provided.")
   if (!(this instanceof Keygrip)) return new Keygrip(keys, algorithm, encoding)
 
-  if (!keys || !(0 in keys)) {
-    throw new Error("Keys must be provided.")
-  }
+  this.keys = keys
+  this.algorithm = algorithm || 'sha1'
+  this.encoding = encoding || 'base64'
+}
 
-  function sign(data, key) {
-    return crypto
-      .createHmac(algorithm, key)
-      .update(data).digest(encoding)
-      .replace(/\/|\+|=/g, function(x) {
-        return ({ "/": "_", "+": "-", "=": "" })[x]
-      })
-  }
+Keygrip.prototype.sign = function Keygrip$_sign(data, key) {
+  // default to the first key
+  key = key || this.keys[0]
 
-  this.sign = function(data){ return sign(data, keys[0]) }
+  return crypto
+    .createHmac(this.algorithm, key)
+    .update(data)
+    .digest(this.encoding)
+    .replace(/\/|\+|=/g, _sign_replace)
+}
 
-  this.verify = function(data, digest) {
-    return this.index(data, digest) > -1
-  }
+// replace base64 characters with more friendly ones
+var _sign_replacements = {
+  "/": "_",
+  "+": "-",
+  "=": ""
+}
 
-  this.index = function(data, digest) {
-    for (var i = 0, l = keys.length; i < l; i++) {
-      if (constantTimeCompare(digest, sign(data, keys[i]))) return i
-    }
+function _sign_replace(x) {
+  return _sign_replacements[x]
+}
 
-    return -1
+Keygrip.prototype.verify = function Keygrip$_verify(data, digest) {
+  return this.index(data, digest) > -1
+}
+
+Keygrip.prototype.index = function Keygrip$_index(data, digest) {
+  var keys = this.keys
+  for (var i = 0, l = keys.length; i < l; i++) {
+    if (constantTimeCompare(digest, this.sign(data, keys[i]))) return i
   }
+
+  return -1
 }
 
 Keygrip.sign = Keygrip.verify = Keygrip.index = function() {
diff --git a/package.json b/package.json
index 2f4f2ac..3adea1a 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,7 @@
   "version": "1.0.0",
   "description": "Key signing and verification for rotated credentials",
   "scripts": {
-    "test": "mocha --reporter spec"
+    "test": "mocha --reporter spec --bail"
   },
   "repository": "expressjs/keygrip",
   "dependencies": {

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



More information about the Pkg-javascript-commits mailing list